useStableCallback
Stable function identity that calls the latest fn.
Import
TypeScript
import { useStableCallback } from '@/hooks';API
Signature
useStableCallback(fn) → stableFn
When to use
Use useStableCallback 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 · useStableCallbackEditor
function ChildFriendly({ count }: { count: number }) { // identity never changes; always sees latest count const log = useStableCallback(() => console.log(count)); return <button onClick={log}>Log count</button>; }
Preview
Implementation source
Full library source for useStableCallback — copy into your project when you are not installing a package yet.
hooks/performance/useStableCallback.ts
export { useStableCallback } from '../utility/useStableCallback';
Copy-paste example
useStableCallback.example.tsx
'use client';
import { useStableCallback } from '@/hooks';
export function ChildFriendly({ count }: { count: number }) {
// identity never changes; always sees latest count
const log = useStableCallback(() => console.log(count));
return <button onClick={log}>Log count</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/useStableCallback.tsfor implementation details