Quantcast
Channel: User Brett Merrifield - Stack Overflow
Viewing all articles
Browse latest Browse all 37

Prepend to nameless array in Redux reducer

$
0
0

I get an array of objects from an API and store it in an articleData prop via Redux. The prop is stored like this:

articleData Prop

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:

enter image description here

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! :)


Viewing all articles
Browse latest Browse all 37

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>