Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 3680

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T04:58:09+00:00 2024-11-26T04:58:09+00:00

How to Confirm Leaving a Page in Ionic React with React Router

  • 61k

Have you ever been in the middle of an important task on a page, like filling out a form, and accidentally left and lost all your work? That bites!

And it's a bad experience for your users, especially on mobile.

A typical technique is to confirm with the user if they do want to leave the page with a confirmation dialog. In this post, I'll show you how to do so in an Ionic React application, and how you can customize the confirmation UI to fit your particular app. Let's go!

Using React Router's Prompt Component

An Ionic React application uses React Router for all of its navigation, and, fortunately, React Router has good support for prompting the user on navigation with their Prompt component. With Prompt, a confirmation box pops up asking the user if they want to leave the page. If they click yes, the navigation takes place, and if they click no, they are left on the current page.

The Prompt component takes two props, a message to display, and a when boolean to activate it.

Here is a simple IonPage with a form that utilizes the Prompt component:

const Tab1: React.FC = () => {   const [text, setText] = useState('');    useIonViewWillLeave(() => {     setText('');   });   return (     <IonPage>       <IonHeader>         <IonToolbar>           <IonTitle>Tab 1</IonTitle>         </IonToolbar>       </IonHeader>       <IonContent fullscreen>         <IonInput           value={text}           placeholder="Name"           onIonChange={(e) => {             setText(e.detail.value!);           }}         ></IonInput>          <IonButton           expand="block"           onClick={() => {             setText('');           }}         >           Submit         </IonButton>         <Prompt           when={!!text}           message="You have unsaved changes, are you sure you want to leave?"         />       </IonContent>     </IonPage>   ); }; 
Enter fullscreen mode Exit fullscreen mode

Prompt is imported from 'react-router'

To determine if the form is “dirty” (if the form has been modified), we check to see if the IonInput has a value or not. This is a simple method, and you will probably need to expand on the concept in your app. Many form libraries provide a way to determine if the form has been modified as well.

In the useIonViewWillLeave hook, when a user leaves the page, we set the value of text back to a blank string to “reset” the form. This keeps the prompt from showing on other pages.

Now, if we try to leave the form, by say, accidentally tapping one of the other tab buttons, we get a nice confirmation:

Image of form with standard browser confirm dialog

This is fairly functional as is. The confirm dialog on mobile devices looks decent, but if you want to customize the UI, we will dive into that next.

Customizing the Confirm UI

Instead of showing the built-in confirm dialog that comes with the browser, you might want to display something a bit more custom to match the look and feel of your app. To change it up, we will use an IonAlert with custom buttons for the confirmation.

React Router provides a way to tie into the process by passing in a getUserConfirmation prop when setting up the router. In an Ionic app, we use IonReactRouter and we can pass this prop here and the router will, in turn, pass the prop back down to the underlying ReactRouter.

This prop excepts a function that gets passed in the message to display, as well as a callback. The callback takes a boolean parameter to indicate if the route navigation should happen or not. We'll add it to the main App.tsx page, where the routing is set up:

<IonReactRouter   getUserConfirmation={(message, callback) => {    }} > 
Enter fullscreen mode Exit fullscreen mode

When getUserConfirmation is called, we want to display an IonAlert overlay with the message that was passed in. We will use a state variable to store the message. Also, we utilize a ref object to hold a reference to the callback that will be used in the alert:

const [leaveConfirmMessage, setLeaveConfirmMessage] = useState<string>(); const confirmCallback = useRef<(ok: boolean) => void>(); 
Enter fullscreen mode Exit fullscreen mode

And to set them in the getUserConfirmation:

<IonReactRouter   getUserConfirmation={(message, callback) => {     setLeaveConfirmMessage(message);     confirmCallback.current = callback;   }} > 
Enter fullscreen mode Exit fullscreen mode

Next, we add the IonAlert towards the bottom of the page, but before the closing </IonReactRouter>:

<IonAlert   isOpen={!!leaveConfirmMessage}   message={leaveConfirmMessage}   buttons={[     {       text: "No",       handler: () => {         confirmCallback.current && confirmCallback.current(false);       },     },     {       text: "Yes",       handler: () => {         confirmCallback.current && confirmCallback.current(true);       },     },   ]}   onDidDismiss={() => setLeaveConfirmMessage(undefined)} /> 
Enter fullscreen mode Exit fullscreen mode

To determine if the alert is shown, we check if the confirm message has a value, and then set the message back to undefined when the alert is dismissed. In the buttons, we use the ref we set up to invoke the callback function, passing true when the user clicks “Yes”, and false when “No” is clicked.

And that's it! We use the Prompt component as we did before in any page we want to use this custom UI. No changes are needed in our form page.

Image of form with custom dialog using IonAlert

Using the Browsers beforeUnload Event

One last thing we need to cover, which is what happens when the user tries to move away from the page outside of our app, like via the back button or changing the URL manually?

We can use the browser's beforeUnload event for this, though it is not customizable, browser support is limited, and it requires a bit more code. However, setting it up will give our users whose browsers support it extra protection if they, say, accidentally refresh their page.

Back in the page with the form, we will add a useEffect hook to monitor the text state. We set the onbeforeunload method on the window object to a function that returns true when the text has a value, and when the effect changes, set it null to turn it back off:

useEffect(() => {   if (text) {     window.onbeforeunload = () => true;   }   return () => {     window.onbeforeunload = null;   }; }, [text]); 
Enter fullscreen mode Exit fullscreen mode

This could also be abstracted into its own component or hook for reuse.

Wrapping up

Adding some safeguards to prevent your users from accidentally leaving a page while they are performing an important task is, thankfully, pretty straight-forward in an Ionic React app thanks to the built-in support in React Router.

I put together a demo app you can take a look at, feel free to check it out. Also, hit me up on twitter @elylucas or in the comments below if you have any questions.

Happy Coding!

ionicjavascriptreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers
  • Author

    Refactoring for Efficiency: Tackling Performance Issues in Data-Heavy Pages

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.