reactjs - react-router-dom - 链接和history.push之间的区别?
问题描述
我理解href和link之间的区别。
但是我想知道将链接和withrouter与history.push一起使用的区别?如果我已经访问过该页面,history.push 是否会在缓存中检索该页面?
使用链接:
<Link className="btn btn-primary" onClick={logout} to="/">
Log out
</Link>
使用历史:
constructor(props) {
super(props);
this.handleLogout = this.handleLogout.bind(this);
};
handleLogout(e) {
const { history } = this.props;
logout()
history.push("/");
}
<button type="button" onClick={this.handleLogout}>Log out</button>
解决方案
使用链接,您可以通过包装例如按钮导航到另一个“页面”,并在单击时进行重定向。大多数情况下,这是您可能想要做的。但在某些情况下,您希望以编程方式导航到另一个“页面”。例如,当您的应用程序发生与单击按钮或链接无关的更改时。
因此,您可以使用 history.push 以编程方式更改 url,而无需单击按钮或链接。=)
推荐阅读
- c# - 使用 moq 模拟使用异步回调的方法
- flutter - 如何使容器可滚动?
- eclipse - RAM 中的 ARM Eclipse 调试代码。是否可以看到源代码`
- windows - Windows 上的 PyTorch 扩展 - 为什么无法解析符号(THFloatTensor 等)?
- json - Grep 在应用正则表达式之前忽略特殊字符
- svn - 显示警告提交后挂钩失败(退出代码 255),没有输出
- terraform - 通过地形变量并行实例?
- azure - 如何在 Azure 中实现多个应用服务的高可用性
- r - windows中Rscript进程的图片名称
- javascript - Django - 如何正确地将列表从模板发送到 AJAX 以查看?