use-hash

Get and set hash value in url

Import

Usage

use-hash returns hash from URL, subscribes to its changes with hashchange event and allows changing it with setHash function:

Current hash:

import { useHash, randomId } from '@mantine/hooks';
import { Button, Text, Code } from '@mantine/core';

function Demo() {
  const [hash, setHash] = useHash();
  return (
    <>
      <Button onClick={() => setHash(randomId())}>Set hash to random string</Button>
      <Text>Current hash: <Code>{hash}</Code></Text>
    </>
  );
}

Definition

function useHash(): readonly [string, (value: string) => void];