It is futile to do with more things that which can be done with fewer.
—Occam's razor
There's a visible rift between the approaches to local state from React's useState()
and shared state from the popular libs:
- Most libs bring quite an inventory of new entities:
- Redux Toolkit has store, slices, actions, reducers, selectors, and thunks;
- Jotai has atoms, stores, and own Provider;
- Zustand has stores, actions, selectors, and a hook factory;
- MobX has elaborate observables with a set of subtypes, actions, reactions, computeds;
- Most libs require substantial rewrites to move local state to shared state;
- Some libs also require additional workarounds for SSR.
Since shared state is a pretty common thing in web apps, it's reasonable to shave off additional scaffolding with Occam's razor as much as possible, for it can make coding more comfortable.
In my quest for a minimal approach to shared state I ended up creating Groundstate. It's similar to useState()
: it's focused on the single task of storing the state, flexible enough, and effortlessly compatible with SSR, while avoiding verbosity. Groundstate introduces just one new entity: store.
Top comments (0)