Skip to content

Instantly share code, notes, and snippets.

@artalar
Created December 6, 2021 09:54
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 artalar/dc08a52a0ac8da2e16839e76a6c7eca7 to your computer and use it in GitHub Desktop.
Save artalar/dc08a52a0ac8da2e16839e76a6c7eca7 to your computer and use it in GitHub Desktop.
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