react-router4 Route中的children属性作用是什么

发布于 2022-09-07 03:24:05 字数 423 浏览 8 评论 0

看了官方文档,没想明白是干什么的,和Route的render属性有什么区别?

const OldSchoolMenuLink = ({ label, to, activeOnlyWhenExact }) => (
    <Route path={to} exact={activeOnlyWhenExact} children={({match}) => (
        <div className={match ? 'active' : ''}>
            {match ? '>' : ''}<Link to={to}>{label}</Link>
        </div>
    )}></Route>
);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水水月牙 2022-09-14 03:24:05

Route 渲染组件的三种方式

  • component 最常用,只有匹配 location 才会加载 component 对应的 React组件
  • render 路由匹配函数就会调用
  • children 不管路由是否匹配都会渲染对应组件
亢潮 2022-09-14 03:24:05

找到官方解释了

Sometimes you need to render whether the path matches the location or not. In these cases, you can use the function children prop. It works exactly like render except that it gets called whether there is a match or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文