Brent Phillips
Brent Phillips

Reputation: 27

React Todo Delete Button Removes all listed items at once

I have 2 files

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;

What I am trying to accomplish: Add many todos to the list. Click on the remove todo button. ONLY the todo that is selected will be removed.

I am VERY new to react and cannot figure this out. In my deleteToDo method I am trying to filter out the todos and only keep the todos that are current. I am unclear if I am using .filter properly or not.

Upvotes: 0

Views: 1347

Answers (2)

Emad Emami
Emad Emami

Reputation: 6639

Problem is that filter() method should return a condition, not value:

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

Upvotes: 3

Ryan Turnbull
Ryan Turnbull

Reputation: 3944

I am fairly sure you have simply over-complicated your code. You are not using the parameter id that you have passed into your method at all. Your deleteTodo method could simply be:

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

    this.setState({ todos: remainingToDos })
}

Upvotes: 0

Related Questions