我正在使用复选框实现 Todo。我在该州有一个 id、content、status 属性。Status 有 3 种状态(pending、done、deleted)。待办事项列表有一个复选框。如果状态为待定,则不应选中复选框。如果状态为完成,则应选中复选框。默认情况下,状态为待定。应根据状态选中/取消选中复选框。在删除待办事项时,状态应更新为已删除。现在我对复选框的实现感到震惊。它没有按照要求工作。
应用程序.js:
import React from 'react';
import Todo from './Todo';
import AddTodo from './AddTodo';
class App extends React.Component{
state={
todos:[
{id:1,content:'Buy milk1',status:'pending'},
{id:2, content:'buy carrots1', status: 'pending'},
{id:3,content:'Buy milk2',status:'done'},
{id:4, content:'buy carrots2', status: 'deleted'}
]
}
onDelete=(id)=>{
const todo = this.state.todos.find((todoItem => todoItem.id === id))
todo.status ='deleted';
this.setState([...this.state.todos]);
}
onChangeCheckbox=(id, checked)=>{
const todo = this.state.todos.find((todoItem => todoItem.id === id))
if(checked){
todo.status = 'done'
}
else{
todo.status = 'pending'
}
this.setState([...this.state.todos]);
}
addTodo=(todo)=>{
todo.id=Math.random();
todo.status = "pending";
let todos=[...this.state.todos,todo];
this.setState({todos});
}
render(){
return(
<div>
<h1>Todo's App</h1>
<AddTodo addTodo={this.addTodo} />
<Todo todos={this.state.todos} deleteTodo={this.onDelete} onChangeCheckbox=
{this.onChangeCheckbox} />
</div>
)
}
}
export default App;
慕村9548890
回首忆惘然
慕工程0101907
相关分类