I'm having trouble updating an object in my React / Redux reducer. The initial state is an object of Immutable. I'm stuck trying to update the object.
import Immutable from 'immutable';
import * as actionTypes from '../actions/actionTypes';
const initialState = Immutable.fromJS({
user: {
id: null,
name: null,
age: null
}
});
export default function addUserReducer(state = initialState, action) {
switch(action.type) {
case actionTypes.ADD_USER:
const user = {
id: action.id,
name: action.name,
age: action.age
}
return state.setIn(['user'], user);
default:
return state;
}
}
The state always returns a map with the values of id, name and age as null.
What's the correct way to update the state in my reducer?
actionhas not those properties. Did you tryconsole.log(action)and see what is inside? The correct way probably should be set a user to the action and the doaction.userstate.mergeDeep({user})orstate.setIn(['user'], fromJS(user))