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

228
Visualizações
In React, replacing URL with an identical value causes infinite loop
      if (new_path !== window.location.pathname + window.location.search) {
        history.replace({
          pathname: new_path,
          search: "",
        });
      }

EDITED:

new_path is just a string. Without the if statement, the above code will cause infinite loop.

Is that expected behavior?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

It will only cause an infinite loop if

new_path is different than window.location.pathname + window.location.search.

If you want to check its values, try to preserve logs on chrome developer tools and then start your app. That way you can see why it is generating an infinite loop.

about 4 years ago · Juan Pablo Isaza Relatório

0

If new_path is a state in your app it should be inside {}

pathname: {new_path}
about 4 years ago · Juan Pablo Isaza Relatório

0

The "if" condition is the wrong one.

if (new_path !== window.location.pathname + window.location.search) {}

This will be true always and history.replace will run again.

so the solution is

if (new_path[<your key>] !== window.location[<your key>]) {}

If you meant to be the new_path is a string, you need to check what is the new_path value

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