Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | 356x 356x 1295x 1295x 1295x 1210x 356x 1286x | import {
createContext,
useContext,
useEffect,
useRef,
ReactNode,
ReactElement,
} from "react";
import { Location, useLocation } from "react-router";
const HistoryContext = createContext<Location | null>(null);
interface Props {
children: ReactNode;
}
export const HistoryProvider = ({ children }: Props): ReactElement => {
const location = useLocation();
const previousLocation = useRef<Location | null>(null);
useEffect(() => {
previousLocation.current = location;
}, [location]);
return (
// eslint-disable-next-line react-hooks/refs
<HistoryContext.Provider value={previousLocation.current}>
{children}
</HistoryContext.Provider>
);
};
export const usePreviousLocation = (): Location | null => {
return useContext(HistoryContext);
};
|