I have a state object that has a few nested objects. I'd like to update those nested objects in a dynamic way so i'm not copying and pasting the same logic with slight tweeks in different cases. Here's an example of a case in my reducer.
case UPDATE_MESSAGE_FORM:{
const {id, data} = action.payload
const key = id.toLowerCase()
return {
...state,
form: {
...state.form
[key]: {
// this must be incorrect or not possible
...state.form.key,
message: data
}
}
}
}
Currently that works and I get the message to store in the state object, but if I run that again with another key or even in a different case that is supposed to update another field in that nested object it returns the object with just that field. Can you dynamically spread with a nested object?