useHash
A hook to get current hash
Installation
npx shadcn@latest add @hooks/use-hashpnpm dlx shadcn@latest add @hooks/use-hashyarn dlx shadcn@latest add @hooks/use-hashbun x shadcn@latest add @hooks/use-hashCopy and paste the following code into your project.
import { useSyncExternalStore } from 'react'
function subscribe(onStoreChange: () => void) {
window.addEventListener('hashchange', onStoreChange)
return () => window.removeEventListener('hashchange', onStoreChange)
}
function getSnapshot() {
return window.location.hash
}
function getServerSnapshot() {
return ''
}
export function useHash() {
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
}API
/**
* A hook to get current hash
* @returns The current hash
*/
export function useHash(): stringCredits
Last updated on