Skip to content

Instantly share code, notes, and snippets.

@sstur
Last active July 28, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sstur/69052532bc7cdefa6dffd7d1b7a06535 to your computer and use it in GitHub Desktop.
Save sstur/69052532bc7cdefa6dffd7d1b7a06535 to your computer and use it in GitHub Desktop.
export function useStableCallback<T extends Array<unknown>, R>(
callback: (...args: T) => R,
): (...args: T) => R {
let ref = useRef(callback);
useLayoutEffect(() => {
ref.current = callback;
});
let stableCallback = useCallback((...args: T) => {
return ref.current(...args);
}, []);
return stableCallback;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment