Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

Using React Router's Outlet Component for Nested Routing in React

By admin, 5 April, 2024

In React, there is no built-in component named "Outlet." However, "Outlet" is a concept introduced in the React Router library, specifically in its version 6.

The "Outlet" component in React Router v6 is used as a placeholder where child routes are rendered. It acts as the container for child components associated with specific routes. When a route matches, React Router renders the components associated with that route inside the "Outlet" component.

Here's a basic example of how to use "Outlet" in React Router v6:

import React from 'react';
import { BrowserRouter as Router, Routes, Route, Outlet } from 'react-router-dom';
const Home = () => <h2>Home</h2>;
const About = () => <h2>About</h2>;
const Contact = () => <h2>Contact</h2>;
const App = () => (
 <Router>
   <Routes>
     <Route path="/" element={<Outlet />}>
       <Route path="/" element={<Home />} />
       <Route path="/about" element={<About />} />
       <Route path="/contact" element={<Contact />} />
     </Route>
   </Routes>
 </Router>
);

export default App;

In this example, the "Outlet" component is used within a parent route. When the parent route matches, React Router will render the child routes inside the "Outlet" component. This allows for nested routing and the rendering of multiple components based on the URL path.

Tags

  • React

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal