Intercept Route Changes with React Router v4 Prompt Component

Share this video with your friends

Send Tweet

If a user has entered some input, or the current Route is in a “dirty” state and we want to confirm that data will be lost, React Router v4 provides a Prompt component to interrupt the Route transition and ask the user a question.

Gilad
Gilad
~ 7 years ago

Cool example. How come you didn't have to bind the setDirty method to this?

Konekoya
Konekoya
~ 6 years ago

Gilad, Joe is using arrow function, so you don't have to manually bind the handler in the class constructor.

Radu Paraschivx
Radu Paraschivx
~ 6 years ago

Hello, is there any way to replicate the functionality from the Angular router to intercept the route change and only allow the transition only when data is loaded? On the subject, is there any way to block the router transition without the use of js blocking operations (alert/prompt)?