【问题标题】:react-router-dom pass the Route props (match, location and history) to the child element when rendering using the child elements Not Workingreact-router-dom 在使用子元素渲染时将 Route 道具(匹配、位置和历史记录)传递给子元素
【发布时间】:2020-07-26 00:17:12
【问题描述】:

The docs say:

Route render methods 推荐的渲染方法 with a 是使用子元素,如上图。有, 但是,您可以使用其他一些方法来渲染具有 .这些主要用于支持已构建的应用程序 在引入钩子之前使用早期版本的路由器。 功能你应该 在给定的 .看他们的解释 下面来了解它们之间的区别。路线道具全部 三个渲染方法将通过相同的三个路由道具匹配 位置记录

但是如果我按照推荐的方式渲染:

<Router>
      <Route exact path="/">
        <Home />
      </Route>
</Router>

Component Home 真的可以访问 Route 道具(位置、匹配和历史记录)吗?

如果是这样,这些 props 是如何传递或到达 Home 组件的?

【问题讨论】:

    标签: react-router react-router-dom


    【解决方案1】:

    我刚刚发现了 react-router 钩子:

    https://reacttraining.com/react-router/web/api/Hooks

    所以它就像导入 useLocation 钩子并使用它一样简单:

    let location = useLocation();
    

    对不起!

    【讨论】:

      猜你喜欢
      • 2018-12-19
      • 1970-01-01
      • 2023-03-30
      • 2019-05-02
      • 2016-04-29
      • 2016-03-19
      • 2021-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多