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

看了官方文档,没想明白是干什么的,和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>
);
阅读 6.1k
2 个回答

Route 渲染组件的三种方式

  • component 最常用,只有匹配 location 才会加载 component 对应的 React组件
  • render 路由匹配函数就会调用
  • children 不管路由是否匹配都会渲染对应组件

找到官方解释了

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.
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题