Remove item from array in React

后端 未结 8 820
醉酒成梦
醉酒成梦 2021-01-03 12:45

I have problem with removeItem function (it should remove current

  • that button is nested in, and item from array on this.state.list), no code currentl
  • 8条回答
    •  小蘑菇
      小蘑菇 (楼主)
      2021-01-03 13:36

        _deleteTodo(index) {
          console.log("delete " + index);
          this.state.todos.splice(index, 1);
          this.setState({
            todos: this.state.todos.filter(i => i !== index)
          });
        }
      

      I had a problem with splice and i honestly don know why. However this method work for me and you can try it! Ps. If anybody know why splice is not working with state and index please let me know i am curious!

    提交回复
    热议问题