Last active
October 30, 2025 21:45
-
-
Save MarekZeman91/1522b6727836326251a10bcf437d5a70 to your computer and use it in GitHub Desktop.
React hook that allows you to have always up-to-date function without re-render trigger.
This file contains hidden or 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 { useCallback } from 'react'; | |
| // eslint-disable-next-line @typescript-eslint/no-unsafe-function-type | |
| export function useFunction<T extends Function>(fn: T): T { | |
| const hook = useCallback<any>(function (this: any, ...args: any[]) { | |
| return hook.fn.apply(this, args); | |
| }, []); | |
| hook.fn = fn; | |
| return hook as T; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment