Warm tip: This article is reproduced from serverfault.com, please click

React how to create sub pages with react router to display?

发布于 2020-11-28 01:54:51

So, I'm creating a sidebar that has dropdown menus and I can't figure out how I would create a display page or filler page for my sub menus.

Here's my example data file

 const dropdownData = [
      {
         title: "Data',
         path: '/data',
         dropdownItems: [
          {
            title: 'Sub Data',
            path: '/data/subdata',
           },
           {
             title: 'Sub Data 2',
            path: '/data/subdata2',
           },
         ]
       }
     

So in my App.js I would normally do this for only one page

   <Router>
    <Sidebar />
    <Switch>
      <Route path='/data' component={Data} />
    </Switch>
  </Router>

I then create a pages folder with a Data.js page and just show a filler text like below

      import React from 'react';

      function Data() {
        return (
          <div className='data'>
            <h1>Data Page</h1>
          </div>
        );
      }

      export default Data;

So, if I click on the data menu link, it will just show "Data page" on the screen.

But what I can't figure out is how would I display a "fake" preview page for the sub menus?

Ideally, a page that says "This is the sub data page"

I've seen examples using :id but I don't get how to display it visually?

     <Route path='/data/:id' component={Data} />

I don't get how this would generate a new page if it's linked to my Data page?

Right now I just link to my path

  <Link to={item.path}> 

and on the URL it will show data/subdata when I click my sub menu, but I can't figure out how to create the sub menu data page and actually show it visually on my screen?

Questioner
Johnxr
Viewed
0
Henry Woody 2020-11-28 10:24:59

You need to create a route (using <Route>) to your subpages. You can do this in one of two ways. Either you can create other <Route>s next to your existing <Route> to "/data" and give the base data route the exact prop. Or you can nest the routing for data subpages in the Data component and split out a separate data "home page" component.

Both of these contain the same main idea but they differ in the manner of organization and the second approach is better for managing and expanding your app in the long-run.

First approach:

<Router>
  <Sidebar />
    <Switch>
      <Route exact path='/data' component={Data} />
      <Route path='/data/subdata' component={SubData} />
      <Route path='/data/subdata2' component={SubData2} />
    </Switch>
</Router>

Second approach (leave the top-level routing as-is and update your Data component to look like this):

function Data() {
  return (
    <Switch>
      <Route exact path="/data" component={ DataHome }/>
      <Route path="/data/subdata" component={ SubData }/>
      <Route path="/data/subdata2" component={ SubData2 }/>
    </Switch>
  );
}

export default Data;


function DataHome() {
  return (
    <div className='data'>
      <h1>Data Page</h1>
    </div>
  );
}

function SubData() {}

function SubData2() {}