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
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
Share Improve this question asked Mar 16, 2018 at 6:49 FelixFelix 5,62614 gold badges80 silver badges173 bronze badges 2- check this question stackoverflow./questions/44121069/… – Suresh Ponnukalai Commented Mar 16, 2018 at 6: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 Commented Jul 12, 2018 at 7:38
3 Answers
Reset to default 4I think you are looking for this
this.props.history.push({
pathname: "URL"
})
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
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)