Skip to content

useDocumentTitle

Dynamically update the document title of a webpage.

The useDocumentTitle hook is helpful when you want to dynamically update the document title based on a current state or data.

useDocumentTitle

Count is : 0

**Check the document title

Usage

Import the hook from @abhushanaj/react-hooks and use in required component.

./src/App.tsx
import React from 'react';
import { useDocumentTitle } from '@abhushanaj/react-hooks';
function App() {
const [count, setCount] = React.useState(0);
useDocumentTitle(`Count: ${count}`);
return (
<div>
<p>Count is : {count}</p>
<button onClick={() => setCount((prev) => prev + 1)}>Increment by 1</button>
</div>
);
}
export default App;

With config options

./src/App.tsx
import React from 'react';
import { useDocumentTitle } from '@abhushanaj/react-hooks';
import type { UseDocumentTitleOptions } from '@abhushanaj/react-hooks';
function App() {
const [count, setCount] = React.useState(0);
const options: UseDocumentTitleOptions = {
resetOnUnmount: true
};
useDocumentTitle(`Count: ${count}`, options);
return (
<div>
<p>Count is : {count}</p>
<button onClick={() => setCount((prev) => prev + 1)}>Increment by 1</button>
</div>
);
}
export default App;

API Reference

Parameters

ParameterTypeDescriptionDefault
titlestringThe title to set for the document.N/A
optionsDocumentTitleOptions (optional)The configuration options.undefined

Types Reference

1. DocumentTitleOptions

ParameterTypeDescriptionDefault
resetOnUnmountbooleanWhether to reset to the original title after unmount.false