-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathuseTitle.ts
More file actions
28 lines (22 loc) · 708 Bytes
/
Copy pathuseTitle.ts
File metadata and controls
28 lines (22 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { useEffect, useRef } 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);
if (document.title !== title) document.title = title;
useEffect(() => {
const curTitleRef = prevTitleRef.current;
if (options && options.restoreOnUnmount) {
return () => {
document.title = curTitleRef;
};
} else {
return;
}
}, [options]);
}
export default typeof document !== 'undefined' ? useTitle : (_title: string) => {};