mirror of
https://github.com/EnixCoda/Gitako.git
synced 2026-03-11 08:54:44 +00:00
15 lines
421 B
TypeScript
15 lines
421 B
TypeScript
import { useCallback, useEffect, useRef } from 'react'
|
|
|
|
function useLatestValueRef<T>(value: T) {
|
|
const ref = useRef(value)
|
|
useEffect(() => {
|
|
ref.current = value
|
|
})
|
|
return ref
|
|
}
|
|
export function useCallbackRef<Args extends AnyArray, R>(
|
|
callback: (...args: Args) => R,
|
|
): (...args: Args) => R {
|
|
const ref = useLatestValueRef(callback)
|
|
return useCallback((...args: Args) => ref.current(...args), [ref])
|
|
}
|