error error - 26 days ago 8
Javascript Question

How to destruct in a correct way?

I'm wondering how to return state with fetched array in a correct way using spread operator.

So here is my reducer:

function themes(state = [], actions){
switch(actions.type){
case FETCH_THEMES_SUCCESSFULLY:
const { themes } = actions.theme;
return {
...state,
...themes
}
default :
return state;
}
};


actions contains
array
called
themes
. But when
themes
is fetched my
props
looks like :

{ themes: { themes: [...]} }
instead of
{ themes: [] }


What am i doing wrong?

Answer

actions.theme is already your […]; you don't need to destructure it any further. Replace const { themes } = actions.theme with const themes = actions.theme should get you the shape you're looking for.

Alternately, const { themes } = actions; would also work. But not both destructuring and accessing actions.theme.