PerformanceIntermediateSSR-safehooks/performance/useRenderCount.ts

useRenderCount

Count how many times the component rendered.

Import

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

API

Signature

useRenderCount() → number

When to use

Use useRenderCount 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 · useRenderCount
Editor
function Debug() {
  const renders = useRenderCount();
  return <p>Renders: {renders}</p>;
}
Preview

Implementation source

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

hooks/performance/useRenderCount.ts
import { useRef } from 'react';

/** Increments and returns the render count (starts at 1). */
export function useRenderCount(): number {
  const count = useRef(0);
  count.current += 1;
  return count.current;
}

Copy-paste example

useRenderCount.example.tsx
'use client';

import { useRenderCount } from '@/hooks';

export function Debug() {
  const renders = useRenderCount();
  return <p>Renders: {renders}</p>;
}

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