首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用React-Router-4包装孩子的应用程序路由

使用React-Router-4包装孩子的应用程序路由
EN

Stack Overflow用户
提问于 2017-04-12 19:30:31
回答 1查看 4K关注 0票数 1

当使用react-router (版本3)时,我能够创建嵌套的路由,因为包装器组件接收子组件。通过这种方式,我能够对根组件使用“全局”缩减程序,因为每个子组件都有自己的缩减程序:

代码语言:javascript
复制
<Provider store={store}>
    <Router key={Math.random()} history={browserHistory}>
        <Route path="/" component={App}>
            <IndexRoute component={MainPage}/>
            <Route path="mainPage" component={MainPage}/>
            <Route path="secPage" component={SecPage}/>
            <Route path="*" component={MainPage}/>
        </Route>
    </Router>
</Provider>

在根组件内部:

代码语言:javascript
复制
render() {
        return (
        <div className="app-wrapper">
            {this.props.children}
        </div>
    );
}

我将路由器升级为使用版本4:

代码语言:javascript
复制
<Provider store={store}>
    <Router history={history}>
        <div>
            <Route exact path="/" component={MainPage}/>
            <Route path="secPage" component={SecPage}/>
            <Route path="mainPage" component={MainPage}/>
        </div>
    </Router>
</Provider>

正如你所看到的-我的路由现在是“扁平的”,所以我不能真正使用根组件,因此需要为每个组件使用"globalReducer“。

我怎样才能使用和以前一样的方法?或者至少是接近它的东西?

EN

回答 1

Stack Overflow用户

发布于 2017-04-12 19:43:34

我刚刚找到了一个解决方案--用根组件包装子路由:

代码语言:javascript
复制
<Provider store={store}>
    <Router history={history}>
        <App>
            <Route exact path="/" component={MainPage}/>
            <Route path="mainPage" component={MainPage}/>
            <Route path="secPage" component={SecPage}/>
        </App>
    </Router>
</Provider>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43368308

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档