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

文章链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: