Reputation: 4053
I am trying to update my state using redux, but for some reason the reducer only gets a string instead of an object.
// action
const switchTab = (activeTab) => { return { type: SWITCH_TAB, activeTab } }
//reducer
const appUIManagement = (state = initialState, action) => {
switch(action.type) {
case SWITCH_TAB:
return Object.assign({}, state, {activeTab: action.activeTab});
default:
return state;
}
}
Assuming my initial state is
{activeTab: 'lines'}
Now when I dispatch an action:
{ type:SWITCH_TAB, { activeTab: 'favorites' } },
the state parameter of my reducer is only 'lines' instead of the whole object and because of this my response looks something like
{
activeTab: {
'0': 'l',
'1': 'i',
'2': 'n',
'3': 'e',
'4': 's',
activeTab: 'favorites'
}
}
What am I doing wrong or what is it that I'm not getting about redux? Thank you
Upvotes: 2
Views: 905
Reputation: 35481
You are passing the string action.activeTab
into Object.assign
const state = {};
const action = { type: 'SWITCH_TAB', activeTab: 'lines' };
const newState = Object.assign({}, state, action.activeTab);
console.log(newState);
However, Object.assign
expects objects as its arguments so you should pass the whole action or better yet the portion of the action that you need:
const state = {};
const action = { type: 'SWITCH_TAB', activeTab: 'lines' };
const newState = Object.assign({}, state, { activeTab: action.activeTab })
console.log(newState);
Upvotes: 2