PerformanceIntermediateSSR-safehooks/performance/useAnimationFrame.ts

useAnimationFrame

Alias of useRaf.

Import

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

API

Signature

useAnimationFrame(callback, options?)

When to use

Use useAnimationFrame 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 · useAnimationFrame
Editor
function Anim() {
  useAnimationFrame((_t, delta) => {
    // frame work
  }, { enabled: true });
  return null;
}
Preview

Implementation source

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

hooks/performance/useAnimationFrame.ts
import { useRaf } from './useRaf';

/** Alias of {@link useRaf}. */
export function useAnimationFrame(
  callback: (time: number, delta: number) => void,
  options?: { enabled?: boolean },
): { isActive: boolean; start: () => void; stop: () => void } {
  return useRaf(callback, options);
}

Copy-paste example

useAnimationFrame.example.tsx
'use client';

import { useAnimationFrame } from '@/hooks';

export function Anim() {
  useAnimationFrame((_t, delta) => {
    // frame work
  }, { enabled: true });
  return null;
}

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/useAnimationFrame.ts for implementation details