Created
July 31, 2025 21:34
-
-
Save Wigny/7a6267228e7b8582db07e1e780cb67c2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { useSyncExternalStore } from 'react'; | |
| function onHashChange(callback: () => void) { | |
| window.addEventListener('hashchange', callback); | |
| return () => window.removeEventListener('hashchange', callback); | |
| } | |
| function getHash() { | |
| return window.location.hash.slice(1) || null; | |
| } | |
| function setHash(hash: string | null) { | |
| window.location.hash = hash ?? ''; | |
| } | |
| export const useURLHash = (): [string | null, (hash: string | null) => void] => { | |
| const hash = useSyncExternalStore(onHashChange, getHash); | |
| return [hash, setHash]; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment