3 ms·
I'd like to share my approach to React state management, because after a decent amount of industry experience I've stumbled onto a solution I find to be very ex
by windowshopping 4y ago
I'd like to share my approach to React state management, because after a decent amount of industry experience I've stumbled onto a solution I find to be very excellent.
Firstly, I use the open source library Pullstate[1], which I find to be as effective as any alternative but _far, far_ simpler to understand and use.
All my components are functional components. For any state that _only_ that component needs, of course I simply use the useState hook.
When things need to be shared among multiple components, I create a Pullstate store - for instance, for an app I'm working on now I have a UiStateStore like so:
type UiStateStore = {
isSidebarOpen: boolean;
// etc
};
export const uiStateStore = new Store<UiStateStore>({
isSidebarOpen: false,
});
Then, in the components that need to know if the sidebar is open, I import this store and use its state hook _the exact same as the normal useState hook_, like so:
const isSidebarOpen = uiStateStore.useState(s => s.isSidebarOpen);
You simply pick which properties you need off the state object ("s"). When you open the sidebar, you simply update the store like so:
onClick={() => { uiStateStore.update(s => { s.isSidebarOpen = true; }) }}
And automatically, any components using uiStateStore.useState and watching the isSidebarOpen property will get updated, exactly the same as the normal useState hook - just shared.
It's so dead simple and has made complex app-building so much easier for me.
By comparison, Redux in my experience has a harder learning curve with a lot of unnecessary pieces and boilerplate - it seems crazy to me that people think it's a great solution and it feels like people have convinced themselves all those pieces are "necessary", where in my experience they are anything but.
The one caveat is that if I have a component with many handlers, e.g. onClick, onMouseMove, onContextMenu, onMouseLeave, etc (and in some cases I do), components can get bloated. I haven't found a fix to that yet. But that's more an inherent issue with react than anything to do with state management.
[1] https://github.com/lostpebble/pullstate https://github.com/lostpebble/pullstate
- etimberg 4y agoIn some ways this feels a lot like http://alt.js.org/ http://alt.js.org/
- have_faith 4y agoYou might like the atomic state management libraries mentioned in the article, they simplify things to another level because they sort of remove the concept of a store entirely. For example in Recoil I would do this: export const isSidebarOpenAtom = atom({key: 'isSidebarOpen', default: false}); And in the component: (it mimics useState) const [isSidebarOpen, setIsSidebarOpen] = useRecoilState(isSidebarOpenAtom); and later... setIsSidebarOpen(true) Recoil will then re-render any component that relies on isSidebarOpen. The only real boilerplate is having to specify keys for each atom, which I find a small price to pay for such simple global state.