@darwish/hooks-core
Version:
27 lines (26 loc) • 991 B
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_is_1 = require("@darwish/utils-is");
var react_1 = require("react");
var useUnmount_1 = __importDefault(require("./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.
*/
function useTitle(title, restoreOnUnmount) {
if (restoreOnUnmount === void 0) { restoreOnUnmount = false; }
var titleRef = (0, react_1.useRef)(utils_is_1.isBrowser ? document.title : '');
(0, react_1.useEffect)(function () {
document.title = title;
}, []);
(0, useUnmount_1.default)(function () {
if (restoreOnUnmount) {
document.title = titleRef.current;
}
});
}
exports.default = useTitle;