react中警告提示:
react_devtools_backend.js:4061 Warning: Can't call setState on a component that is not yet mounted. This is a no-op, but it might indicate a bug in your application. Instead, assign to `this.state` directly or define a `state = {};` class property with the desired state in the Home component.
代码:
class App extends React.Component{
constructor(props) {
super(props);
...
this.state = {
name:"开发测试",
adopters:[]
}
this.init()
}
init(){
...
this.setState({adopters:result})
}
因为在constructor() 中初始化state使用了setState,调整如下即可:
class App extends React.Component{
constructor(props) {
super(props);
...
this.state = {
name:"开发测试",
adopters:[]
}
}
componentDidMount(){
this.init()
}
init(){
...
this.setState({adopters:result})
}