基本用法
import { useState, useCallback, useEffect } from 'react';
export const useHash = () => {
const [hash, setHash] = useState(() => window.location.hash);
const onHashChange = useCallback(() => {
setHash(window.location.hash);
}, []);
useEffect(() => {
window.addEventListener('hashchange', onHashChange);
return () => {
window.removeEventListener('hashchange', onHashChange);
};
}, []);
const _setHash = useCallback(
(newHash: string) => {
if (newHash !== hash) {
window.location.hash = newHash;
}
},
[hash],
);
return [hash, _setHash] as const;
};
import { useHash } from './useHash';
export default function App(){
const [hash, setHash] = useHash();
}
|