react state初始化问题
报错
1 | 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 *** component. |
大致:
不能再一个组件尚未mounted时调用 setState() 方法,这是一个空操作,但是可能会导致bug,所以, 直接给this.state
赋值,或者定义成state = {};
问题代码
1 | import React from 'react'; |
render() 方法中如果使用了 this.state.docDetail,会导致编译报错
仔细检查代码
1 | constructor(props) { |
constructor 里使用了 setState,改为直接赋值后正常