A lightweight and customizable React hook for managing document titles with support for prefixes, suffixes, custom separators, title restoration, and page visibility changes.
- π Lightweight and easy to use
- βοΈ Built for React 18 and React 19
- π Set the document title with a simple API
- π Add custom prefixes and suffixes
- β¨ Customize the separator between title parts
- π Restore the previous title on unmount
- π Change the title when the page becomes hidden
- π Fully written in TypeScript
npm install react-use-title
export default function App() {
useTitle("Dashboard")
return <h1>react-use-title</h1>;
}
export default function App() {
useTitle("Dashboard",{
prefix:"Admin"
})
return <h1>react-use-title</h1>;
}
export default function App() {
useTitle("Dashboard",{
suffix:"My App"
})
return <h1>react-use-title</h1>;
}
export default function App() {
useTitle("Dashboard",{
prefix:"Admin",
suffix:"My App",
separator:"π"
})
return <h1>react-use-title</h1>;
}
5. onHidden
export default function App() {
useTitle("Dashboard",{
prefix:"Admin",
suffix:"My App",
separator:"π",
onHidden:{title:"Why Did You Leave? π"}
})
return <h1>react-use-title</h1>;
}Note: GIF may take a few seconds to load.
useTitle(title: string, options?: UseTitleOptions): void| Parameter | Type | Description |
|---|---|---|
title |
string |
The document title. |
options |
UseTitleOptions |
Optional configuration object. |
| Option | Type | Default | Description |
|---|---|---|---|
prefix |
string |
undefined |
Adds text before the title. |
suffix |
string |
undefined |
Adds text after the title. |
separator |
string |
"|" (Pipe) |
Separator between title parts. |
restoreOnUnmount |
boolean |
false |
Restores the previous document title when the component unmounts. |
onHidden |
{ title: string } |
undefined |
Changes the document title when the page becomes hidden. |
This project is licensed under the MIT License. See the LICENSE file for details.