1

I am working on a project with react-router.

This is a project structure

main
   - mainRouter.tsx

   - programFolder
     - ProgramMainComponent.tsx
     - ProgramScheduleComponent.tsx
     - ProgramDetailComponent.tsx
     - programRouter.tsx

   - serviceFolder
     - ServiceMainComponent.tsx
     - ServiceFAQComponent.tsx
     - ServiceCustomerComponent.tsx
     - serviceRouter.tsx

And this is how I make programRouter.tsx and serviceRouter.tsx

// programRouter.tsx

const ProgramRouter = () => {
  return (
    <BrowserRouter>
      <Switch>
          <Route path={'/program'} exact={true} component={ProgramMainComponent} />
          <Route path={'/program/schedule'}  exact={true} component={ProgramScheduleComponent} />
          <Route path={'/program/detail'}  exact={true} component={ProgramDetailComponent} /
      </Switch>
    </BrowserRouter>
  )
}

// serviceRouter.tsx

const ServiceRouter = () => {
  return (
    <BrowserRouter>
      <Switch>
          <Route path={'/service'} exact={true} component={ServiceMainComponent} />
          <Route path={'/service/faq'}  exact={true} component={ServiceFAQComponent} />
          <Route path={'/program/customer'}  exact={true} component={ServiceCustomerComponent} />
      </Switch>
    </BrowserRouter>
  )
}

Here is the what I want to do.

As I add more pages, I want have each folder contains each their own router.tsx file and import those files inside of mainRouter.tsx file like the example below.

import * as React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { programRouter } from 'main/program';
import { serviceRouter } from 'main/service';

const Router = () => {
  return (
    <BrowserRouter>
      <Switch>
          <Route path='/' exact={true} component={MainComponent} />
          <Route path={'/program'} exact={true} component={programRouter} />
          <Route path={'/service'} exact={true} component={serviceRouter} />
      </Switch>
    </BrowserRouter>
  )
}

Is there a way I can use each router components inside fo mainRouter.tsx?

1 Answer 1

1

There is an example of nesting components and routes in the React Router docs. Basically, you don't need to use BrowserRouter more than once at the root level. You should also remove the exact={true} props for the routes in the root component to ensure their child routes can render properly.

import * as React from 'react';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
import { programRouter } from 'main/program';
import { serviceRouter } from 'main/service';

const Router = () => {
  return (
    <BrowserRouter>
      <Switch>
          <Route path='/' exact component={MainComponent} />
          <Route path={'/program'} component={programRouter} />
          <Route path={'/service'} component={serviceRouter} />
      </Switch>
    </BrowserRouter>
  )
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.