-
Hi ! import fastDeepEquals from 'fast-deep-equal';
import { Atom, useAtom, useAtomValue } from 'jotai';
import { selectAtom } from 'jotai/utils';
// `useIMemo` ignores eslint dependency error.
import { useIMemo } from '@utils/hooks/useIMemo';
export type DynamicAtomSelector<T, R = unknown> = (state: T) => R;
export const useDynamicAtomValue = <T, R = unknown>(
atom: Atom<T>,
selector: DynamicAtomSelector<T, R>,
deepEquals?: boolean
) => {
const dynamicAtom = useIMemo(
() => selectAtom(atom, selector, deepEquals ? fastDeepEquals : undefined),
[deepEquals]
);
return useAtomValue(dynamicAtom);
}; With this hooks, We can select property dynamically. |
Beta Was this translation helpful? Give feedback.
Answered by
dai-shi
Jun 10, 2024
Replies: 1 comment
-
This is an open space of third-party libraries. Jotai library only provides primitives. For the implementation, I would add |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
gaki2
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an open space of third-party libraries. Jotai library only provides primitives.
For the implementation, I would add
atom
(and preferablyselector
) in the dependency array. But, it's up to the design decision.