mirror of
https://github.com/streamich/react-use.git
synced 2026-01-25 14:17:16 +00:00
23 lines
629 B
TypeScript
23 lines
629 B
TypeScript
import { useRef, useEffect } from 'react';
|
|
export interface UseTitleOptions {
|
|
restoreOnUnmount?: boolean;
|
|
}
|
|
const DEFAULT_USE_TITLE_OPTIONS: UseTitleOptions = {
|
|
restoreOnUnmount: false,
|
|
};
|
|
function useTitle(title: string, options: UseTitleOptions = DEFAULT_USE_TITLE_OPTIONS) {
|
|
const prevTitleRef = useRef(document.title);
|
|
document.title = title;
|
|
useEffect(() => {
|
|
if (options && options.restoreOnUnmount) {
|
|
return () => {
|
|
document.title = prevTitleRef.current;
|
|
};
|
|
} else {
|
|
return;
|
|
}
|
|
}, []);
|
|
}
|
|
|
|
export default typeof document !== 'undefined' ? useTitle : (_title: string) => {};
|