Zedux-like selectors #2596
-
Our application uses a quasi-normalized store to edit a tree structure, similar to below:
We don't like passing atoms into react components as props, so we like the zedux concept of selectors, and in particular, selectors that take parameters (in our case, the parameter passed to the selector would normally be the component id). In Zedux, this looks like the following - useAtomSelector, in particular the example
In our case, we would have selectors such as getChildrenById to get the child ids of a certain parent and getPropertiesById to get the properties of a certain component based on its id. Note that in both these cases, the "thing" returned is itself editable - so the process of adding a new component involves adding the component with a new unique id to the main list, and adding a new element to its parents "children" array. Similarly, deleting a component involves removing it from the main list and removing the id of that deleted element from its parents "children" array. I can't seem to work out how to structure jotai to achieve the same or similar to Zedux's parameterized selector and useAtomSelector. Any help would be greatly appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
We used to have Here's what I would do: const useAtomSelector = (selector, param) => useAtomValue(
useMemo(() => atom((get) => selector(get, param)), [selector, param])
) |
Beta Was this translation helpful? Give feedback.
We used to have
useSelector
, butselectAtom
is more capable, so unified.However,
selectAtom
isn't recommended except for escape hatches, and what's recommended is derived atoms.Here's what I would do: