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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With