7 ms·
When you refer to frontend state management, I'm assuming you're talking about libraries like Redux, and are using something like React. Basically, storing stat
by peterhunt 4y ago
When you refer to frontend state management, I'm assuming you're talking about libraries like Redux, and are using something like React. Basically, storing state that cuts across the component hierarchy.
First of all, I would suggest minimizing it as much as possible! You can often get away with a combination of component local state and data fetching libraries like SWR (the one I use) and react-query (I've heard it's good but haven't used). Modern data fetching libraries support intelligent caching, cache invalidation, and even optimistic updates.
The vast majority of internal tools at Twitter (prev employer where we built internal tools) didn't need any state management at all outside of swr and component local state.
However there will be some cases where some sort of client side state management will be useful. In these cases I reach for jotai. It was one of the first state management solutions designed post React Hooks, so it feels much more natural than state management solutions where hooks were bolted on. Additionally, it doesn't suffer from some of the rough edges that similar libraries like Recoil have (like naming fatigue from all those string keys).
Btw, I only use a tiny subset of jotai. I don't use any of the persistence or async stuff.
So tl;dr:
1. Build your app with component local state and SWR only.
2. If this gets messy, refactor your component local state and SWR code to be abstracted away as reusable functions / hooks.
3. Only when there is a performance problem or a really convoluted component hierarchy that doesn't make much logical sense, consider adopting jotai, but only for the parts of your app state that are causing the issues.