【发布时间】:2020-06-09 00:37:44
【问题描述】:
我是 React 的新手,我一直在使用 State Hooks 制作一个简单的待办事项列表。我无法为 style={{textDecoration: Checkbox.checked ? "line-through" : "" }} 分配函数
function Checkbox(){
const [checked, setChecked] = useState(false);
return (
<label>
<input type="checkbox"
checked={checked}
onChange={() => setChecked(!checked)}
/>
</label>
);
}
为了完成一些任务。任务应该是默认的“任务”,但一旦选中,我希望任务被划掉:“T̶a̶s̶k̶”。我会为你发布我的尝试:
import React, {useState} from 'react';
function TodoList({ todo, index, completeTodo, removeTodo}) {
return (
<div className="todo">
<Checkbox />
<div style={{textDecoration: Checkbox.checked ? "" : "line-through" }} >
{todo.text}
</div>
<div>
<button class = "button" onClick={() => removeTodo(index)}>Delete Task</button>
</div>
</div>
);
}
function Checkbox() {
const [checked, setChecked] = useState(false);
return (
<label>
<input type="checkbox"
checked={checked}
onChange={() => setChecked(!checked)}
/>
</label>
);
}
export default TodoList;
【问题讨论】:
-
奇怪的是 TodoList 只呈现单个 Todo 并且您正在从列表中删除项目但使用 index as key,查找
We don’t recommend using indexes for keys。您还有来自某个地方的completeTodo,但您认为在关于如何完成待办事项的问题中提及它的来源无关紧要。
标签: javascript reactjs checkbox react-hooks