You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
11 lines
436 B
11 lines
436 B
import { useCallback, useRef } from 'react' |
|
|
|
/** |
|
* Stable callback identity with always-fresh implementation (React `useEvent` pattern). |
|
* Avoids stale closures without forcing unrelated consumers to re-render. |
|
*/ |
|
export function useEventCallback<A extends readonly unknown[], R>(fn: (...args: A) => R): (...args: A) => R { |
|
const ref = useRef(fn) |
|
ref.current = fn |
|
return useCallback((...args: A) => ref.current(...args), []) |
|
}
|
|
|