UNPKG

889 BTypeScriptView Raw
1import * as React from 'react';
2import type { NavigationContainerRef } from '@react-navigation/core';
3import type { DocumentTitleOptions } from './types';
4
5/**
6 * Set the document title for the active screen
7 */
8export default function useDocumentTitle(
9 ref: React.RefObject<NavigationContainerRef>,
10 {
11 enabled = true,
12 formatter = (options, route) => options?.title ?? route?.name,
13 }: DocumentTitleOptions = {}
14) {
15 React.useEffect(() => {
16 if (!enabled) {
17 return;
18 }
19
20 const navigation = ref.current;
21
22 if (navigation) {
23 const title = formatter(
24 navigation.getCurrentOptions(),
25 navigation.getCurrentRoute()
26 );
27
28 document.title = title;
29 }
30
31 return navigation?.addListener('options', (e) => {
32 const title = formatter(e.data.options, navigation?.getCurrentRoute());
33
34 document.title = title;
35 });
36 });
37}