Advertisement
Guest User

Todo exmple

a guest
Jun 3rd, 2024
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const Todo = () => {
  2.   const [text, setText] = useState('');
  3.   const [list, setList] = useState([]);
  4.  
  5.   function onChangeText(e) {
  6.     setText(e.target.value);
  7.   }
  8.  
  9.   function addTask() {
  10.     /**
  11.      * Use setState callback to safely access the current state
  12.      * use the current list's length to generate the id
  13.      */
  14.     setList((current) => [...current, { id: current.length, title: text }]);
  15.  
  16.     /**
  17.      * Clear the input after adding the task
  18.      */
  19.     setText('');
  20.   }
  21.  
  22.   function deleteTask(id) {
  23.     /**
  24.      * Use setState callback to safely access the current state
  25.      * filter the list to remove the item with the id
  26.      * then map the list to update the ids to avoid future ID collisions
  27.      * since we use the list length to generate the id if we had a list of
  28.      * [{ id: 0, title: 'task 1' }, { id: 1, title: 'task 2' }] and then we
  29.      * delete the first item the list would be [{ id: 1, title: 'task 2' }]
  30.      * and the next time we add an item length would be 1 and the id would be 1
  31.      * so we would have 2 items with the same id.
  32.      * To avoid this we map the list to update the ids after filtering the list
  33.      *
  34.      * This is a good reason to not use list length to generate ids
  35.      * there is no real reason to have auto incrementing ids in the todo list
  36.      * consider using a uuid library to generate unique ids instead
  37.      */
  38.     setList((current) =>
  39.       current
  40.         .filter((item) => item.id !== id)
  41.         .map((item, index) => ({ ...item, id: index + 1 })
  42.   );
  43.   }
  44.  
  45.   return (
  46.     <>
  47.       <label htmlFor="item">Type item here: </label>
  48.       {
  49.         /**
  50.          * Use a controlled component for the input to avoid
  51.          * having to use a dom selector to get the current value for saving a todo
  52.         */
  53.       }
  54.       <input
  55.         type="text"
  56.         id="input-textbox"
  57.         onChange={onChangeText}
  58.         value={text}
  59.       />
  60.  
  61.       <div>
  62.         {/**
  63.          * disable the button if there's no text in the input
  64.          * to avoid adding empty todo item
  65.          */}
  66.         <button disabled={Boolean(text.length)} onClick={addTask}>Add task button</button>
  67.  
  68.         {list.map((item) => {
  69.           return (
  70.             <div>
  71.               {item.id} : {item.title}
  72.               <button onClick={() => deleteTask(item.id)}>delete</button>
  73.             </div>
  74.           );
  75.         })}
  76.       </div>
  77.     </>
  78.   );
  79. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement