鍍金池/ 問(wèn)答/HTML5  HTML/ react-router4 Route中的children屬性作用是什么

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

看了官方文檔,沒(méi)想明白是干什么的,和Route的render屬性有什么區(qū)別?

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

Route 渲染組件的三種方式

  • component 最常用,只有匹配 location 才會(huì)加載 component 對(duì)應(yīng)的 React組件
  • render 路由匹配函數(shù)就會(huì)調(diào)用
  • children 不管路由是否匹配都會(huì)渲染對(duì)應(yīng)組件
2018年3月26日 13:09
編輯回答
陌如玉

找到官方解釋了

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.
2018年7月29日 10:40