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

4.7K
Visualizações
How to go back to previous route in react-router-dom v6

On early versions we can go back to previous route using history.

history.goBack()

How I can achieve that with v6 of react-router-dom?

over 4 years ago · Santiago Trujillo
5 Respostas
Responde à pergunta

0

Try this approach

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

function YourApp() {
  const navigate = useNavigate();

  return (
    <>
      <button onClick={() => navigate(-1)}>go back</button>
    </>
  );
}
over 4 years ago · Santiago Trujillo Relatório

0

in V6,

import { useNavigate } from 'react-router-dom';
 
function App() {
  const navigate = useNavigate();
 
  return (
    <>
      <button onClick={() => navigate(-2)}>Go 2 pages back</button>
      <button onClick={() => navigate(-1)}>Go back</button>
      <button onClick={() => navigate(1)}>Go forward</button>
      <button onClick={() => navigate(2)}>Go 2 pages forward</button>
    </>
  );
}
over 4 years ago · Santiago Trujillo Relatório

0

In old versions of react-router-dom there exists functions pop

you can reach them like:

const history = useHistory();
history.pop()

now in v6 you can use function useNavigate

const navigate = useNavigate();
navigate(-1) // you will go one page back
navigate(-2) // you will go two pages back
over 4 years ago · Santiago Trujillo Relatório

0

There is another way using a delta (number) in react-router Links v6 :

const BackButton = () => {
  return (
    <Link to={-1}>
      Back
    </Link>
  );
};

Unfortunately there is a type error in typescript, Link component does not accept numbers, but still it works.

over 4 years ago · Santiago Trujillo Relatório

0

Just in case anyone gets here like I did trying to navigate back OR navigate somewhere else if you can't navigate back (e.g. link opened in new tab), there doesn't seem to be any way of verifying the history with react-router in v6. However it seems you can access window.history.state which has an idx property that is zero if you're at the start of the history stack.

It's possible there are some gotchas around it that I haven't hit up against, but it's working for me:

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

// ...

const navigate = useNavigate();

// ...

if (window.history.state && window.history.state.idx > 0) {
    navigate(-1);
} else {
     navigate('/', { replace: true }
}
over 4 years ago · Santiago Trujillo 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