Remove item from array in React

后端 未结 8 817
醉酒成梦
醉酒成梦 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:35
      removeItem(item) {
          const item = getItem(this.state.list, item.id) // Method to get item in list through comparison (IE: find some item with item.id), it has to return ITEM and INDEX in array
          const newlist = [].concat(list) // Clone array with concat or slice(0)
          newlist.splice(item.index, 1);
          this.setState({list: newlist});       
        }
      
      0 讨论(0)
    • 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!

      0 讨论(0)
    提交回复
    热议问题