1. ReactRouter - RouterProvider
import { Outlet } from 'react-router-dom';
function Layout() {
return (
<div>
<Header />
<Outlet />
<Footer />
</div>
);
}
const routes = [
{
element: <Layout />,
children: [
{ path: '/', element: <HomePage /> },
{ path: '/about', element: <AboutPage /> },
],
},
];
export default routes;
import { createBrowserRouter, RouterProvider } from 'react-router-dom';
import routes from './routes';
const router = createBrowserRouter(routes);
root.render((
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
));
//v5
<Switch>
<Route path="/member" />
<Route path="/member/:memberId" />
</Switch>
//v6
<Routes>
<Route path="/member">
<Route path=":memberId" />
</Route>
</Routes>
import { Outlet } from 'react-router-dom';
const routes = [
{
path: "/",
element: <App1 />,
errorElement: <NotFound />,
children: [
{ index: true, element: <First /> },
{ path: "second", element: <Second /> },
{ path: "third", element: <Third /> },
],
},
{
path: "/app2",
element: <App2 />,
errorElement: <NotFound />,
children: [
{ index: true, element: <First /> },
{ path: "second", element: <Second /> },
{ path: "third", element: <Third /> },
],
},
];
import { Outlet } from "react-router-dom";
// App1.js
function App1() {
return (
<div>
<h1>This is App1 Header</h1>
<Outlet />
<h2>This is App1 Footer</h2>
</div>
);
}
function App2() {
return (
<div>
<h1>This is App2 Header</h1>
<Outlet />
<h2>This is App2 Footer</h2>
</div>
);
}
// App2.js