【问题标题】:Why does the React Router history length increase on refresh?为什么 React Router 的历史长度在刷新时会增加?
【发布时间】:2020-03-12 11:53:00
【问题描述】:

第一次打开app,history对象的长度(useHistoryHook返回)是3。

在每次进一步刷新时,长度都会增加。这是为什么?生产中也会发生这种情况吗?

环境:

  • create-react-app@3.2.0
  • react-router@5.1.2
  • react-router-dom@5.1.2

【问题讨论】:

    标签: reactjs react-router react-router-dom


    【解决方案1】:

    如果您在访问过其他页面的选项卡中导航到该站点,history.length 将 >1。 history.length 是联合会话历史中的位置数,而不是当前站点的会话历史。

    即使打开一个新标签并输入您的应用的 url,也意味着 history.length 为 2:空白标签 + 您的 url。如果您通过新窗口/标签上的链接直接到达该页面,则可以得到长度 1。

    这可以解释第一次打开您的应用时 >1 的长度。这并不能解释为什么您的应用会在刷新时增加长度,因此它可能与您的特定结构有关。

    【讨论】:

      【解决方案2】:
      • react-router@5.1.2
      • react-router-dom@5.1.2

      我认为您不需要同时使用 react-router-domreact-router

      仅使用 react-router-dom,因为 react-router-domreact-router-native 使用 react-router 核心

      【讨论】:

      • 本身不是我的问题的答案,但谢谢!我删除了对react-router 的依赖,应用仍然可以正确加载。
      猜你喜欢
      • 1970-01-01
      • 2021-06-26
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 2020-12-10
      • 2021-10-24
      • 2020-03-07
      相关资源
      最近更新 更多