diff --git a/src/mantine-hooks/src/use-hash/use-hash.ts b/src/mantine-hooks/src/use-hash/use-hash.ts index 264dfb04bdb..4436f269bdc 100644 --- a/src/mantine-hooks/src/use-hash/use-hash.ts +++ b/src/mantine-hooks/src/use-hash/use-hash.ts @@ -5,16 +5,20 @@ export function useHash() { const [hash, setHashValue] = useState(''); const setHash = (value: string) => { - window.location.hash = value; - setHashValue(value); + const valueWithHash = value.startsWith('#') ? value : `#${value}`; + window.location.hash = valueWithHash; + setHashValue(valueWithHash); }; useWindowEvent('hashchange', () => { - setHashValue(window.location.hash); + const newHash = window.location.hash; + if (hash !== newHash) { + setHashValue(hash); + } }); useEffect(() => { - setHash(window.location.hash); + setHashValue(window.location.hash); }, []); return [hash, setHash] as const;