2016-08-30 16 views
1

我正在使用React,Redux和[email protected]。我试图找出如何在特定查询参数传递时装入顶级组件。这可能吗?react-router可能根据查询参数装载组件?

所以我现有的路线是这样的:

export default (
    <Route name="root" path="/" component={RootComponent}> 
    <Route name="routeOne" path="a/route/:slug(/)" component={RouteOneComponent} /> 
    <Route name="routeTwo" path="a/route/:slug/:index" component={RouteTwoComponent} /> 
    </Route> 
); 

现在,我需要重构routeTwo使,而不是被安装在a/route/slug/1,它仅仅将在a/route/slug?i=1

这是可能的吗?我似乎无法在文档中找到任何关于此的信息。似乎只有实际的路线可以有一个组件,而查询作为道具传递......但不幸的是,这不会为我的用例工作。 :(任何想法?

回答

0

是的,你可以使用getComponent这个

<Route path="a/route/:slug" 
    getComponent={(nextState, cb) => { 
     if (nextState.location.query[i] === '1') { 
      cb(null, RouteTwoComponent); 
     } else { 
      cb(null, RouteOneComponent); 
     } 
    }} /> 

虽然我觉得一般人会在基于道具的渲染方法处理这些分歧。

+0

谢谢!这其实伟大工程因为我的需要,但是我很想听到更多关于你提到的“正常”方式(可能是为了遵循最佳实践),你介意说明一下吗?当然我会将你标记为正确的。 – Prefix