【问题标题】:How can I follow a react-router Link without it being pushed to history, but keep URL change?如何在不将其推送到历史记录的情况下关注 react-router 链接,但保持 URL 更改?
【发布时间】:2015-02-14 03:26:09
【问题描述】:

我想在单击组件链接时更改 URL,与它们当前默认更改的方式相同,但不会将它们推送到浏览器历史记录。 我发现以下内容适用于某人in this answer:

this.props.activeRouteHandler({key: "anystring"})

但我不知道应该把它放在哪里,也不知道它是否还能工作。有人可以给我一个例子吗? 谢谢

【问题讨论】:

    标签: url history react-router


    【解决方案1】:

    对于版本 ^0.13.3,我必须创建与 Link 相同的自定义 MyLink 组件,但将 this.transitionTo 替换为 this.replaceWith。

    对于版本^1.0.x API 已更改: var { History } = require('react-router') ... mixins: [ History ] ... this.history.pushState(null, '/newPathName')

    【讨论】:

      【解决方案2】:

      使用Link 的replace 属性并将其设置为true。

      这里是文档的链接:https://reactrouter.com/web/api/Link

      例如

      <Link to=[your_url] replace={true}>
        ...
      </Link>
      

      【讨论】:

        猜你喜欢
        • 2020-01-31
        • 2020-10-16
        • 2017-07-30
        • 2022-07-18
        • 2017-09-24
        相关资源
        最近更新 更多