@mantine/hooks
Version:
A collection of 50+ hooks for state and UI management
24 lines (23 loc) • 893 B
JavaScript
"use client";
const require_use_window_event = require("../use-window-event/use-window-event.cjs");
let react = require("react");
//#region packages/@mantine/hooks/src/use-hash/use-hash.ts
function useHash({ getInitialValueInEffect = true } = {}) {
const [hash, setHash] = (0, react.useState)(getInitialValueInEffect ? "" : window.location.hash || "");
const setHashHandler = (value) => {
const valueWithHash = value.startsWith("#") ? value : `#${value}`;
window.location.hash = valueWithHash;
setHash(valueWithHash);
};
require_use_window_event.useWindowEvent("hashchange", () => {
const newHash = window.location.hash;
if (hash !== newHash) setHash(newHash);
});
(0, react.useEffect)(() => {
if (getInitialValueInEffect) setHash(window.location.hash);
}, []);
return [hash, setHashHandler];
}
//#endregion
exports.useHash = useHash;
//# sourceMappingURL=use-hash.cjs.map