const setTitle = (title) => {
const prevTitle = document.title;
document.title = title;
return () => document.title = prevTitle;
}
const MyComponent = () => {
useEffect(() => setTitle('Title while MyComponent is mounted'), []);
return <div>My Component</div>;
}
This is a pretty straight forward solution I threw together while working today. setTitle
returns a function that resets the title to what it was prior to using setTitle
, it works wonderfully inside of React's useEffect
hook.