All files / src/hooks/common/router useBackListener.ts

75% Statements 6/8
100% Branches 0/0
75% Functions 3/4
75% Lines 6/8

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        14x 2x 2x         2x   2x 2x            
import { useEffect } from "react";
 
type Callback = () => void;
 
const useBackListener = (callback: Callback) => {
  useEffect(() => {
    const handlePopState = (event: PopStateEvent) => {
      event.preventDefault();
      callback();
    };
 
    window.addEventListener("popstate", handlePopState);
 
    return () => {
      window.removeEventListener("popstate", handlePopState);
    };
  }, []);
};
 
export default useBackListener;