【发布时间】:2021-12-26 14:21:18
【问题描述】:
我正在尝试创建一个事件来删除按钮单击时的元素,但收到错误“TypeError: Cannot read properties of undefined (reading 'filter')”
const Todo = ({ text, todo, todos, setTodos }) => {
const deleteHandler = () => {
setTodos(todos.filter((el) => el.id !== todo.id));
};
return (
<div className="todo">
<li className="todo-item">{text}</li>
<button className="complete-btn">
<i className="fas fa-check"></i>
</button>
<button onClick={deleteHandler} className="trash-btn">
<i className="fas fa-trash"></i>
</button>
</div>
);
};
【问题讨论】:
-
todos 可能不是一个数组。只有数组具有过滤器属性。
-
没有名为id的属性