usePageTitle is a hook that sets document.title for the lifetime of a component. It supports nested usage: it keeps a stack of titles and, on unmount, restores the previous title (or the original one if the stack becomes empty).
function usePageTitle(title: string): void;-
Parameters
title: string— the page title to set on the current render.
-
Returns
void— no return value; the hook managesdocument.titleand the title history.
import { usePageTitle } from '@webeach/react-hooks/usePageTitle';
export function SettingsPage() {
usePageTitle('Settings — MyApp');
return <h1>Settings</h1>;
}import { usePageTitle } from '@webeach/react-hooks/usePageTitle';
function PageLayout() {
usePageTitle('Dashboard — MyApp');
return <Widget />; // a child can override the title
}
function Widget() {
usePageTitle('Notifications — MyApp');
return <div />; // while Widget is mounted, the title is "Notifications — MyApp"
}
// After Widget unmounts, the title reverts to "Dashboard — MyApp"import { usePageTitle } from '@webeach/react-hooks/usePageTitle';
type UserProfileProps = {
name: string;
};
export function UserProfile(props: UserProfileProps) {
const { name } = props;
usePageTitle(`${name} — Profile`);
return <h1>{name}</h1>;
}-
LIFO priority
- The most recently mounted
usePageTitlehas priority and defines the current title. Updates to lower levels do not appear until the top instance unmounts.
- The most recently mounted
-
Title restoration
- On unmount, the hook removes its entry from the stack and restores the previous title. When the stack is empty, it restores the original
document.titlecaptured on the hook’s first use.
- On unmount, the hook removes its entry from the stack and restores the previous title. When the stack is empty, it restores the original
-
Update on every render
- When the current instance’s
titlechanges, the title is updated immediately; the stack entry is updated without changing the order (priority still belongs to the most recently mounted instance).
- When the current instance’s
-
Layout semantics
- The title is updated during the layout phase (isomorphic layout effect), ensuring it’s applied before paint on the client.
-
SSR‑safe
- There are no side effects on the server; access to
documenthappens only after mount. The hook is safe for SSR/ISR.
- There are no side effects on the server; access to
-
Global original value
- The original
document.titleis saved once on the first hook usage and is used to restore the title after the last entry is removed.
- The original
- Pages and routes in SPA/MPA where a component is responsible for the tab title.
- Nested widgets/modals that temporarily override the title.
- Dynamic titles based on data (user name, page state, etc.).
- If the title is managed by a router/meta framework (Next/Remix/React‑Helmet) — avoid having multiple sources of truth; prefer a single mechanism.
- If the title should not change per component — static markup is enough.
-
Expecting “last update” priority instead of “last mount”
- The stack order is defined by mounting, not by the time of the last update. To temporarily raise priority, mount a separate hook instance higher in the tree or unmount the current top one.
-
Managing the title from multiple places
- If both the router and components change the title, you may see tug‑of‑war behavior. Choose a single source of truth.
-
Attempting to read
documentduring SSRdocumentis unavailable on the server. Let the hook handle updates on the client after mount.