Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

332
Vistas
How to use the browser back button to close a Modal in react-router v6

I have a simple modal that displays a larger version of a picture when a React component clicks a thumbnail. I would like the modal to close when the back button is pressed in the browser instead of the default behavior. I have seen this question answered using browserHistory(), but that has been replaced with useNavigate() in v6, and I am not sure how to implement this.

Edit: I implemented the below answer, but I had to nest the route from the modal within the parent compnenet's route and then add an in the parent component. If you simply make two seperate routes it will unmount the parent component when the modal compontent mounts.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Yes you can... here's one (of the many) approach that you can take. First thing first, you have to use url to determine if modal is open or not.

For example

  • /photos show photo list page
  • /photos/:id show photo's detail modal

And you can do something like this

<Routes>
  <Route path="/photos" element={<PhotoList />} />
  <Route path="/photos/:id" element={<PhotoDetailModal />} />
</Routes>

Notice that I didn't put "exact" in /photos Route, because I expect the photos page to still show underneath the modal.

And in photo list page, you can open a modal by just change the url

// Your Photo's list

<Link to={`/photos/${photo.id}`}>
  <img src="your photo" />
</Link>

Now the browser history will work as your friend, and the back button will close modal (since it changes url from /photos/1 to /photos

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda