Question about useReducerAtom and derived atoms #2672
-
Hi All - i'm starting out with Jotai and had a question around re-renders and usage of useReducerAtom in this contrived example https://codesandbox.io/p/sandbox/inspiring-haze-37n98f I would have expected the components to only re-render when their state changed but doesnt look like thats the case. The atoms are derived from state slices and the overall state is updated by the dispatcher |
Beta Was this translation helpful? Give feedback.
Answered by
rothsandro
Jul 26, 2024
Replies: 1 comment 1 reply
-
Your - const [, setState] = useAtom(anAtom);
+ const setState = useSetAtom(anAtom); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
mo-lon
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your
useReducerAtom
hook reads the whole atom state, that's why the component re-render. Replace it withuseSetAtom
which doesn't return the atom value: