1.3 KiB
1.3 KiB
usePageMeta
Sets the page <title> and/or <meta name="description"> while a component is mounted, then restores the previous values on unmount.
Signature
function usePageMeta(params: UsePageMetaParams): void;
interface UsePageMetaParams {
title?: string;
description?: string;
}
| Param | Type | Description |
|---|---|---|
title |
string |
Value to set as document.title |
description |
string |
Value to set in <meta name="description"> |
Both params are optional — pass only the ones you need.
Usage
function ArticlePage({ article }: { article: Article }) {
usePageMeta({
title: article.title,
description: article.summary,
});
return <main>{article.content}</main>;
}
// Title only
function DashboardPage() {
usePageMeta({ title: 'Dashboard' });
return <div />;
}
Notes
- Restores the previous
document.titleand<meta>content when the component unmounts, so nested or sequential usage is safe. - If no
<meta name="description">tag exists in the document, one is created and removed on cleanup. - Client-only (
'use client'). Does not run during SSR.