Skip to content

useHash

Category
Export Size
522 B
Last Changed
5 days ago

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

Source

Source · Demo

Contributors

hairyf

Changelog

v0.1.0 on
e608a - feat(ci): align CI, packaging and release with VueUse
60c01 - feat(core): implement useHash (#199)

Released under the MIT License. v0.1.8