Skip to content
Advertisement

Dynamic change of global variables in react native

I have an mqtt client inside my react native app and it constently receives data from a broker. This data is being used in many different screens/components differently (I have 1 class which extends React.Component per screen). For example I show the data I receive from topic x on screen 1 while I show the data I receive from topic y and also topic x on screen 2.

The way I am storing this data is with redux. So I can reach out the latest data with using e.g. this.props.dataX. I am not sure if this is the best way to store it. What I want to achieve is to show dynamic lists for this data, e.g. one flatlist for the data received on one topic on first screen and another flatlist for topic2+topic1-screen2.

If I simply send the data to a flatlist, it doesn’t become dynamic. I had to refresh the page to render the new data:

JavaScript
JavaScript

I know that if this data is in the state of the component then it would be dynamic. Therefore eventhough it doesn’t really make sense, I tried to duplicate it in the state with the hope that it would be dynamic. But it didn’t:

JavaScript

So how can I achieve this?

Advertisement

Answer

To continue my comment: This is a full example of how i’m doing.

I’m using the mqttws31.js library to connect to mosquito. If you need it, just tell me.

I have the app.js that load the redux store and the app in the App.jsx.

App.jsx handle my wsClient functions but you can move them to an other file.

This is the Redux Connect function and mapstateToProps that re-render my component. If props change, the compoenent render again

store.js:

JavaScript

App.js:

JavaScript

App.jsx:

JavaScript

And in my case, DimmableLight.jsx:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement