I know that similar questions were asked before, but what if i don't want to set the entire state, only one of its properties, to a variable? Something like this:
var initialProperty = {
name: '',
id: 0,
type: ''
}
class Example extends React.Component{
constructor(props){
super(props);
this.state = {
otherProperty: '',
targetProperty: initialProperty
//at the start of the Component, everything works fine.
//the targetproperty is set to initialproperty
}
}
//by the time, the targetproperty was changed from the initial in the component
//but if i click a button, i want to set targetproperty to the initialproperty
somethingHappensOnClick = () => {
this.setState({targetProperty: initialProperty})
//unfortunately, the targetproperty wasn't set to the initial.
}
}
Am I doing something wrong? Why targetProperty
doesn't change?