reactjs - react-router-dom-链接和history.push之间的区别?

标签 reactjs react-router react-router-v4 react-router-dom

我理解的 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/

相关文章:

javascript - ReactJS 身份验证不起作用,重定向到登录页面失败

reactjs - 在 React 和 React Native 的组件之间传递函数

javascript - 在 React FixedDataTable 上创建自定义表格单元格内容

react-router - 强制 React-Router <Link> 加载页面,即使我们已经在该页面上

javascript - Express 和 React Routes 如何处理来自浏览器的初始 GET 请求?

reactjs - React router 4 History.listen 永远不会触发

javascript - 箭头函数不应返回赋值 Eslint

javascript - 如何从 React 类中获取 prop 值到函数中?

reactjs - 通过路线传递 Prop

javascript - 使用 React-router v4 进行导航