aherriot aherriot - 1 month ago 10
React JSX Question

React: removing element from array in component state

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

variable directly, is there a better way (more concise) to remove an element from an array than what I have here?:

onRemovePerson: function(index) {
var newData = this.state.data.slice(); //copy array
newData.splice(index, 1); //remove element
this.setState({data: newData}); //update state

Thank you.


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.

  data: update(this.state.data, {$splice: [[index, 1]]})