PerformanceIntermediateSSR-safehooks/performance/useMemoizedFn.ts

useMemoizedFn

Alias of useStableCallback.

Import

TypeScript
import { useMemoizedFn } from '@/hooks';

API

Signature

useMemoizedFn(fn) → stableFn

When to use

Use useMemoizedFn instead of re-implementing the same pattern in multiple components.

SSR / Next.js

SSR-safe. Timers and rAF only run after mount / interactions.

Live playground

Edit the example and see it run against the real @/hooks package in this app.

Live playgroundEdit · run with real @/hooks · useMemoizedFn
Editor
function Example({ n }: { n: number }) {
  const fn = useMemoizedFn(() => n * 2);
  return <button onClick={() => alert(fn())}>Double</button>;
}
Preview

Implementation source

Full library source for useMemoizedFn — copy into your project when you are not installing a package yet.

hooks/performance/useMemoizedFn.ts
export { useMemoizedFn } from '../utility/useMemoizedFn';

Copy-paste example

useMemoizedFn.example.tsx
'use client';

import { useMemoizedFn } from '@/hooks';

export function Example({ n }: { n: number }) {
  const fn = useMemoizedFn(() => n * 2);
  return <button onClick={() => alert(fn())}>Double</button>;
}

Production checklist

  • Called only from Client Components or other custom hooks
  • Type parameters filled in (e.g. useFetch<User[]>)
  • Tip: Stable callbacks prevent child re-renders. Always cancel debounce on unmount (handled for you).
  • Source of truth: open hooks/performance/useMemoizedFn.ts for implementation details