UtilityBeginnerSSR-safehooks/utility/useMounted.ts

useMounted

true after the component mounts.

Import

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

API

Signature

useMounted() → boolean

When to use

Use useMounted 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 · useMounted
Editor
function ClientOnly({ children }: { children: React.ReactNode }) {
  const mounted = useMounted();
  if (!mounted) return null;
  return children;
}
Preview

Implementation source

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

hooks/utility/useMounted.ts
import { useEffect, useState } from 'react';

/** `true` after the component mounts (client-only). */
export function useMounted(): boolean {
  const [mounted, setMounted] = useState(false);
  useEffect(() => {
    setMounted(true);
  }, []);
  return mounted;
}

Copy-paste example

useMounted.example.tsx
'use client';

import { useMounted } from '@/hooks';

export function ClientOnly({ children }: { children: React.ReactNode }) {
  const mounted = useMounted();
  if (!mounted) return null;
  return children;
}

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