I'm trying to update the property 'name' of my object person using setState. But it doesn't seem to work. I'm getting the 'newName' through user input.
What am I missing here?
this.setState({ person: { name: newName } });
I'm trying to update the property 'name' of my object person using setState. But it doesn't seem to work. I'm getting the 'newName' through user input.
What am I missing here?
this.setState({ person: { name: newName } });
class TestJS extends React.Component {
constructor(props) {
super(props);
this.state = {
person : {name : "DefaultName", age : 56}
}
}
componentDidMount(){
this.setState(state => ({ person: Object.assign({}, state.person, { name: "sdsds" }) }));
}
render() {
return(
<div id="root">
{this.state.person.name}
{this.state.person.age}
<p>Hello world</p>
</div>
);
}
}
export default TestJS;