@react-hookz/web
Version:
React hooks done right, for browser and SSR.
29 lines (28 loc) • 1.11 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDocumentTitle = void 0;
var react_1 = require("react");
var const_1 = require("../util/const");
var __1 = require("..");
/**
* Sets title of the page.
*
* @param title Title to set, if wrapper option is set, it will be passed through wrapper function.
* @param options Options object.
*/
function useDocumentTitle(title, options) {
if (options === void 0) { options = {}; }
var titleRef = (0, react_1.useRef)(const_1.isBrowser ? document.title : '');
var optionsRef = (0, __1.useSyncedRef)(options);
// it is safe not to check isBrowser here, as effects are not invoked in SSR
(0, react_1.useEffect)(function () {
document.title = options.wrapper ? options.wrapper(title) : title;
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [title, options.wrapper]);
(0, __1.useUnmountEffect)(function () {
if (optionsRef.current.restoreOnUnmount) {
document.title = titleRef.current;
}
});
}
exports.useDocumentTitle = useDocumentTitle;