Skip to content

Instantly share code, notes, and snippets.

@raibima
Created September 19, 2019 10:04
Show Gist options
  • Select an option

  • Save raibima/0ffc366d7e1a2fea5a4858274daee633 to your computer and use it in GitHub Desktop.

Select an option

Save raibima/0ffc366d7e1a2fea5a4858274daee633 to your computer and use it in GitHub Desktop.
const { useEffect, useState } = React;
export default function IndexPage(props) {
const [currentHash, setCurrentHash] = useState(window.location.hash);
useEffect(() => {
function handleHashChange(e) {
setCurrentHash(window.location.hash);
}
window.addEventListener('hashchange', handleHashChange);
return () => {
window.removeEventListener('hashchange', handleHashChange);
}
}, []);
return (
<main>
<h1>Index Page</h1>
<h2>{`Current hash: ${currentHash}`}</h2>
<button onClick={() => {
location.hash = "foo";
}}>
Add hash
</button>
</main>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment