请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

Warning: Can't call setState on a component that is not yet mounted.

啊老师您好,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.state directly or define a state = {}; class property with the desired state in the HeaderComponent component.

请问要怎么处理呢?

我把 store.subscribe(this.handleStoreChange);提出来放到 componentDidMount() 里面,警告就消失了,请问这样是否规范呢?

	componentDidMount() {
		store.subscribe(this.handleStoreChange);
	}

正在回答 回答被采纳积分+3

1回答

阿莱克斯刘 2021-09-07 03:46:20

你的改动合乎规范,而课程为了讲解方便反而不符合规范。不过,也不用太在意,因为在真实项目中,我们一般都不会直接使用订阅模式,而是通过react-redux来订阅store。

1 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号