React Router not only allows you to define routes and render components, but it also provides the ability to nest routes. Nesting routes is a powerful technique that helps you organize and structure your routes efficiently. Let's explore the world of nesting routes and learn how to efficiently manage and navigate through nested routes in your React applications.
Nested routes in React Router refer to the practice of defining routes within other routes. This hierarchical structure allows you to organize your routes in a more logical and maintainable way. With nested routes, you can create a clear and intuitive routing structure for your application.
Nested routes in React Router are routes that are defined within other routes. This nesting creates a hierarchical structure where parent routes can contain child routes. Nested routes are useful when you have sections of your application that require multiple levels of navigation.
Nested routes offer several benefits:
Here are the steps to nest routes in React Router:
Route
component.Route
component and specify the path
and element
or component
prop.index
attribute instead of omitting the path
prop.Let's define our parent routes:
// App.js
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Home from './Home';
import News from './News';
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/news">
<Route path="/news/technology" element={<Technology />} />
<Route path="/news/sports" element={<Sports />} />
</Route>
</Routes>
</Router>
);
}
export default App;
In this code, we define two parent routes: one for the homepage (/
) and another for the news page (/news
).
Now, let's nest child routes within the news page route:
// News.js
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Technology from './Technology';
import Sports from './Sports';
function News() {
return (
<Routes>
<Route path="/news/technology" element={<Technology />} />
<Route path="/news/sports" element={<Sports />} />
</Routes>
);
}
export default News;
In this code, we define two child routes within the news page route: one for the technology section (/news/technology
) and another for the sports section (/news/sports
). We specify the element
prop to indicate the component that should be rendered for each child route.
If you want a specific child route to be rendered by default, you can use the index
attribute instead of omitting the path
prop:
// News.js
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Technology from './Technology';
import Sports from './Sports';
import IntroductionH from './IntroductionH';
function News() {
return (
<Routes>
<Route path="/news/technology" element={<Technology />} />
<Route path="/news/sports" element={<Sports />} />
<Route index element={<IntroductionH />} /> {/* This route will be rendered by default */}
</Routes>
);
}
export default News;
In this code, we added a default route (/news
) that will be rendered when the user visits /news
without specifying a specific child route. The IntroductionH
component will be rendered by default.
Let's see the complete example:
// App.js
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import Home from './Home';
import News from './News';
function App() {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/news">
<Route path="/news/technology" element={<Technology />} />
<Route path="/news/sports" element={<Sports />} />
<Route index element={<IntroductionH />} /> {/* This route will be rendered by default */}
</Route>
</Routes>
</Router>
);
}
export default App;
// News.js
import React from 'react';
import { Routes, Route } from 'react-router-dom';
import Technology from './Technology';
import Sports from './Sports';
import IntroductionH from './IntroductionH';
function News() {
return (
<Routes>
<Route path="/news/technology" element={<Technology />} />
<Route path="/news/sports" element={<Sports />} />
<Route index element={<IntroductionH />} /> {/* This route will be rendered by default */}
</Routes>
);
}
export default News;
Route
component. The parent routes act as the main sections of our application.Route
component. The child routes represent subsections within the parent routes.index
attribute to specify a default route. This route will be rendered when the user visits the parent route without specifying a specific child route.Nesting routes in React Router provides a structured and organized way to manage your application's routing. It allows you to create a hierarchical structure that reflects the logical flow of your application. By nesting routes, you can efficiently manage and navigate through different sections of your application.