3 ms·
I don't understand why state management is filled with so much weird terminology. When I started working on a React app years ago every state management package
by Rohansi 2y ago
I don't understand why state management is filled with so much weird terminology. When I started working on a React app years ago every state management package I saw was filled with it - and they still are.
I ended up rolling my own solution [1] to escape all that so I could focus on getting things done. It's been working great for me in production. Too bad there's no popular option that works similarly.
[1] https://github.com/facepunch/react-class-model https://github.com/facepunch/react-class-model
- Izkata 2y agoLooking at how CounterDisplay and CounterControls work at the bottom of the README, the idea behind this is similar to Valtio (direct updates and changes are pushed on update), except with a lot more manual work. Valtio uses proxies and hooks to get the effect of CounterModel, CounterModelProvider, and useCounterModel automatically, without having to write them yourself. For comparison, Valtio has the same kind of "counter" example at the top of their README (first and third code blocks): https://github.com/pmndrs/valtio?tab=readme-ov-file#wrap-your-state-object https://github.com/pmndrs/valtio?tab=readme-ov-file#wrap-you...
- Rohansi 2y agoThat looks pretty similar but I like the use of classes in react-class-model to encourage keeping logic out of components. It's not shown in the readme but using context is optional - you can similarly achieve global state by storing it in a variable and then using `watchCounterModel` (the third return value from `defineModel`) to subscribe to updates on it. Both hooks also return proxies to re-render only when the referenced fields change.