How do I add an item to the front of a state array in React

If you want to modify the original array, use Array.unshift

var arr = [1,2,3];
arr.unshift(0);
arr // 0,1,2,3

If you want a new array, use Array.concat as Alexander suggested

this.setState({statusData: [0].concat(this.state.statusData)})

Which is similar to spreading into a new array

this.setState({statusData: [0, ...this.state.statusData]})

Actually you can use .concat in this case,

var newStatuses = [data.statuses].concat(allStatuses);

Is there a problem doing

[data.statuses].concat(allStatuses);

If you are using ES6, you can do

var newStatuses = [data.statuses, ...allStatuses]

Tags:

Reactjs