I want to set state a nested object and I tried this answer, but I don't know everytime I setState
following the answer, the state still not set
Here's my code I've tried:
// declare the state
constructor(props){
super(props);
this.state = {
bius: {name:'bius', time:0, status:false},
}
}
// Function to setstate
_timer(name){
// this.setState({[name]: !this.state.name})
var upd = {...this.state[name]}
upd.status = !upd.status
console.log(upd.status)
console.log(this.state.bius)
this.setState({upd})
console.log("setstate upd")
console.log(upd)
console.log(this.state.bius)
}
// here I call the function to set state
<TouchableOpacity onPress={()=>{this._timer('bius')}}>
<Text style={[global.global.SubHeaderText, {color:'white'}]}>Start</Text>
</TouchableOpacity>
and here is output of log :
I/ReactNativeJS(6694): true
I/ReactNativeJS(6694): { name: 'bius', time: 0, status: false }
I/ReactNativeJS(6694): setstate upd
I/ReactNativeJS(6694): { name: 'bius', time: 0, status: true }
I/ReactNativeJS(6694): { name: 'bius', time: 0, status: false }
I don't know why the upd.status
already true
, but the this.state.bius.status
still false
?
why this.state.bius.status
not set to true
?