Appearance
useHash
Shorthand for a reactive window.location.hash.
Demo
Usage
tsx
import { useHash } from '@reause/core'
const [hash, setHash] = useHash()
console.log(hash) // '#foobar'
setHash('foobar') // window.location.hash becomes '#foobar'Pass a default value exposed while the hash is empty, and pick the history mode used when writing it:
tsx
const [hash, setHash] = useHash('foobar', { mode: 'push' })
setHash('') // clears the hash, `hash` falls back to 'foobar'Type Declarations
ts
export interface UseHashOptions {
mode?: 'replace' | 'push';
}
export type UseHashReturn = [
hash: string,
setHash: (value: string) => void
]
export function useHash(defaultValue?: string, options?: UseHashOptions): UseHashReturn