Skip to content

Instantly share code, notes, and snippets.

@brettinternet
Last active January 24, 2017 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettinternet/52919efb207ed43df4d8c5b65b3d47fe to your computer and use it in GitHub Desktop.
Save brettinternet/52919efb207ed43df4d8c5b65b3d47fe to your computer and use it in GitHub Desktop.
For returning new state in redux with nested objects from a reducer's initialState
let state = {
nestedObj: {
x: 1,
y: 2,
},
h: 10
}
let addObj = {
z: 3
}
fn = () => {
return Object.assign( {}, state, { nestedObj : Object.assign( {}, state.nestedObj, addObj ) } );
}
fn();
@brettinternet
Copy link
Author

A better way to handle this however is to separate the parts of state that you want to renew in different reducers. Then make state default to that specific portion of state with ( state = intialState.nestedObj , action )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment