UNPKG

etc-hooks

Version:
32 lines (31 loc) 805 B
import { useEffect, useRef } from "react"; import useUnmount from "../useUnmount"; import isBrowser from "../utils/isBrowser"; var DEFAULT_OPTIONS = { restorePrevTitle: false }; /** * @param {string} title * @param {Object} options * @description * 1. 实现更新 title * 2. 组件销毁时,回复本身的页面标题 */ function useTitle(title, options) { if (options === void 0) { options = DEFAULT_OPTIONS; } var titleRef = useRef(isBrowser ? document.title : ""); useEffect(function() { if (title.trim().length > 0) { document.title = title; } }, [ title ]); useUnmount(function() { if (options.restorePrevTitle) { document.title = titleRef.current; } }); } export default useTitle;