apple2js/js/components/hooks/useHash.ts

20 lines
478 B
TypeScript

import { useEffect, useState } from 'preact/hooks';
export const useHash = () => {
const [hash, setHash] = useState(window.location.hash);
const popstateListener = () => {
const hash = window.location.hash;
setHash(hash);
};
useEffect(() => {
window.addEventListener('popstate', popstateListener);
return () => {
window.removeEventListener('popstate', popstateListener);
};
}, []);
return hash;
};