Learning how to use Jotai state management, I'm facing an issue regarding the propagation of atoms through part of the component tree.
Let's imagine I have this component tree (each node is a React component):
Declaring "global" atoms (like current logged user, selected theme, ...) is straightforward. I can write in a module these atoms and provide them directly to the use useAtom
hook.
However, I have some large part of the application that can share local data.
For example, I need to load data for each customer, and make this data available in the component sub tree of every customer component. Obviously, this data is different for each customer tree.
To make this data available, I can see several solutions:
useAtom
hook by passing the atomFamily and customer ID parameterI also was wondering if there's something I can do by playing with local Provider
(inserting a Provider
component around the Customer
component), but this way, as far as I know, I will "loose" my global atoms, or to be precise, each global atoms will hold different values inside every Provider.
To conclude, the question is: what is the recommended way to deal with atom propagation when value is shared only in part of the component tree?
Jotai Scopes explicitly deals with this use case, allowing multiple stores that can override the parent store's atoms.
Another choice is to simply use a regular React Context provider to pass down a component-created atom (in case you want contextual state to also be mutable without inefficient re-renders).
const AtomContext = createContext(undefined);
function MyScopedAtom(props) {
const someAtom = useMemo(() => atom(0), []);
return <AtomContext.Provider value={someAtom} {...props} />;
}
function MyComponent() {
const [value, set] = useAtom(useContext(AtomContext));
...
}