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?