Created
April 20, 2023 06:07
-
-
Save zhouzi/39b42c2a0480722bad8b38b4dcb8b88a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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