Skip to content

Instantly share code, notes, and snippets.

@MarekZeman91
Last active October 30, 2025 21:45
Show Gist options
  • Select an option

  • Save MarekZeman91/1522b6727836326251a10bcf437d5a70 to your computer and use it in GitHub Desktop.

Select an option

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.
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