4

Hi in my reactJS application I use this

this.props.history.push("URL");

to redirect to another page or to the same page when I want to reload the page.

But I got this error:

Hash history cannot PUSH the same path; a new entry will not be added to the history stack

how can I solve this issue?

thanks in advance

Felix
  • 5,452
  • 12
  • 68
  • 163
  • check this question https://stackoverflow.com/questions/44121069/how-to-pass-params-with-history-push-in-react-router-v4 – Suresh Ponnukalai Mar 16 '18 at 06:52
  • Is it warning or an error? Seems like warning to me. If I'm not mistaken, the warning is shown only on dev mode but not in production. Just check if new location is the same as current one `const {history} = this.props; if(history.pathname !== newPath) history.push(newPath);` – Eduard Jacko Jul 12 '18 at 07:38

3 Answers3

4

I think you are looking for this

   this.props.history.push({
         pathname: "URL"
      })
Adnan
  • 1,589
  • 11
  • 17
2

You can use

if (history.location.pathname !== somePath) history.push(somePath);

Here somePath could be any url. In react-router-4, pathname can be accessed like

  this.props.history.location.pathname
Ashu
  • 51
  • 3
0

You can use replace prop. When replace prop is true, clicking the link will replace the current entry in the history stack instead of adding a new one. To get current pathname use props.location.

<Link replace={to === location.pathname} to={to}><Button>{to}</Button></Link>

You can create higher order function and wrap your <Link> in it. So you can use it as

<NavLink to="{to}"></NavLink> 

Here is tutorial and example (my blog)

Filip Molcik
  • 1,011
  • 10
  • 12