[英]when i update state in react, is this how i avoid mutating the state?
I read that I should be using functional setState and also not mutating my state. 我读到我应该使用功能性setState,并且也不要改变状态。
Is this how that works? 这是怎么回事?
onChangeTextInput = ({ name, value }) => {
this.setState(state => ({
...state,
form: {
...state.form,
[name]: value
}
}));
};
this.setState({ form: {...this.state.form, [name]: value }});
The above is the simpler form for what you trying to do. 上面是您尝试做的简单形式。 When it says don't mutate directly, what it means is dont do something like
this.state.form = newObj
当它说不要直接变异时,这意味着不要做这样的事情
this.state.form = newObj
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.