英文:
Removing a todo from todo list in react
问题
I am creating this todo list app, but I can't seem to find out how to make the react icon when clicked to delete that value that it's next to.
我正在创建这个待办事项列表应用,但似乎无法弄清楚如何使点击时的 React 图标删除其旁边的值。
I tried doing this:
我尝试了以下方法:
const click = () => {
setTodos(todos => todos.filter((item) => item.id));
}
return (
<>
Your Todos
<NewTodo onAdd={(newTodo) => setTodos([...todos, newTodo])} />
-
{visibleTodos.map((todo) => (
-
{todo.text}
))}
</>
);
When I clicked the icon next to the todo it got deleted, but when I tried deleting another value it didn't work. I could only delete one todo.
当我点击待办事项旁边的图标时,它会被删除,但当我尝试删除另一个值时,它不起作用。我只能删除一个待办事项。
英文:
I am creating this todo list app, but I can't seem to find out how to make the react icon when clicked to delete that value that it's next to.
I tried doing this:
const click = () => {
setTodos(todos => todos.filter((item) => item.id));
}
return (
<>
<h2>Your Todos</h2>
<NewTodo onAdd={(newTodo) => setTodos([...todos, newTodo])} />
<ul>
{visibleTodos.map((todo) => (
<li key={todo.id}>
{todo.text}
<button onClick={click}><AiFillDelete /></button>
</li>
))}
</ul>
</>
);
}
When I clicked the icon next to the todo it got deleted, but when I tried deleting another value it didn't work. I could only delete one todo.
答案1
得分: 0
This is meant to filter:
todos.filter((item) => item.id)
This code returns all items that have an ID, which is probably all of them.
Typically, you would filter items based on a condition. For example, to filter items that don't match a specific ID:
todos.filter((item) => item.id !== id)
In this case, you need a value for id
, which can be passed to the function:
const click = (id) => {
Then, you can pass the ID to the function like this:
onClick={() => click(todo.id)}
英文:
What is this meant to filter?:
todos.filter((item) => item.id)
This is returning all items which have an ID, which is probably all of them, no? Generally you would filter items which match a condition. For example, all items which don't match a specific ID:
todos.filter((item) => item.id !== id)
So then you'd need a value for id
, which can be passed to the function:
const click = (id) => {
And then you'd just need to pass the ID to the function:
onClick={() => click(todo.id)}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论