Removing element from array in component state

前端 未结 10 2059
感动是毒
感动是毒 2020-11-28 01:31

I am trying to find the best way to remove an element from an array in the state of a component. Since I should not modify the this.state variable directly, is

10条回答
  •  盖世英雄少女心
    2020-11-28 02:07

    The cleanest way to do this that I've seen is with filter:

    removeItem(index) {
      this.setState({
        data: this.state.data.filter((_, i) => i !== index)
      });
    }
    

提交回复
热议问题