【发布时间】:2019-08-16 20:28:51
【问题描述】:
我想知道,在 ReactJS 中设置对象状态的正确方法是什么。
请找到我的示例代码:
class MyComponent extends Component {
constructor(props) {
this.updateName = this.updateName.bind(this);
this.state = {
name: "Sample code"
};
}
updateName() {
this.setState(name: "Updated");
}
componentDidMount() {
this.updateName();
}
render() {
return (
<div>
<label>{this.state.name}</label>
</div>
);
}
}
这是我更新名称的示例代码。在这里,使用setState 更新名称并导致输出“更新”。
如果updateName()被改写为,
updateName(){
this.state.name = 'Updated';
}
然后打印名称,它会给出相同的结果。 我想知道,在 ReactJS 中设置对象状态的正确方法
【问题讨论】:
标签: reactjs