score:0

 const [modalVisible, setModalVisible] = useState(false);

  const [lastLocation, setLastLocation] = useState<Location | null>(null);

  const [confirmedNavigation, setConfirmedNavigation] = useState(false);

  const history = useHistory();

  const { pathname } = useLocation();



  const closeModal = (): void => {

    setModalVisible(false);

  };

  const handleBlockedNavigation = (nextLocation: Location): boolean => {

    if (!confirmedNavigation && pathname) {

      setModalVisible(true);

      setLastLocation(nextLocation);

      return false;

    }

    return true;

  };

  const handleConfirmNavigationClick = (): void => {

    setModalVisible(false);

    setConfirmedNavigation(true);

  };

  useEffect(() => {

    if (confirmedNavigation && lastLocation) {

      // Navigate to the previous blocked location with your navigate function

      history.push(lastLocation.pathname);

    }

  }, [confirmedNavigation, history, lastLocation]);

More Query from same tag