Ad

How Do I Add An Item To The Front Of A State Array In React

- 1 answer

I know that you can add items to the end of the array with concat but how do I unshift to add an item to the front?

Add to the end:

var allStatuses = this.state.statusData;
var newStatuses = allStatuses.concat([data.statuses]);
this.setState({statusData: newStatuses});

What I want to do is change the "concat" to something that will put data.statuses to the front and not the end.

Ad

Answer

Actually you can use .concat in this case,

var newStatuses = [data.statuses].concat(allStatuses);
Ad
source: stackoverflow.com
Ad