简体   繁体   English

React Router v4不呈现父组件

[英]React Router v4 not rendering parent components

I have my routes set up as following (index.js): 我的路由设置如下(index.js):

<BrowserRouter history={browserHistory}>
   <Switch>
      <Route exact path="/" component={RootContainer} />
      <Route exact path="/login" component={RedirectToApp(LoginPage)} />
      <Route exact path="/dashboard" component={RequiresAuth(AppContainer)} />
      <Route exact path="/dashboard/venues" component={VenuesPage} />
   </Switch>
</BrowserRouter>

In my AppContainer I have my component written as: 在我的AppContainer中,我的组件写为:

render() {
    return (
      <div className='wrapper main-app-wrapper'>
        <Header toggleSidebar={this.toggleSidebar}>
          <AuthHeaderActions />
        </Header>
         <div className='content-wrapper'>
          {this.props.children}
        </div>
        <LeftMenu />
        <Footer />
      </div>
    );
  }

When I go to the route /dashboard, the header, footer and the left menu get rendered properly. 当我转到路线/ dashboard时,页眉,页脚和左侧菜单会正确呈现。

But when I hit the route /dashboard/venues, only the content present inside the VenuesPage is rendered. 但是当我打到/ dashboard / venues路线时,仅呈现VenuesPage内部的内容。 The header, footer and leftmenu are not getting rendered. 页眉,页脚和左菜单未得到渲染。 Having a problem with nesting of routes. 路由嵌套有问题。 I am using react-router-dom - v4. 我正在使用react-router-dom-v4。

You need to put the venues Route inside dashboard 您需要将场地路线放置在仪表板中

<Route exact path="/dashboard" component={RequiresAuth(AppContainer)}>
    <Route exact path="/venues" component={VenuesPage}/>
</Route>

EDIT 编辑

NB! NB! As the comments (and the other answer) state, this is no longer valid for V4. 由于评论(和其他答案)的状态,这对于V4不再有效。

You can add Routes within your components with react-router v4 您可以使用react-router v4在组件内添加Routes

You can change the routes to 您可以将路线更改为

index.js index.js

<BrowserRouter history={browserHistory}>
   <Switch>
      <Route exact path="/" component={RootContainer} />
      <Route exact path="/login" component={RedirectToApp(LoginPage)} />
      <Route path="/dashboard" component={RequiresAuth(AppContainer)} />

   </Switch>
</BrowserRouter>

Now that you want the VenuesPage to be child of the AppContainer . 现在,您希望VenuesPage成为VenuesPageAppContainer You can add it within the AppContainer component like 您可以将其添加到AppContainer组件中,例如

render() {
    return (
      <div className='wrapper main-app-wrapper'>
        <Header toggleSidebar={this.toggleSidebar}>
          <AuthHeaderActions />
        </Header>
         <div className='content-wrapper'>
          {this.props.children}
          <Route exact path="/dashboard/venues" component={VenuesPage} />
        </div>
        <LeftMenu />
        <Footer />
      </div>
    );
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM