Skip to main content

Implementing nested routes for intricate layouts

In this lesson, we will learn how to implement nested routes in React Router to create intricate layouts and nested views in a single-page application. Nested routes allow you to define routes within routes, enabling you to create complex layouts with multiple levels of nesting.

Setting up nested routes​

To implement nested routes in React Router, you can use the Routes and Route components to define the nested routes. The Routes component is used to group multiple Route components together and define the nested routes.

Here's an example of how you can implement nested routes in your application:

/src/App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Header from './Header';
import Home from './Home';
import About from './About';
import Contact from './Contact';
import Profile from './Profile';
import Settings from './Settings';

function App() {
return (
<>
<h1>My App</h1>
<Header />
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route path="/contact" element={<Contact />} />
<Route path="/profile" element={<Profile />}>
<Route path="settings" element={<Settings />} />
</Route>
</Routes>
</Router>
</>
);
}

export default App;

In the example above, we have defined a nested route for the Profile component. When the user navigates to the /profile route, the Profile component is rendered, and the nested route for settings is also defined within the Profile component. This allows us to create a nested view for the settings page within the profile page.

By using nested routes, you can create intricate layouts and nested views in your single-page application, making it easier to manage complex routing scenarios.

In the next lesson, we will learn how to implement route parameters in React Router to handle dynamic routing scenarios.

Conclusion​

In this lesson, we learned how to implement nested routes in React Router to create intricate layouts and nested views in a single-page application. Nested routes allow you to define routes within routes, enabling you to create complex layouts with multiple levels of nesting. By using nested routes, you can manage complex routing scenarios and create a more structured application layout.