(callback: T)
| 20 | * <ChildComponent onClick={handleClick} /> |
| 21 | */ |
| 22 | export function useEvent<T extends (...args: any[]) => any>(callback: T): T { |
| 23 | const callbackRef = useRef<T>(callback) |
| 24 | |
| 25 | // Update the ref to the latest callback on every render |
| 26 | // This ensures the ref is always in sync with the current render |
| 27 | callbackRef.current = callback |
| 28 | |
| 29 | // Return a stable function that calls the latest callback |
| 30 | return useCallback( |
| 31 | ((...args: any[]) => callbackRef.current(...args)) as T, |
| 32 | [], // Empty deps array ensures the function identity is stable |
| 33 | ) |
| 34 | } |
no outgoing calls
no test coverage detected