Skip to content

Commit dac22e1

Browse files
perf(react): don't memoize getServerSnapshot in useStore (#3563)
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. Co-authored-by: Samuel Attard <[email protected]>
1 parent d7a5583 commit dac22e1

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

‎src/react.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)