Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why I cant delete todo item in todo list?

What should I do to remove item from my todo list? What did i do wrong?

Here is my code

function App1() {
  const myInputValue = useRef();
  const [todo, setToDo] = useState([]);
  const addItem = (event) => {
    event.preventDefault();
    const id = uuid();
    setToDo([
      ...todo,
      {
        id: id,
        name: myInputValue.current.value,
      },
    ]);
    myInputValue.current.value = "";
  };
  const removeItem = e => {
        const data = todo.filter(el => el.e !== e);
        setToDo(data);
  };
  const listItems = todo.map((element) => (
    <li key={element.id}>
      {element.name}
      <button>Done</button>
      <button onClick={() => removeItem(element.id)}>Delete</button>
    </li>
  ));

  return (
    <div>
      <input ref={myInputValue}></input>
      <button onClick={addItem}>Add</button>
      <ul>
        {listItems}
      </ul>
    </div>
  );
}

export default App1;

What should I change in my removeItem function? Where I do a mistake? Please help

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Iam not sure about it, but when you are adding an item, you add an object with these following keys (id and name), so why when you call removeItem, you test with elemnt.e; didn’t you mean to write this ? :

todo.filter(el => el.id !== e)

instead of this:

todo.filter(el => el.e !== e)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading