MCPcopy Create free account
hub / github.com/bvaughn/react-window / useStableCallback

Function useStableCallback

lib/hooks/useStableCallback.ts:11–26  ·  view source on GitHub ↗
(
  fn: ((...args: Args) => Return) | undefined
)

Source from the content-addressed store, hash-verified

9 fn: ((...args: Args) => Return) | undefined
10): ((...args: Args) => Return) | undefined;
11export function useStableCallback<Args extends unknown[], Return>(
12 fn: ((...args: Args) => Return) | undefined
13): ((...args: Args) => Return) | undefined {
14 const ref = useRef<typeof fn>(() => {
15 // This error ensures components don't render with previous/stale function
16 throw new Error("Cannot call an event handler while rendering.");
17 });
18
19 useIsomorphicLayoutEffect(() => {
20 ref.current = fn;
21 }, [fn]);
22
23 return useCallback((...args: Args) => ref.current?.(...args), [ref]) as (
24 ...args: Args
25 ) => Return;
26}

Callers 3

useDynamicRowHeightFunction · 0.90
useVirtualizerFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected