I get an array of objects from an API and store it in an articleData
prop via Redux. The prop is stored like this:
In my reducer I want to prepend to this array with another articleData
object. The object I want to prepend is stored in action.articleData
but I can't find a way to prepend this array without naming it. Here is my reducer code:
export function articleData(state = {}, action) { switch (action.type) { case 'ARTICLE_FETCH_DATA_SUCCESS': return action.articleData; //HERE IS THE PROBLEM! case 'ARTICLE_POST_NEW_ARTICLE_SUCCESS': return {arr:[action.articleData, ...state]} default: return state; }}
It prepends the new object successfully. Problem is: When I execute this code it changes the state from an array of objects to an array named "arr". See picture:
I can't figure out how to just add to the array of objects without naming it something. If I remove the arr:
from the reducer code it results in a syntax error.
Thanks in advance to anyone willing to help me out! :)