Removing element from array in component state

前端 未结 10 2034
感动是毒
感动是毒 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:01

    Here is a way to remove the element from the array in the state using ES6 spread syntax.

    onRemovePerson: (index) => {
      const data = this.state.data;
      this.setState({ 
        data: [...data.slice(0,index), ...data.slice(index+1)]
      });
    }
    

提交回复
热议问题