Render multiple components in React Router

后端 未结 6 1966
野趣味
野趣味 2021-02-05 20:54

I\'m used to application layouts with multiple yield areas, i.e. for content area and for top bar title. I\'d like to achieve something similar in React Router. For example:

相关标签:
6条回答
  • 2021-02-05 21:20

    To render multiple components you can do this:

    <Route 
        path="/EditEmployee/:id"  
        render={(props) => 
            <div>
                <NavMenu />
                <EditEmployee {...props} />
            </div> 
        } 
    />
    

    Here I'm passing parameter to specific conponent.

    0 讨论(0)
  • 2021-02-05 21:21

    Another method is within the render method of route multiple passed components can be created using react.createElement

    <Route render ={(props)=>React.createElement(Component1, {...props}},
                         React.createElement(Component2, {...props}}/> 
    
    0 讨论(0)
  • 2021-02-05 21:26

    //this is the simplest method to render multiple components and it works for me

    <Router>
      <Route path="/">
           <ListView />
           <ListTopBar /> 
      </Route>
    </Router>
    
    0 讨论(0)
  • 2021-02-05 21:33

    Instead of using div's you can use Fragments. `

    <Route path='/some-path' render={props =>
      <Fragment>
        <Child 1/>
        <Child 2/>
      </Fragment>
    } />
    

    `

    0 讨论(0)
  • 2021-02-05 21:39

    To passe multiple component you can do like this :

    <Route path="groups" components={{main: Groups, sidebar: GroupsSidebar}} />
    <Route path="users" components={{main: Users, sidebar: UsersSidebar}}>
    

    See the doc here : https://github.com/ReactTraining/react-router/blob/v3/docs/API.md#named-components

    0 讨论(0)
  • 2021-02-05 21:40

    In v4, according to the docs, you can render multiple components like this:

    <Route path='/some-path' render={() =>
      <Fragment>
        <FirstChild />
        <SecondChild />
      </Fragment>
    } />
    
    0 讨论(0)
提交回复
热议问题