import { Page2 } from '../Page2';
import { UrlParameter } from '../UrlParameter';
export const page2Routes = [
{
path: '/',
exact: true,
children: <Page2 />,
},
{
path: '/:id',
exact: false,
children: <UrlParameter />,
},
];
import { Link } from 'react-router-dom';
export const Page2 = () => {
return (
<div>
<h1>Page2ページです。</h1>
<Link to="/page2/9999">URL Parameter</Link>
</div>
);
};
import { useParams } from 'react-router-dom';
export const UrlParameter = () => {
const { id } = useParams();
return (
<div>
<h1>UrlParameterページです</h1>
<p>パラメータは{id}です</p>
</div>
);
};
import { Switch, Route } from 'react-router-dom';
import { Page2 } from '../Page2';
import { Home } from '../Home';
import { page1Routes } from './Page1Routes';
import { page2Routes } from './Page2Routes';
export const Router = () => {
return (
<Switch>
<Route exact path="/">
<Home />
</Route>
<Route
path="/page1"
render={({ match: { url } }) => (
<Switch>
{page1Routes.map((route) => (
<Route
key={route.path}
exact={route.exact}
path={`${url}${route.path}`}
>
{route.children}
</Route>
))}
</Switch>
)}
></Route>
<Route
path="/page2"
render={({ match: { url } }) => (
<Switch>
{page2Routes.map((route) => (
<Route
key={route.path}
exact={route.exact}
path={`${url}${route.path}`}
>
{route.children}
</Route>
))}
</Switch>
)}
></Route>
</Switch>
);
};