1

I am creating a Project where I have multiple component in /. So I wrapped them into a Route with Fragment. Now from those child component one is parent of another some component.

How can I set them to Route?

Infomation

  1. Browser Router are in it's parent component.
  2. I can see all 4 Dashboard, Hero, List, Footer in Homepage.
  3. I am getting nothing after List
  4. The router will be like /li/:liId/:olId means /li/1/1. I can view only /

Code

<Routes>
     <Route path='/' element={
        <>
          <Dashboard />
          <Hero />
          <List>
             <Route path="/li" element={<Hi />}>
                <Route path="/:liId" element={<Hi />} >
                    <Route path="/:olId" element={<Hi />} />
                    <Route path="/i" element={<Info />} />
                    <Route path='/d' element={<Details />} />
                 </Route>
              </Route>
              <Route path='*' element={<Error />} />
           </List>
           <Footer />
        </>
       }>
      </Route>
 </Routes>

Solved with

<Routes>
  <Route
    path='/*'
    element={(
      <>
        <Dashboard />
        <Hero />
        <List>
          <Outlet />
        </List>
        <Footer />
      </>
    )}
  >
    <Route path="/li/:liId" element={<Hi />}>
      <Route path="/li/:liId/:olId" element={<Hi />}/>
        <Route path="/li/:liId/i" element={<Info />} />
        <Route path="*" element={<Error />} />
    </Route>
  </Route>
</Routes>
mrhrifat
  • 161
  • 1
  • 2
  • 9

1 Answers1

1

The Route components that are rendering nested routes should specify the trailing "/*" wildcard character to allow sub-route path matching.

Route components should also only have either the Routes or another Route component as parent.

You can either wrap the nested routes in a Routes component:

<Routes>
  <Route
    path='/*'
    element={(
      <>
        <Dashboard />
        <Hero />
        <List>
          <Routes>
            <Route path="li" element={<Hi />}>
              <Route path=":liId" element={<Hi />}>
                <Route path=":olId" element={<Hi />} />
                <Route path="i" element={<Info />} />
                <Route path="d" element={<Details />} />
              </Route>
            </Route>
            <Route path='*' element={<Error />} />
          </Routes>
        </List>
        <Footer />
      </>
    )}
  />
</Routes>

Edit how-to-render-multiple-component-which-parent-are-wrapped-into-another-multiple v1

Or render an Outlet for the nested Route components to be rendered into.

<Routes>
  <Route
    path='/'
    element={(
      <>
        <Dashboard />
        <Hero />
        <List>
          <Outlet /> // <-- nested `Route`s render out here
        </List>
        <Footer />
      </>
    )}
  >
    <Route path="li" element={<Hi />}>
      <Route path=":liId" element={<Hi />}>
        <Route path=":olId" element={<Hi />} />
        <Route path="i" element={<Info />} />
        <Route path="d" element={<Details />} />
      </Route>
    </Route>
    <Route path='*' element={<Error />} />
  </Route>
</Routes>

Edit how-to-render-multiple-component-which-parent-are-wrapped-into-another-multiple v2

Drew Reese
  • 165,259
  • 14
  • 153
  • 181
  • I'm looking for you. Thankfully you answered my question. First of all I try with different style with your 1st method. It doesn't work but 2nd one is working. Thanks. Will you please review this [question](https://stackoverflow.com/questions/71082165/how-to-prevent-duplicate-modal-popup-not-scrolling-to-last-element-in-reactjs) – mrhrifat Feb 11 '22 at 15:00
  • @mrhrifat Looks like that other question is resolved. I didn't test either implementation, but it sounds like the second version worked for you? – Drew Reese Feb 11 '22 at 17:35
  • Yes. I can't get any component rendered with 1st suggestion. Although I test many times with changes. But then I follow 2nd one & it's work. But I need to create path in details. Like `I have edit & added to my question with Solved With` Section. Please do check. – mrhrifat Feb 12 '22 at 05:05
  • @mrhrifat I see. Both solutions appear to work, though I did have a typo in the first example, the `Error` route needed to be move up into the `Routes` component to fix an issue with that implementation. I also tweaked the paths. I updated my answer and added running codesandbox demos for each. – Drew Reese Feb 12 '22 at 07:30
  • Thank you so much. After finishing all components I will try with your solved implementation. For now please will you review this [question](https://stackoverflow.com/questions/71100333/how-to-get-value-of-a-component-to-another-component) – mrhrifat Feb 13 '22 at 11:50
  • this router v6 is so shitty.. it brings nothing usefull, it complicates routing. In v5 you could use components as a children of route, eg creating auth wrapper, now you need to go through hoops to achive this. Code becomes ugly – Paweł Andruszków Sep 22 '22 at 13:36
  • @PawełAndruszków That is personal opinion. In my opinion RRDv6 is vastly superior to RRDv5. It does so many things so much better than v5 that I don't miss any of the functionality that was removed. React is moving in a certain direction and if you read the `RRDv6` FAQ you'll see that RRD is traveling in the same direction. If you want a good comparison of route protection between v5 and v6 see my answer [here](/a/66289280/8690857), it's not *that drastically* different. If anything I'm assuming it's the `Outlet` component that is confusing you. I also didn't like it so much at first. – Drew Reese Sep 22 '22 at 15:36