I'm trying to setup a hash route modal using the useHash() hook but I'm having some trouble.
Here's what I'm trying to do:
const hash = useHash()
const pathname = usePathname()
const { isOpen, onClose, onOpen } = useModal() // some kind of modal hook
useEffect(() => {
if (hash === "subscribe") {
onOpen()
} else {
onClose()
}
}, [hash, onClose, onOpen])
return (
<>
<Link href={`${pathname}#subscribe`}>
Subscribe
</Link>
<SubscribeModal isOpen={isOpen} onClose={onClose} />
</>
)
This seems to only work only on initial load (whether the app loads with the hash or not, the code runs). However if I try to add or remove the hash from the url after the initial load, the useEffect does not run anymore.
Is it possible to subscribe to the hash changes like you would with normal react state?