11

Migrating a codebase to react-router v6 specifically v6.2.1 and running into some issues when trying to use <Prompt /> component or usePrompt, turns out they are not available in this version of react-router. Or am I wrong? If anyone have run in to the same issue or if you know a way around this issue, your help would be appreciated.

Abdulhamid
  • 123
  • 1
  • 1
  • 6
  • It's not currently included, though I believe there are plans to reintroduce them later. – Drew Reese Jan 07 '22 at 05:54
  • check this: https://stackoverflow.com/questions/62792342/in-react-router-v6-how-to-check-form-is-dirty-before-leaving-page-route – Kais Jan 07 '22 at 05:57

5 Answers5

10

Prompt is currently not supported in v6

You will have to create your own component that performs the check and displays the prompt.

Josh Ackland
  • 603
  • 4
  • 19
5

As it says in the documentation:

<Prompt> from v5 (along with usePrompt and useBlocker from the v6 betas) are not included in the current released version of v6. We decided we'd rather ship with what we have than take even more time to nail down a feature that isn't fully baked. We will absolutely be working on adding this back in to v6 at some point in the near future, but not for our first stable release of 6.x.

So you have two options:

  1. Create your own component and handle the logic there.
  2. Downgrade react-router-dom to v5.

Source: https://reactrouter.com/docs/en/v6/upgrading/v5#prompt-is-not-currently-supported

Peter Palmer
  • 726
  • 11
  • 18
0

React router v6 doesn't support or usePrompt.

You can use this simple hook to block navigation based on certain state changes or criteria.

Usage

import { useState } from 'react';
import useNavigationPrompt from 'use-navigation-prompt';
    
function App() {
    const [isNavigationBlocked, setIsNavigationBlocked] = useState(false);
    useNavigationPrompt(isNavigationBlocked);
    
    return (
        <div>
            Navigation is: { !isNavigationBlocked ? "not" : "" } Blocked
            <button 
                onClick={() => setIsNavigationBlocked(prevIsNavigationBlocked => !prevIsNavigationBlocked)}
            >
                Toggle
            </button>
        </div>
    );
}
    
export default App;
mubashir
  • 21
  • 4
0

You can use the beta supported unstable_usePrompt:


import { unstable_usePrompt } from 'react-router-dom';

unstable_usePrompt({
  message: "Are you sure you want to leave?",
  when: true
})

And here is a full example of <Prompt /> in react router v6 using unstable_useBlocker that has all the features

-5

Prompt is currently not supported in react-router-dom 6

Prompt not supported at the moment

Resort to the following:

  1. Writing your own prompt component
  2. installing react-router-dom 5 and using prompt.
Promise Ihunna
  • 293
  • 3
  • 8