When someone manually modifies the URL hash (aka the URI fragment identifier), how can I trigger a re-render?
I have a custom hook looking like this:
function useUrlHash(): string | null {
const router = useRouter();
useEffect(() => {
console.log("the effect ran!");
}, [router.asPath])
if (!router.isReady) {
return null;
}
return router.asPath.split("#")[1];
}
I expect that when router.asPath
changes (such as the user manually changing the hash and pressing Enter), the effect should run, thus triggering a re-render with the new value return by useUrlHash
.
But it does not. Why? And what's the simplest way to trigger re-render? Ideally without holding on to the URL hash state in React (so single source of truth is in the URL itself).