[英]React : Simple Todo App, Checked Box not updating
Working on a simple React project for learning purposes.出于学习目的在一个简单的 React 项目上工作。 I've been able to get everything functioning so far except for the checked box is not updating when it is clicked.
到目前为止,我已经能够让所有功能正常运行,除了选中的框在单击时不会更新。 The state of the app doesn't seem to update.
app的state好像没有更新。 Any help is appreciated!Thank you!
任何帮助表示赞赏!谢谢!
import React from 'react';
let id = 0
const Todo = props => (
<li>
<input type="checkbox" checked={props.todo.checked} onChange={props.onToggle} />
<button onClick = {props.onDelete}> delete</button>
<span>{props.todo.text}</span>
</li>
)
class App extends React.Component {
constructor() {
super()
this.state = {
todos: [],
}
}
addTodo() {
const text = prompt("TODO TEXT PLEASE!")
this.setState({
todos: [...this.state.todos, {id: id++, text: text, checked: false},
]
})
}
toggleTodo(id) {
this.setstate({
todos: this.state.todos.map(todo => {
if (todo.id !== id) return todo
return {
id: todo.id,
text: todo.text,
checked: !todo.checked,
}
})
})
}
render() {
return (
<div>
<div> Todo Count: {this.state.todos.length}</div>
<div> Unchecked Count: {this.state.todos.filter(todo => !todo.checked).length} </div>
<button onClick={() => this.addTodo()}> Add TODO </button>
<ul>
{this.state.todos.map(todo => (
<Todo
onToggle={() => this.toggleTodo(todo.id)}
onDelete={() => this.removeTodo(todo.id)}
todo={todo}
/>
))}
</ul>
</div>
)
}
}
export default App;
You have a typo error just change this.setstate
to this.setState
你有一个拼写错误只需将
this.setstate
更改为this.setState
toggleTodo(id) {
this.setState({
todos: this.state.todos.map(todo => {
if (todo.id !== id) return todo
return {
id: todo.id,
text: todo.text,
checked: !todo.checked,
}
})
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.