zustand
The client global store library: https://github.com/pmndrs/zustand
We use Zustand as a global store for sharing data between components, both in an outside of React. This serves the same purpose as things like Redux, for having reactive global state that lets components communicate outside of direct props and callbacks.
Stores
To declare a store, you call create
with a store type, which returns a hook that lets you access the store data. The hook also includes methods for getting and updating data from the store outside of react.
Exporting helpers
The syntax for getting a value out of a store can be a little verbose, since you have to pass a selector function to the store hook. Instead, it is good practice to not export the store itself from a file, and instead export specific hooks data out of the store.
Also, Zustand lets you either declare setters as methods actions in the store, or exported functions. Their docs say that either is fine, and in practice exported functions seem easier to work with, as they don't require an extra hook to generate them, and reduce duplication in the store interface.
Finally, for shared logic for returning a calculated value of the store, exporting a selector wrapped in a hook makes writing components, and sharing logic between them, easier.
However in tests, it is sometimes useful to have direct access to the store to set up test cases. So you can export the store itself, but leave a doc comment reminding devs not to directly import the store in implementation code.
Instead of
you can instead export helpers, which both make components easier to write, and make it clear which parts of the store are publicly available
simpleStore
If you have a single piece of state you are storing with a useState
, and either want to share it between multiple components, or cache its value as components are created and destroyed, simpleStore
(packages/shared/store/simpleStore.ts) may be helpful.
To use it, replace a local useState
with a global simpleStore
Now, the count
state will persist after MyComponent
is destroyed, and can be shared with multiple components.
Last updated