Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can we handle router Prompt before navigation to different path? #464

Open
ThaDeveloper opened this issue Apr 29, 2021 · 3 comments
Open

Comments

@ThaDeveloper
Copy link

ThaDeveloper commented Apr 29, 2021

Something similar to this https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/Prompt.md.
The need for the user to confirm before going back the page or clicking a different route if they have unsaved changes.

Currently, this is not possible as it returns Error: Invariant failed: You should not use <Prompt> outside a <Router>

@alexdunne
Copy link

@ThaDeveloper

It looks as though Prompt or something similar isn't planned/intended to be added to reach-router.

https://reach.tech/router/credits

Trade-offs (mostly compared to React Router)

No history blocking. I found that the only use-case I had was preventing the user from navigating away from a half-filled out form. Not only is it pretty easy to just save the form state to session storage and bring it back when they return, but history blocking doesn’t happen when you navigate away from the app (say to another domain). This kept me from actually using history blocking and always opting to save the form state to session storage.

@alsan1997
Copy link

alsan1997 commented Oct 27, 2021

i think this should be the workaround

import { useLocation, globalHistory, useNavigate } from '@reach/router'

  const curLocation = useLocation()
  const navigate = useNavigate();
  useEffect(() => {
    return globalHistory.listen(({ action, location }) => {
      if ((action === 'PUSH' && !location.pathname.includes('${your_current_path}'))|| action === 'POP') {
        if(!window.confirm("Are you sure you want to exit video screening?")){
          navigate(`${curLocation.pathname}`)
        }
      }
    
    })
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

@EmmanuelMr18
Copy link

With what actions will be 'PUSH' and when it will be 'POP'. Or where can I read about how action works?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants