Removing element from array in component state

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

    Here is a simple way to do it:

    removeFunction(key){
      const data = {...this.state.data}; //Duplicate state.
      delete data[key];                  //remove Item form stateCopy.
      this.setState({data});             //Set state as the modify one.
    }
    

    Hope it Helps!!!

提交回复
热议问题