Hi Everyone !
The big picture of what I’m trying to achieve :
I’m developing a Dash application to control some output of a Raspberry-pi. The issue with Dash is that it is a stateless app therefore each user only see and interact with it’s own state and not the state of the Raspberry pi output. Let’s take an example :
My dashboard show a slider that will control a fan speed on the raspberry pi. If multiple users are connected to the Rpi, when one will change the slider, this change won’t be reflected to other users session. So there will be synchronisation issue between each user session, and the actual value that the Rpi is outputting for the fan speed.
I could use some workaround using websocket and polling the actual state of the Rpi, but my project is much more complicated than this example and I need a more general solution.
The solution I’am aiming for :
Having a custom component called a Synchronizer. It take as input a list of children like any bootstrap component for example, and a list of props to synchronize for all those children. Under the hood, the Synchronizer component will listen to a state change for the desired props among his children, when a change happen it will send a message using WebSocket to a Quart server running on a specific url (that could also be an input of the Synchronizer). The Quart server will reply to the same url and send the id and props value that have been updated to all users, updates that will be applied via the Synchronizer.
Tell me if i’m not clear here, I very new to react and I’ve tendency to confuse props and state (the value of a slider in dash, is it a prop or a state in the react slider component ?)
Solution that already exist :
A fork of dash was developed called dash_devices, it’s looking very good but being a fork of the main dash project, I’m not sure how compatible is it with recent version of dash, how it will be maintained ect… also it is changing a lot of the core aspect of dash…
There is also the WebSocket component on the dash-extension, which I’m using as source of inspiration for my component.
Aware of an other already existing solution ? let me know.
The issues I’m facing :
I’ve developed a lot in python, C#, C++… but when I started to look at React… I got very confused, even though I did a bit of javascript back in the time. So far I managed to build and run the dummy boiler-plate provided by dash, I did tinker it a bit but I’m having trouble to simply render the children component that have been passed to my Synchronizer component…
I’ve done this post to have your opinions regarding the solution I’m aiming for, does it sound reasonable for a React beginner ? Does the idea even make sense ?
If I manage to go through this, the code will be available on github as I think I’m not the only one that need such functionality.
In the meantime :
I’m stuck on this code that should be simple :
constructor(props) {
super(props);
this.ChildRef = React.createRef();
}
componentDidUpdate(prevProps) { /*Fetch the state that was changed in the children and send a WebSocket msg.
TODO LATER*/
}
render() {
const children = this.props.children;
return (
//This is Obviously not working...
children.forEach(Child => {
return <Child ref={this.ChildRef}/>
});
);
}
//The PropTypes I'm using for the children prop
children: PropTypes.node,
For each children passed via dash to the component, I want to render the children and use the React.CreateRef function has described here to be able to fetch the component state in the componentDidUpdate function.
Big post, thank you for your time If you went all the way