React Todo删除按钮一次删除所有列出的项目

时间:2018-02-04 02:56:52

标签: javascript reactjs

我有2个文件

App.js

    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: [],
          newTodoDescription: ''
        };
        this.deleteTodo = this.deleteTodo.bind(this);
      }

    handleChange(e) {
      this.setState({ newTodoDescription: e.target.value })
    }

    handleSubmit(e) {
      e.preventDefault();
      if (!this.state.newTodoDescription) { return }
      const newTodo = { id: this.state.todos.id, 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 });
    }




    deleteTodo(id) {
      const remainingToDos = this.state.todos.filter((todo, remainingToDos) => {
          if(todo.id !== remainingToDos.id) return todo; });
      this.setState({ todos: remainingToDos });
    }


      render() {
        return (
          <div className="App">
            <h1>Add a ToDo!</h1>
            <form onSubmit={ (e) => this.handleSubmit(e)}>
              <input type="text"
                value={ this.state.newTodoDescription }
                onChange={ (e) => this.handleChange(e) }
                />
              <input type="submit" value="Add Todo" />
            </form>
            <ul>
              { this.state.todos.map( (todo) =>
                <ToDo key={ todo.id }
                  description={ todo.description }
                  isCompleted={ todo.isCompleted }
                  toggleComplete={ () => this.toggleComplete(todo) }
                  onDelete={ this.deleteTodo }
                   />
              )}

            </ul>
          </div>
        );
      }
    }

    export default App;

ToDo.js

      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>
              <button onClick={() => this.props.onDelete(this.props.id)}>Remove Todo</button>
            </li>
          );
        }
      }

      export default ToDo;

我想要完成的任务:在列表中添加许多待办事项。单击删除待办事项按钮。只有被选中的待办事项才会被删除。

我非常新的反应,无法解决这个问题。在我的deleteToDo方法中,我试图过滤掉待办事项,只保留当前的待办事项。我不清楚我是否正确使用.filter。

2 个答案:

答案 0 :(得分:3)

问题是filter()方法应该返回一个条件,而不是值:

deleteTodo(id) {
      const remainingToDos = this.state.todos.filter((todo, remainingToDos) => {
          return (todo.id !== remainingToDos.id)
       });
      this.setState({ todos: remainingToDos });
}

答案 1 :(得分:0)

我很确定你的代码过于复杂。您没有使用已经传递给方法的参数id。您的deleteTodo方法可能只是:

deleteTodo = (id) => {
    var remainingToDos = this.state.todos.filter((todo) => {
        return todo.id === id
    });

    this.setState({ todos: remainingToDos })
}