Created
December 6, 2021 09:54
-
-
Save artalar/dc08a52a0ac8da2e16839e76a6c7eca7 to your computer and use it in GitHub Desktop.
This file contains 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 { useRef } from 'react' | |
export function useCallbackRef<Deps extends any[], Args extends any[], Return>( | |
fn: (deps: Deps, ...args: Args) => Return, | |
deps: Deps, | |
): (...args: Args) => Return { | |
const argsRef = useRef({ | |
fn: (...args: Args) => fn(argsRef.current.deps, ...args), | |
deps, | |
}) | |
argsRef.current.deps = deps | |
return argsRef.current.fn | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment