useDocumentTitle
Sets document.title declaratively and restores the original title on unmount (unless opted out).
Signature
function useDocumentTitle(title: string, persistOnUnmount?: boolean): void
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
title | string | — | The title to set |
persistOnUnmount | boolean | false | When true, the title is not restored when the component unmounts |
Behaviour
- Sets
document.titleimmediately on each render whentitlechanges. - Captures the original title in a
refon mount. - Restores the original title on unmount, unless
persistOnUnmountistrue.
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.