我理解的 href 和 link 之间的区别。
但是我想知道使用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,而无需单击按钮或链接。 =)
关于reactjs - react-router-dom-链接和history.push之间的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53363557/