@darwish/hooks-core
Version:
21 lines (20 loc) • 699 B
JavaScript
import { isBrowser } from "@darwish/utils-is";
import { useEffect, useRef } from "react";
import useUnmount from "./useUnmount";
/**
* This hook is used to set the title of the page.
* @param title The title of the page.
* @param restoreOnUnmount Whether to restore the title when the component unmounts.
*/
export default function useTitle(title, restoreOnUnmount) {
if (restoreOnUnmount === void 0) { restoreOnUnmount = false; }
var titleRef = useRef(isBrowser ? document.title : '');
useEffect(function () {
document.title = title;
}, []);
useUnmount(function () {
if (restoreOnUnmount) {
document.title = titleRef.current;
}
});
}