In React Router, the Outlet
component is a placeholder for nested routes. It allows you to render child routes inside a parent route’s component. When you define nested routes, Outlet
acts as a container to display the matching child route’s content within the parent route layout.
Key Features:
- Nested Routing: Enables you to render sub-routes or child routes inside a parent route component, providing flexibility to create layouts like dashboards, sidebars, or any page structure with nested content.
- Component Composition:
Outlet
helps to build complex layouts where parts of the UI (like headers, footers, or sidebars) remain constant, while other sections (determined by child routes) change dynamically.
How It Works:
When you define routes, some routes might have child routes (nested routes). Outlet
is used in the parent route’s component to indicate where the child route should render its component.
Example:
Let’s say you have a parent route Dashboard
that has two child routes: Overview
and Reports
.
Defining the Routes:
import { createBrowserRouter, RouterProvider, Route } from "react-router-dom";
import Dashboard from "./Dashboard";
import Overview from "./Overview";
import Reports from "./Reports";
const router = createBrowserRouter([
{
path: "dashboard",
element: <Dashboard />, // Parent route component
children: [
{
path: "overview", // Child route 1
element: <Overview />,
},
{
path: "reports", // Child route 2
element: <Reports />,
},
],
},
]);
function App() {
return <RouterProvider router={router} />;
}
export default App;
Parent Component (Dashboard.js
):
import { Outlet, Link } from "react-router-dom";
function Dashboard() {
return (
<div>
<h1>Dashboard</h1>
<nav>
<Link to="overview">Overview</Link>
<Link to="reports">Reports</Link>
</nav>
<Outlet /> {/* Child routes will render here */}
</div>
);
}
export default Dashboard;
Child Components (Overview.js
and Reports.js
):
// Overview.js
export default function Overview() {
return <h2>Overview Page</h2>;
}
// Reports.js
export default function Reports() {
return <h2>Reports Page</h2>;
}
How It Works:
- When you navigate to
/dashboard
, theDashboard
component renders with the links toOverview
andReports
. - If you navigate to
/dashboard/overview
, theOverview
component will render inside theOutlet
inDashboard
. - If you navigate to
/dashboard/reports
, theReports
component will render in the sameOutlet
.
Why Use Outlet
?
- Dynamic Content: You can create layouts where only part of the UI changes (like the main content area), while other parts (like headers or sidebars) remain static.
- Nested Layouts: It allows for hierarchical routing where a parent route can provide a structure, and child routes can populate different sections of that structure dynamically.
Summary:
Outlet
is used in parent components to render child routes within the nested route structure. It’s essential for building modular and scalable layouts in React applications that require different sections or views to change based on the route.
Use the share button below if you liked it.
It makes me smile, when I see it.