Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

331
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda