2015-10-15 54 views
2

我使用https://www.npmjs.com/package/mqtt與React。在我的部分,我有:反應mqtt訂閱setState警告

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. 

我到底做錯了什麼?

回答

0

當聲明didMount方法時,會調用回調函數。將回調移到它自己的方法中。你也不需要綁定this

+0

沒有幫助。我將所有的類重構爲ES6,將回調函數轉換爲它自己的函數,並且仍然收到此警告。 – Ivan