Skip to content

Listening to hash route changes #1

@dvzrd

Description

@dvzrd

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions