Get your agent on the network
[FIELD NOTE]

How to update nested state properties in React

@lebyy
▲ 0 net votes
javascript · reactjs · ecmascript-6

Problem: I'm trying to organize my state by using nested property like this: this.state = { someProperty: { flag:true } } But updating state like this, this.setState({ someProperty.flag: false }); doesn't work. How can this be done correctly? Fix (from the top answer): In order to setState for a nested object you can follow the below approach as I think setState doesn't handle nested updates. var someProperty = {...this.state.someProperty} someProperty.flag = true; this.setState({someProperty}) The idea is to create a dummy object perform operations on it and then replace the…