首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自react-router-redux的ConnectedRouter不支持子道具

来自react-router-redux的ConnectedRouter不支持子道具
EN

Stack Overflow用户
提问于 2017-08-01 20:15:18
回答 2查看 1.9K关注 0票数 0

代码语言:javascript
复制
<Router history={browserHistory} children={this.props.routes} />

在React路由器v4中受支持,

使用react-router-redux ConnectedRouter不支持它

例如

代码语言:javascript
复制
<ConnectedRouter
    history={this.props.history}
    children={this.props.routes}
/>

是否有一种方法可以将路径作为子道具传递,因为这是使用对象构建路径的简单方法

谢谢

EN

回答 2

Stack Overflow用户

发布于 2017-08-01 20:23:50

您应该在内部创建您的应用程序组件

代码语言:javascript
复制
<ConnectedRouter history={history}>
  <div>
    <App />
  </div>
</ConnectedRouter>

您的应用程序组件应该使用switch路由元素处理子组件,如下所示:

代码语言:javascript
复制
<Switch> {/* redirect to the first correct statemet */}
   <Route path={NavigationPath.Home} component={Home} />
   <Route path={NavigationPath.About} component={About} />
   <Route path={NavigationPath.Summary} component={Summary} />
   <Route path={NavigationPath.Account} component={Login} />
   <Redirect from="/" to={NavigationPath.Home} />
   <Route component={NotFound} />
</Switch>

比子组件具有更多路由

票数 1
EN

Stack Overflow用户

发布于 2017-08-01 20:32:45

您可以像在文档中那样将对象映射到组件:

代码语言:javascript
复制
////////////////////////////////////////////////////////////
// then our route config
const routes = [
  { path: '/sandwiches',
    component: Sandwiches
  },
  { path: '/tacos',
    component: Tacos,
    routes: [
      { path: '/tacos/bus',
        component: Bus
      },
      { path: '/tacos/cart',
        component: Cart
      }
    ]
  }
]

// wrap <Route> and use this everywhere instead, then when
// sub routes are added to any route it'll work
const RouteWithSubRoutes = (route) => (
  <Route path={route.path} render={props => (
    // pass the sub-routes down to keep nesting
    <route.component {...props} routes={route.routes}/>
  )}/>
)

const RouteConfigExample = () => (
  <Router>
    <div>
      <ul>
        <li><Link to="/tacos">Tacos</Link></li>
        <li><Link to="/sandwiches">Sandwiches</Link></li>
      </ul>

      {routes.map((route, i) => (
        <RouteWithSubRoutes key={i} {...route}/>
      ))}
    </div>
  </Router>
)

从这里:https://reacttraining.com/react-router/web/example/route-config

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45437534

复制
相关文章

相似问题

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