I have a nested object as a state and I have a form in a component. I was thinking of updating the state every time the user enter something in the form and to avoid creating ma
Of course you can use switch
, Nothing wrong AFAIK.
And to update nested objects with setState
. See the example
updatePreferences = (preferenceName, enteredValue) => {
switch (preferenceName) {
case preferenceName === "minutes":
this.setState({minutes: enteredValue});
return
case preferenceName === "business":
this.setState({...this.state, interests: {
...this.state.interests,
business: !this.state.interests.business
}});
return
default:
return
}
}