Skip to main content
Version: 1.0.0

useDocumentTitle

Deprecated

useDocumentTitle is deprecated and will be removed in 2.0. React 19 supports rendering <title> from any component and hoists it into <head>. The hook logs a one-time warning outside production.

function ProductPage({ name }: { name: string }) {
return (
<>
<title>{`${name} — My Shop`}</title>
<h1>{name}</h1>
</>
);
}

See the migration guide.

Sets document.title declaratively and restores the original title on unmount (unless opted out).

Signature​

function useDocumentTitle(title: string, persistOnUnmount?: boolean): void

Parameters​

ParameterTypeDefaultDescription
titlestring—The title to set
persistOnUnmountbooleanfalseWhen true, the title is not restored when the component unmounts

Behaviour​

  • Sets document.title immediately on each render when title changes.
  • Captures the original title in a ref on mount.
  • Restores the original title on unmount, unless persistOnUnmount is true.

Example​

import { useDocumentTitle } from "@julianfere/hooked";

function ProductPage({ name }: { name: string }) {
useDocumentTitle(`${name} — My Shop`);
return <h1>{name}</h1>;
}
note

This hook interacts with document.title which is not available in server-side rendering environments. In an SSR setup, ensure the hook only runs on the client.