typescriptbeginner
useDocumentTitle Hook
Dynamically updates the document title and restores it on unmount.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { useEffect, useRef } from 'react';
export function useDocumentTitle(title: string, restoreOnUnmount = true) {
const previousTitle = useRef(document.title);
useEffect(() => {
document.title = title;
}, [title]);
useEffect(() => {
if (restoreOnUnmount) {
return () => {
document.title = previousTitle.current;
};
}
}, [restoreOnUnmount]);
}Use Cases
- Page title updates
- Tab identification
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
useFetch — Generic Data Fetching Hook
Custom React hook for data fetching with loading, error, and refetch states using the Fetch API.
Best for: API data fetching
#hooks#fetch
typescriptbeginner
useDebounce — Debounced Value Hook
Debounce any rapidly-changing value with a configurable delay. Useful for search inputs and resize handlers.
Best for: Search-as-you-type
#hooks#debounce
typescriptintermediate
useLocalStorage — Persistent State Hook
Sync React state with localStorage including SSR safety, JSON serialization, and cross-tab updates.
Best for: User preferences
#hooks#localstorage
typescriptbeginner
useClickOutside — Outside Click Detection
Detect clicks outside a referenced element to close dropdowns, modals, and popover menus.
Best for: Dropdown menus
#hooks#click-outside