Zustand useStore currently resubscribe to the store on each re-render when used with useShallow #3303
-
|
While this is not a bug, this could and maybe should be avoided ? useStore uses React's useSyncExternalStore internally and React doc list in this hook caveats
As much as it's probably a very fast operation, why not avoid it all together by ensuring that useShallow returns a stable function ? The change would be to change to I'm happy to open a PR if this is something you agree should be handled at the library level. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Our subscribe function is stable regardless of useShallow. Line 31 in 39a391b A bit technical noteYes, the use of useCallback in useShallow technically optimizes it, but I don't think people use useCallback when they use useShallow, and we don't recommend it anyway as it's just micro optimization. If you need such micro optimization, I think avoiding useShallow would be better. |
Beta Was this translation helpful? Give feedback.
Our subscribe function is stable regardless of useShallow.
zustand/src/react.ts
Line 31 in 39a391b
A bit technical note
Yes, the use of useCallback in useShallow technically optimizes it, but I don't think people use useCallback when they use useShallow, and we don't recommend it anyway as it's just micro optimization. If you need such micro optimization, I think avoiding useShallow would be better.