Created
November 3, 2017 15:28
-
-
Save andrebautista/aedcec23c251f50e3cb71be150a69da8 to your computer and use it in GitHub Desktop.
updating nested properties based on an action property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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