UNPKG

1.03 kBJavaScriptView Raw
1import * as React from 'react';
2
3/**
4 * Set the document title for the active screen
5 */
6export default function useDocumentTitle(ref, {
7 enabled = true,
8 formatter = (options, route) => {
9 var _options$title;
10
11 return (_options$title = options === null || options === void 0 ? void 0 : options.title) !== null && _options$title !== void 0 ? _options$title : route === null || route === void 0 ? void 0 : route.name;
12 }
13} = {}) {
14 React.useEffect(() => {
15 if (!enabled) {
16 return;
17 }
18
19 const navigation = ref.current;
20
21 if (navigation) {
22 const title = formatter(navigation.getCurrentOptions(), navigation.getCurrentRoute());
23 document.title = title;
24 }
25
26 return navigation === null || navigation === void 0 ? void 0 : navigation.addListener('options', e => {
27 const title = formatter(e.data.options, navigation === null || navigation === void 0 ? void 0 : navigation.getCurrentRoute());
28 document.title = title;
29 });
30 });
31}
32//# sourceMappingURL=useDocumentTitle.js.map
\No newline at end of file