Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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 answers
Answer question

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 Report

0

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

pathname: {new_path}
about 4 years ago · Juan Pablo Isaza Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!