admin管理员组

文章数量:1550528

react中警告提示:

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 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})
}

本文标签: 提示CallReactComponentsetState