0

I have a react app with react-router. I have multiple routes, and some of them are shown in a modal window. If I close the modal, I want to send back the user to the previous page, where modal was not active yet.

To know, if I have modal open, I use the state modal:true on the location. I know I could use simply history.goBack(), but I have the scenario when I'm on /login and the user goes from there to /register, but both routes show a modal. In this case, if the user clicks the close button on the modal, they will go back first to /login.

I tried to do something like this, but the history object is not updated with the new state.

goBack = () => {
  while(true) {
   history.goBack();
   if (!history.location.state || !history.location.state.modal) {
     break;
   }
 }
}
Tholle
  • 83,208
  • 13
  • 152
  • 148
ghostika
  • 1,353
  • 1
  • 9
  • 19

0 Answers0