Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unshift data to array in Redux reducer

On the dispatch of the UPDATE_DATA action, I am able to push data to my state.data array in the reducer with the following code.

const toPush = {
    name : "Pushed Name",
    id_name : 100,
    more1 : "pushedMore01"
}

case "UPDATE_DATA":
  return {
    ...state,
    data: [...state.data, toPush],
    isFetching: false
}

How do I unshift rather than push the data to state? What would be clean ES6 syntax for the same?

like image 206
Somename Avatar asked Dec 06 '22 13:12

Somename


1 Answers

Just switch the order:

data: [toPush, ...state.data]

This will insert the new item at the beginning, then spread the rest of the previous data after it.

like image 149
Andrew Li Avatar answered Dec 08 '22 03:12

Andrew Li