啊老师您好,Header.class.tsx 里类 HeaderComponent 的构造函数调用了 store.subscribe(this.handleStoreChange);,而handleStoreChange 函数里又调用了 setState:
this.setState({
language: storeState.language,
languageList: storeState.languageList,
});
这会在浏览器开发者工具的 console 里面引起警告:
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.statedirectly or define astate = {};class property with the desired state in the HeaderComponent component.
请问要怎么处理呢?
我把 store.subscribe(this.handleStoreChange);提出来放到 componentDidMount() 里面,警告就消失了,请问这样是否规范呢?
componentDidMount() {
store.subscribe(this.handleStoreChange);
}
React 18 精讲 + 结合 TS 实战 + 热门业务开发,获取必备技能
了解课程