Render multiple components in React Router
Asked Answered
H

12

15

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:

<Router>
  <Route path="/" component = { AppLayout }>
    <Route path="list"
           component = { ListView }
           topBarComponent = { ListTopBar }/>
  </Route>
</Router>

AppLayout:

<div className="appLayout box">
  <div className="appLayout topBar">
    { -- display ListTopBar here -- }
  </div>
  <div className="appLayout content">
    { -- display ListView here -- }
  </div>     
</div>

Both child components should receive the same props.

How can I approach this?

Hurlyburly answered 20/5, 2016 at 9:28 Comment(0)
P
17

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

Phagy answered 20/5, 2016 at 9:39 Comment(1)
Link link is broken. It should be: github.com/ReactTraining/react-router/blob/v3/docs/…Salenasalene
L
17

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

<Route path='/some-path' render={() =>
  <Fragment>
    <FirstChild />
    <SecondChild />
  </Fragment>
} />
Lawlor answered 16/1, 2018 at 2:13 Comment(1)
Shouldn't the prop for this be path='/popular' instead of to='/popular' ?Goodtempered
M
7

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

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

`

Myna answered 18/10, 2019 at 23:55 Comment(0)
B
5

You can also use Array in latest versions of React-router-dom;

<Route path="groups" element={[<Component1/>,<Component2/>]} />

Will work just fine.

Bertberta answered 15/9, 2022 at 18:49 Comment(0)
U
2

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.

Unsex answered 20/11, 2019 at 10:29 Comment(0)
C
2

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

<Router>
  <Route path="/">
       <ListView />
       <ListTopBar /> 
  </Route>
</Router>
Charmion answered 20/10, 2020 at 9:12 Comment(0)
B
2

 <Route path='/' element={<><Header /> <Home /></>} />

This worked for me in the latest react router dom v6

British answered 26/3, 2022 at 10:8 Comment(0)
W
1

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}}/> 
Wessels answered 26/11, 2018 at 13:0 Comment(0)
F
0

What worked for me was to wrap the multiple components in a <Fragment> or a <div> as a parent element.


  return (
    < Router>
      <div className="App" >
        <Routes>
          <Route path='/'
            element={
              <Fragment>
                  < NavBar />
                  < NewsLetterCard />
                  < TestimonialsCard />
                  < ServicesCard />
                  < ContactsCard />
                  < Footer />
              </Fragment>
            }
          />
        </Routes>
      </div>
    </Router>
  );
}
Fuddyduddy answered 23/3, 2022 at 13:9 Comment(0)
M
0

For v6, where you are using Routes instead of Switch to render your components. This works:

<Router>
<Routes>
<Route path='/' element={<><Child1/> <Child2/></>} />
</Routes>
</Router>

But for v5, this works:

<Router>
<Switch>
<Route path="/">
   <Child1/>
   <Child2/> 
</Route>
Micelle answered 5/9, 2022 at 0:8 Comment(0)
B
0

This seem to work for me, I needed to add canvas animation component as background under homepage ("/") only:

import Page from 'pages/Page';
import Blog from 'pages/Blog';
import Post from 'pages/Post';
import Category from 'pages/Category';
import CanvasParticles from 'components/canvas/CanvasParticles';

...

<Routes>
  {['/', '/:slug'].map((path, index) => {
  return path === '/' ? (
   <Route
    exact
    path={path}
    element={[<CanvasParticles />, <Page />]}
    key={index}
   />
  ):(
   <Route path={path} element={<Page />} key={index} />
  );
 })}
 <Route exact path="/blog" element={<Blog />}></Route>
 <Route path="/blog/:slug" element={<Post />}></Route>
 <Route path="/category/:slug" element={<Category />}></Route>
</Routes>
Brookhouse answered 10/12, 2022 at 17:23 Comment(0)
G
-1

click on this you can view a image v6 feature : this is the simplest method to render multiple components and it works for me

Main Concept is to you should wrap with element <Route path="/" element={<> </>} or wrap with in Fragment <Route path="/" element={ }

Garth answered 11/7, 2022 at 13:54 Comment(2)
meta.#286051Buffoon
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewWrecker

© 2022 - 2024 — McMap. All rights reserved.