title: ReactJS - Is it possible to replace item in browser history?
body: <p>I have this line of code here:</p>
  <pre><code>if(window.location.search == &quot;?mytoken=abc&quot;) {
            window.history.replaceState(null, '', window.location.pathname);
  }
  </code></pre>
  <p>What this code does is check if the query parameter mytoken exists and if it does, replace the current page without the query parameter and this works perfectly.</p>
  <p>But I did notice I did have 2 history items in my browser history, one with the query parameter and the other without the query parameter. I am looking for away to remove the item with the query parameter from the browser history. Is this possible?</p>
  <p>I found this code in my project, if it helps:</p>
  <pre><code>history.listen((newLocation, action) =&gt; {
  
        if (action === &quot;PUSH&quot;) {
          if (
            newLocation.pathname !== this.currentPathname ||
            newLocation.search !== this.currentSearch
          ) {
            // Save new location
            this.currentPathname = newLocation.pathname;
            this.currentSearch = newLocation.search;
  
            // Clone location object and push it to history
            history.push({
              pathname: newLocation.pathname,
              search: newLocation.search,
            });
          }
        } else {
          // Send user back if they try to navigate back
          history.go(1);
        }
      });
  </code></pre>
  <p>This is called every time a new page comes up.</p>