UtilityBeginnerSSR-safehooks/utility/useFirstMountState.ts

useFirstMountState

true only on the first render.

Import

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

API

Signature

useFirstMountState() → boolean

When to use

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

SSR / Next.js

Browser utilities are SSR-gated; lifecycle hooks are safe.

Live playground

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

Live playgroundEdit · run with real @/hooks · useFirstMountState
Editor
function FirstRenderNote() {
  const first = useFirstMountState();
  return <p>{first ? 'First render' : 'Updated'}</p>;
}
Preview

Implementation source

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

hooks/utility/useFirstMountState.ts
import { useRef } from 'react';

/** `true` only on the first render. */
export function useFirstMountState(): boolean {
  const isFirst = useRef(true);
  if (isFirst.current) {
    isFirst.current = false;
    return true;
  }
  return false;
}

Copy-paste example

useFirstMountState.example.tsx
'use client';

import { useFirstMountState } from '@/hooks';

export function FirstRenderNote() {
  const first = useFirstMountState();
  return <p>{first ? 'First render' : 'Updated'}</p>;
}

Production checklist

  • Called only from Client Components or other custom hooks
  • Type parameters filled in (e.g. useFetch<User[]>)
  • Tip: Lifecycle hooks are for side effects only — not for deriving render state.
  • Source of truth: open hooks/utility/useFirstMountState.ts for implementation details