【发布时间】:2015-10-15 20:09:59
【问题描述】:
我将 https://www.npmjs.com/package/mqtt 与 React 一起使用。在我的组件中,我有:
componentDidMount:function(){
client.subscribe('test/topic');
client.on('message',function(topic,message){
if(topic==='test/topic'){
console.log(message.toString());
this.setState({value:parseInt(message.toString())});
}
}.bind(this));
},
componentWillUnmount:function(){
client.unsubscribe('test/topic');
},
所以我在组件挂载时订阅主题,在组件卸载时取消订阅。但是,当我转到我的应用程序中的另一个视图并返回时,每条 mqtt 消息都会收到警告:
Warning: setState(...): Can only update a mounted or mounting component.
This usually means you called setState() on an unmounted component.
This is a no-op.
我做错了什么?
【问题讨论】:
标签: javascript reactjs mqtt