JavaScript React mqtt subscription setState warning react mqtt subscribe

I'm using https://www.npmjs.com/package/mqtt with React. In my component I have:

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');
    },

So I subscribe to the topic when component will mount and unsubscribe when it unmounts. However, when i go to another view in my app and come back i get a warning with every mqtt message:

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.

What am i doing wrong?

Answer:1

In JavaScript, I am running a forEch method on an array of objects. Would like to run through my JSON and replace a substring in a string with a new string if the condition is met. The issue is this:...

In JavaScript, I am running a forEch method on an array of objects. Would like to run through my JSON and replace a substring in a string with a new string if the condition is met. The issue is this:...

I have a string like this : var name="C:\Users\Desktop\task.txt"; I want to get the last index of backslash in the above string. How to do it ?

I have a string like this : var name="C:\Users\Desktop\task.txt"; I want to get the last index of backslash in the above string. How to do it ?

I am using the Bubbles plugin with the Flot charting library for JQuery. The data I have is dynamic and can be quite varied within the X, Y, and Z values. The main issue I am having is the size of the ...

I am using the Bubbles plugin with the Flot charting library for JQuery. The data I have is dynamic and can be quite varied within the X, Y, and Z values. The main issue I am having is the size of the ...

  1. flow bubbles plugin - bubble size

const columns = ['dataA', 'dataD', 'dataB', 'dataE']; const data = [{ dataA: '001a', dataB: '123456', dataC: 'true', dataD: 'normal', dataE: 'ON' }, { dataA: '...

const columns = ['dataA', 'dataD', 'dataB', 'dataE']; const data = [{ dataA: '001a', dataB: '123456', dataC: 'true', dataD: 'normal', dataE: 'ON' }, { dataA: '...

  1. values_list json
  2. django values_list json
  3. python list json values
  4. array list json values
  5. convert list values to json