score:1

Accepted answer
const location = useLocation();
const navigate = useNavigate();

const [open, setOpen] = useState(false);

const state = location.state as { formSubmitted?: boolean };

const handleOpenAlert = () => {
  setOpen(true);
};
const handleCloseAlert = () => {
  setOpen(false);
};

useEffect(() => {
  if (state && state.formSubmitted) {
    handleOpenAlert();
    navigate(".", { replace: true }); // redirect to current path sans state
  }
}, [navigate, state]);

Related Query

More Query from same tag