packages/ra-core/src/store/README.md
The Store is react-admin's global, synchronous store. We use it to store state shared between several componenents and/or state that must be persisted across page reloads (e.g. user preferences).
Here are a few examples of elements stored in the store:
The store uses an adapter pattern to allow developers to store the state in memory, in localStorage, and to synchronize it with an API.
Stores rely on React state and update events that broadcast a change in the state to all components subscribed to that state.
The Store API is modeled after the useState API.
const [value, setValue] = useStore(key, defaultValue);
We could use React-query to store this data, but that would create useless calls to the dataProvider because react-query is asynchronous.
Here is an example scenario demonstrating the issue:
With a synchronous store, this doesn't happen:
React-admin v1, v2 and v3 used Redux to store the global state. This had major shortcomings:
useSelector function is interesting to grab branches of a tree state, but it's overkill when we only need a key/value store. All our needs for a store live perfectly in a key/value storeThese libraries require additional knowledge (e.g. atoms, setter functions, etc.) and a bit of tweaking to support localStorage. Although they are a better fit than Redux for react-admin's needs, they are still overkill and heavier than what we really need.