大家好,我对此有点麻烦,我在这里找到了一些主题,但是无论如何似乎都没有帮助,这是我的问题。我需要使用.filter数组在应用程序组件上创建一个删除按钮以及一个delete todo方法,然后将其作为prop传递给ToDo组件,并将onClick事件侦听器添加到删除按钮,并调用deleteTodo,将待办事项的索引传递给它。
import React, { Component } from 'react';
import './App.css';
import ToDo from './components/ToDo.js';
class App extends Component {
constructor(props) {
super(props);
this.state = {
todos: [
{ description: 'Walk the cat', isCompleted: true},
{ description: 'Throw away the dishes', isCompleted: false},
{ description: 'Buy new dishes', isCompleted: false}
],
newTodoDescription: ''
};
}
handleChange(e) {
this.setState({ newTodoDescription: e.target.value })
}
handleSubmit(e) {
e.preventDefault();
if (!this.state.newTodoDescription) { return }
const newTodo = { description: this.state.newTodoDescription, isCompleted: false};
this.setState({ todos: [...this.state.todos, newTodo], newTodoDescription: ''});
}
toggleComplete(index) {
const todos = this.state.todos.slice();
const todo = todos[index];
todo.isCompleted = todo.isCompleted ? false : true;
this.setState({ todos: todos});
}
render() {
return (
<div className="App">
<ul>
{ this.state.todos.map( (todo, index) =>
<ToDo key={ index } description={ todo.description } isCompleted={ todo.isCompleted } toggleComplete={ () => this.toggleComplete(index) } />
)}
</ul>
<form onSubmit={ (e) => this.handleSubmit(e) }>
<input type="text" value={ this.state.newTodoDescription } onChange={ (e) => this.handleChange(e) } />
<input type="submit" />
</form>
</div>
);
}
}
export default App;
import React, {Component} from 'react';
class ToDo extends Component {
render() {
return (
<li>
<input type="checkbox" checked={ this.props.isCompleted } onChange={ this.props.toggleComplete } />
<span> { this.props.description }</span>
</li>
);
}
}
export default ToDo;
答案 0 :(得分:0)
您可以在deleteTodo
中创建一个新方法App
,该方法接受要删除的todo
对象,并将其从状态中过滤掉。
由于todos
的顺序现在将更改,因此您不能再将数组索引用作key
组件的Todo
。您可以将id
添加到待办事项对象中,并使用它。
示例
class App extends React.Component {
state = {
todos: [
{ id: Math.random(), description: "Buy new dishes", isCompleted: false }
],
newTodoDescription: ""
};
handleChange(e) {
this.setState({ newTodoDescription: e.target.value });
}
handleSubmit(e) {
e.preventDefault();
if (!this.state.newTodoDescription) {
return;
}
const newTodo = {
id: Math.random(),
description: this.state.newTodoDescription,
isCompleted: false
};
this.setState({
todos: [...this.state.todos, newTodo],
newTodoDescription: ""
});
}
toggleComplete(todo) {
this.setState({
todos: this.state.todos.map(t => {
if (t !== todo) {
return t;
}
return { ...t, isCompleted: !t.isCompleted };
})
});
}
deleteTodo(todo) {
this.setState({ todos: this.state.todos.filter(t => t !== todo) });
}
render() {
return (
<div className="App">
<ul>
{this.state.todos.map(todo => (
<ToDo
key={todo.id}
description={todo.description}
isCompleted={todo.isCompleted}
toggleComplete={() => this.toggleComplete(todo)}
deleteTodo={() => this.deleteTodo(todo)}
/>
))}
</ul>
<form onSubmit={e => this.handleSubmit(e)}>
<input
type="text"
value={this.state.newTodoDescription}
onChange={e => this.handleChange(e)}
/>
<input type="submit" />
</form>
</div>
);
}
}
class ToDo extends React.Component {
render() {
return (
<li>
<input
type="checkbox"
checked={this.props.isCompleted}
onChange={this.props.toggleComplete}
/>
<span> {this.props.description}</span>
<button onClick={this.props.deleteTodo}>Delete</button>
</li>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>