Outlet component in React
The Outlet component in React is used with React Router (specifically React Router v6 and later) to render child routes within a parent route. It acts as a placeholder in your component hierarchy where the nested routes will be rendered.
Key Concepts
- Nested Routes:
Outletallows you to define a structure where some routes have their own nested routes. This is useful for layouts that share common elements, such as navigation bars or sidebars, while displaying different content in a main area. - Parent and Child Routes: The parent route uses
Outletto indicate where the child routes should be rendered. When the parent route matches, theOutletcomponent renders the child route component that matches the nested route path.
Basic Example
Let's say you have an application with a main layout and nested routes for a dashboard:
App.js
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Layout from './Layout';
import Home from './Home';
import Dashboard from './Dashboard';
import Profile from './Profile';
import Settings from './Settings';
const App = () => {
return (
<Router>
<Routes>
<Route path="/" element={<Layout />}>
<Route index element={<Home />} />
<Route path="dashboard" element={<Dashboard />}>
<Route path="profile" element={<Profile />} />
<Route path="settings" element={<Settings />} />
</Route>
</Route>
</Routes>
</Router>
);
};
export default App;
Layout.js
import React from 'react';
import { Outlet, Link } from 'react-router-dom';
const Layout = () => {
return (
<div>
<nav>
<ul>
<li><Link to="/">Home</Link></li>
<li><Link to="/dashboard">Dashboard</Link></li>
</ul>
</nav>
<hr />
<Outlet />
</div>
);
};
export default Layout;
Dashboard.js
import React from 'react';
import { Outlet, Link } from 'react-router-dom';
const Dashboard = () => {
return (
<div>
<h2>Dashboard</h2>
<nav>
<ul>
<li><Link to="profile">Profile</Link></li>
<li><Link to="settings">Settings</Link></li>
</ul>
</nav>
<hr />
<Outlet />
</div>
);
};
export default Dashboard;
Profile.js and Settings.js
// Profile.js
import React from 'react';
const Profile = () => {
return <div>Profile Page</div>;
};
export default Profile;
// Settings.js
import React from 'react';
const Settings = () => {
return <div>Settings Page</div>;
};
export default Settings;
Explanation
- App.js: Defines the routes using
RoutesandRoute. TheLayoutcomponent is used for the main layout, and theDashboardcomponent has nested routes forProfileandSettings. - Layout.js: Uses the
Outletcomponent to indicate where the nested routes will be rendered. It also includes a navigation bar. - Dashboard.js: Also uses the
Outletcomponent to render its nested routes (ProfileandSettings) and includes a secondary navigation bar for these routes. - Profile.js and Settings.js: Simple components that represent the content for the nested routes.
When you navigate to /dashboard, the Dashboard component will render and the Outlet within it will display either the Profile or Settings component based on the nested route. This allows you to build complex, nested navigation structures while keeping your code organized and maintaining a clear separation of concerns.
Published on: Jul 21, 2024, 11:04 AM