UNPKG

@mantine/hooks

Version:

A collection of 50+ hooks for state and UI management

24 lines (23 loc) 843 B
"use client"; import { useWindowEvent } from "../use-window-event/use-window-event.mjs"; import { useEffect, useState } from "react"; //#region packages/@mantine/hooks/src/use-hash/use-hash.ts function useHash({ getInitialValueInEffect = true } = {}) { const [hash, setHash] = useState(getInitialValueInEffect ? "" : window.location.hash || ""); const setHashHandler = (value) => { const valueWithHash = value.startsWith("#") ? value : `#${value}`; window.location.hash = valueWithHash; setHash(valueWithHash); }; useWindowEvent("hashchange", () => { const newHash = window.location.hash; if (hash !== newHash) setHash(newHash); }); useEffect(() => { if (getInitialValueInEffect) setHash(window.location.hash); }, []); return [hash, setHashHandler]; } //#endregion export { useHash }; //# sourceMappingURL=use-hash.mjs.map