How do I edit a todo in React

前端 未结 1 1849
慢半拍i
慢半拍i 2021-01-23 05:46

my question is that when i click on edit icon and edit my todo update it. Then that edited todo display new todo in todo-list.New todo and old todo(which i had edited) both disp

相关标签:
1条回答
  • 2021-01-23 06:28

    The problem is in your setState, you are actually adding a todo rather than replacing it

    Do it like

    var todos = [...this.state.todos];
       var index = todos.findIndex((todo) => todo.todo_id == todo_id)
       todos[index].todo_text = newValue
       todos[index].todo_start_at = todoStartDate
       todos[index].todo_end_at=todoEndDate
       this.setState({
                    todos
                 });
    
    0 讨论(0)
提交回复
热议问题