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
onRemovePerson: function(index) {
var newData = this.state.data.slice(); //copy array
newData.splice(index, 1); //remove element
this.setState({data: newData}); //update state
},
You could use the update()
immutability helper from React.addons
, which effectively does the same thing under the hood, but what you're doing is fine.
this.setState({
data: update(this.state.data, {$splice: [[index, 1]]})
})