1/4
Easy#refactorreacthooksperformance

Don't memoize getServerSnapshot in useStore

pmndrs/zustand#3563 · @dai-shi, @MarshallOfSound · MIT · licence

description
React only calls getServerSnapshot during hydration/server rendering and never retains or compares its identity (checked across 18.0-19.2, Fizz, the shim, preact/compat and RN), so the useCallback around it was pure per-render overhead: one hook slot, a deps array and a closure. Dropping it saves ~13% of useStore's per-render allocation and a few bytes. ## Related Bug Reports or Discussions https://github.com/pmndrs/zustand/discussions/3562
1 commit · 1 file
  • 685c5a3perf(react): don't memoize getServerSnapshot in useStore

// files changed

Msrc/react.ts+1−1
@@ -30,7 +30,7 @@ export function useStore<TState, StateSlice>(
3030 const slice = React.useSyncExternalStore(
3131 api.subscribe,
3232 React.useCallback(() => selector(api.getState()), [api, selector]),
33 React.useCallback(() => selector(api.getInitialState()), [api, selector]),
33 () => selector(api.getInitialState()),
3434 )
3535 React.useDebugValue(slice)
3636 return slice