24 lines
500 B
TypeScript
24 lines
500 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
export default function useHashChange() {
|
|
const [hash, setHash] = useState(
|
|
typeof window === 'undefined' ? null : window.location.hash,
|
|
);
|
|
|
|
useEffect(() => {
|
|
function handleHashChange() {
|
|
setHash(window.location.hash);
|
|
}
|
|
|
|
window.addEventListener('hashchange', handleHashChange);
|
|
|
|
handleHashChange();
|
|
|
|
return () => {
|
|
window.removeEventListener('hashchange', handleHashChange);
|
|
};
|
|
}, []);
|
|
|
|
return hash;
|
|
}
|