Last active
July 28, 2021 15:32
-
-
Save sstur/69052532bc7cdefa6dffd7d1b7a06535 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
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