reactJS历史记录推送返回错误

时间:2018-03-16 06:49:09

标签: javascript reactjs

嗨,在我的reactJS应用程序中,我使用了这个

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

当我想重新加载页面时,重定向到另一个页面或同一页面。

但我收到了这个错误:

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

我该如何解决这个问题?

提前致谢

3 个答案:

答案 0 :(得分:2)

我认为您正在寻找

   this.props.history.push({
         pathname: "URL"
      })

答案 1 :(得分:1)

您可以使用replace道具。如果replace prop为true,则单击链接将替换历史记录堆栈中的当前条目,而不是添加新条目。要获取当前路径名,请使用props.location

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

您可以创建高阶函数并将<Link>包装在其中。因此,您可以将其用作

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

Here is tutorial and example (my blog)

答案 2 :(得分:1)

您可以使用

-19800

此处somePath可以是任何网址。 在react-router-4中,路径名可以像访问

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