Home >Web Front-end >JS Tutorial >How Can I Render React Components with Customized Layouts Using React Router v6?
Rendering Components with Customized Layouts using React Router v6
In React, the
React Router v6 introduces a concept called "nested routes." This allows you to create a layout component that will be used as the parent of other routes. In this layout component, you can include shared elements like Navbar and Sidebar.
To implement this, you can create an AppLayout component that includes the Navbar and Sidebar. Then, in your App component, wrap all routes except the login page in the AppLayout.
// AppLayout.js import { Outlet } from 'react-router-dom'; const AppLayout = () => ( <> <NavBar /> <SideBar /> <main className={styles['main--container']}> <div className={styles['main--content']}> <Outlet /> </div> </main> </> ); // App.js import { Routes, Route } from 'react-router-dom'; import { AppLayout } from './AppLayout'; import LoginPage from './LoginPage'; import Dashboard from './Dashboard'; const App = () => { return ( <> <Routes> <Route path='/login' element={<LoginPage />} /> <Route element={<AppLayout />}> <Route path='/' element={<Dashboard />} /> </Route> </Routes> </> ); };
Alternatively, you can use the useRoutes hook or the
The above is the detailed content of How Can I Render React Components with Customized Layouts Using React Router v6?. For more information, please follow other related articles on the PHP Chinese website!