Skip to content

Instantly share code, notes, and snippets.

@andrebautista
Created November 3, 2017 15:28
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 andrebautista/aedcec23c251f50e3cb71be150a69da8 to your computer and use it in GitHub Desktop.
Save andrebautista/aedcec23c251f50e3cb71be150a69da8 to your computer and use it in GitHub Desktop.
updating nested properties based on an action property
//reducer.js
initState = {
foo: {
value_a: "",
value_b: ""
},
bar: {
value_a: "",
value_b: ""
}
}
export default(state = initState, action) => {
switch (action.type) {
case UPDATE_VALUE:
return {...state, <WHAT TO DO HERE :(>}
}
}
//actions.js
//example call: updateValue('foo','value_a',"wohoo");
export const updateValue = (foo_or_bar, property_to_update, value) => {
type: "UPDATE_VALUE",
foo_or_bar, <which parent key to update>
property_to_update <updating value a or b>
value <value to assign to value_a or value_b>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment