Skip to content

Instantly share code, notes, and snippets.

@zhouzi
Created April 20, 2023 06:07
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 zhouzi/39b42c2a0480722bad8b38b4dcb8b88a to your computer and use it in GitHub Desktop.
Save zhouzi/39b42c2a0480722bad8b38b4dcb8b88a to your computer and use it in GitHub Desktop.
import { useCallback, useRef } from "react";
export function useLatestCallback<T extends (...args: any[]) => any>(callback: T): T {
const ref = useRef(callback);
ref.current = callback;
return useCallback((...args: Parameters<T>) => ref.current(...args), []) as T;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment