在 React 中处理组件状态的正确方法是什么?
What is the proper way to handle component's state in React?
我见过有人使用 setState
方式:
this.setState( {enteredlocation: newLocation});
目前我正在这样写状态:
this.state.id.push({"no" : length , "location": this.state.enteredlocation});
更新状态的正确方法是什么?
我稍后也会集成 Redux,但现在,我正在尝试一个部分地理解。
而不是这个:
this.state.id.push({"no" : length , "location": this.state.enteredlocation});
...做:
this.setState({
id: this.state.id.concat({
"no": length,
"location": this.state.enteredlocation
})
});
这将确保 id
是一个新的数组引用,而不是具有不同内容的相同引用。
我见过有人使用 setState
方式:
this.setState( {enteredlocation: newLocation});
目前我正在这样写状态:
this.state.id.push({"no" : length , "location": this.state.enteredlocation});
更新状态的正确方法是什么?
我稍后也会集成 Redux,但现在,我正在尝试一个部分地理解。
而不是这个:
this.state.id.push({"no" : length , "location": this.state.enteredlocation});
...做:
this.setState({
id: this.state.id.concat({
"no": length,
"location": this.state.enteredlocation
})
});
这将确保 id
是一个新的数组引用,而不是具有不同内容的相同引用。