Skip to Content
ohooks

useHash

Reads and updates window.location.hash reactively using useSyncExternalStore.

Loading preview...

The useHash hook reads window.location.hash (without the leading # character) and updates it reactively across the app via useSyncExternalStore.

Installation

pnpm dlx shadcn@latest add https://ohooks.vercel.app/r/useHash.json

Usage

Examples

Hash-Based Tab Navigation

Sync the active tab with the URL hash so users can bookmark or share links to specific sections.

Replacing History Entries Instead of Pushing

Pass { replace: true } as the second argument to setHash to update the URL hash via history.replaceState without creating a new browser history entry.

API Reference

UseHashOptions

Configuration options for initial SSR fallback value.

Property
Specification
defaultHash?
stringdefault:''
Fallback hash string used during SSR

SetHashOptions

Options for controlling history navigation when updating the hash.

Property
Specification
replace?
uniondefault:false
Use history.replaceState instead of history.pushState