DEV Community

Discussion on: Managing State with useState hook with to-do list example

Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

The deleteHandler is very messy. Could have been something like that:

const deleteHandler = (e) => {
    const id = e.target.parentNode.id;
    const updatedTasks = tasks
       .filter((task) => {
         return task.id !== id
       })
       .reduce((acc, curr) => {
         const last = acc[acc.lenght - 1]
         if (!last) return [curr]
         return [...acc, {
            ...curr,
            order: last.order + 1,
         }]
       }, [])

    // Update tasks
    setTasks([...updatedTasks]);

    setTask((prevTask) => ({
      text: "",
      order: prevTask.order - 1,
      id: uniqid()
    }));
  };
Enter fullscreen mode Exit fullscreen mode
Collapse
 
hoangquochung1110 profile image
Quoc-Hung Hoang

Highly appreciated feedback ! The deleteHandler should be that simple as you pointed