Playground

useHash

Read and set location.hash as state.

  • No DOM output. Changing the hash adds a history entry, so Back works.

Import

import '@monoline/css';
import {useHash} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add use-hash

Usage

import {Button, useHash, type Demo} from '@monoline/react';

function Demo() {
  const [hash, setHash] = useHash();
  return <div style={{display: 'flex', gap: 'var(--s3)', alignItems: 'center'}}><output>#{hash || '(empty)'}</output><Button variant="line" size="sm" onClick={() => setHash('use-hash')}>Set #use-hash</Button></div>;
}

<Demo />

Examples

Hash state

Behaviour

  • No DOM output. Changing the hash adds a history entry, so Back works.