How do you handle navigation prompts or confirmations before leaving a route in React Router v5

0 votes
Can i know How do you handle navigation prompts or confirmations before leaving a route in React Router v5?
Apr 21, 2025 in Node-js by Ashutosh
• 33,350 points
521 views

1 answer to this question.

0 votes

Use the <Prompt> component to warn users before leaving a route.

Example:

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

import { useState } from 'react';

function MyForm() {

  const [isBlocking, setIsBlocking] = useState(false);

  return (

    <>

      <Prompt

        when={isBlocking}

        message="Are you sure you want to leave this page?"

      />

      <input

        onChange={(e) => setIsBlocking(e.target.value.length > 0)}

        placeholder="Type something..."

      />

    </>

  );

}

answered Apr 21, 2025 by anonymous

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How do you pass and use URL parameters in a React-Router route?

Passing and Using URL Parameters in React ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
530 views
0 votes
1 answer

How do you redirect a user to a different route in React Router?

Redirecting a User in React Router 1. Using ...READ MORE

answered Feb 23, 2025 in Node-js by Kavya
507 views
0 votes
1 answer

How can you programmatically navigate to a different route in React Router v5?

In React Router v5, you can programmatically ...READ MORE

answered Apr 17, 2025 in Node-js by anonymous
568 views
0 votes
1 answer
0 votes
1 answer
0 votes
0 answers
0 votes
1 answer

How do you handle scroll restoration in a React-Router app?

By default, React Router does not restore ...READ MORE

answered Feb 24, 2025 in Node-js by Kavya
764 views
0 votes
1 answer

How do you handle a large amount of data in React?

Handling Large Amounts of Data in React To ...READ MORE

answered Feb 24, 2025 in Node-js by Kavya
680 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP