研究下react-route的源码找到如何正确分离route配置的方法
背景由于我希望每个子项目管理自己的route,于是想维护一个 react-route是如何配置规则的举个例子: <Router history={browserHistory}> <Route path='/' component={Layout}> <Route path='signup' component={SignupPage} /> </Route> </Router>
ps.这提供给我一个思路,如何一个组件的配置太过于多和复杂,那么可以搞出个专门用来做配置用的子组件,在渲染前读取子组件的配置,加载成自己的配置。这样的好处是大大提高了可读性。让配置成为声明而不是运算。 /** * Creates and returns a routes object from the given ReactChildren. JSX * provides a convenient way to visualize how routes in the hierarchy are * nested. * * import { Route,createRoutesFromReactChildren } from 'react-router' * * const routes = createRoutesFromReactChildren( * <Route component={App}> * <Route path="home" component={Dashboard}/> * <Route path="news" component={NewsFeed}/> * </Route> * ) * * Note: This method is automatically used when you provide <Route> children * to a <Router> component. */ function createRoutesFromReactChildren(children,parentRoute){...} 为什么不能用一个wrapper组件来隐藏子route的细节有了上面的知识后,这个问题就显而易见了,因为配置是通过读取children的 怎么实现我想要的功能最简单的方法就是写个函数来直接return route。这样既达到了分割的目的,也不会加入中间层。 为什么一开始会想用wrapper呢一开始不够了解react-route的原理,以为只要能渲染出来,效果是一样的。 SO 相关问题链接点我 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |