All files / client/src/app HistoryContext.tsx

88.88% Statements 8/9
100% Branches 0/0
100% Functions 3/3
88.88% Lines 8/9

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);
};