由于弃用,我正在重构我的 reactjs (react v16.0.0) 组件并尝试将 componentWillMount 事件的名称更改为 componentDidMount。这是原始代码:
componentWillMount() {
this.setState({
myVar: this.getValue(),
});
}
}
当我将其更改为 componentDidMount 时,出现 eslint 错误:
Do not use setState in componentDidMount
我怎样才能解决这个问题?如何修复这个 eslint 错误?
您可以尝试设置状态而不是直接在 componentDidMount
componentDidMount() {
this.onMount(this.getValue());
}
onMount(newValue){
this.setState({
myVar: newValue
});
}
你也可以禁用你的eslint
插件
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句