7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial
Advertisement
  1. Code
  2. React

Understanding Nested Routing in React

Scroll to top
Read Time: 7 mins

React is a JavaScript library built by Facebook for composing user interfaces in a single-page application. In this tutorial, we'll discuss how to properly set up routing and also how to handle nested routing in a React web application using React Router.

A Closer Look at Route Nesting

In order to clearly explain what nested routes are and how they are implemented in React, I created a simple website.

Before proceeding, please view the app demo below. Expand the right window and pay attention to the URLs as you navigate through the site.

Basically, the idea behind this website is that on the front page you have a navigation menu with links to three pages: the Home page, Dashboard page, and Products page.

The first two pages, Home and Dashboard, both have simple routes which look like this: /home and /dashboard. However, in certain situations, we might be required to use nested routes to structure our pages. In our case, the Products page will have multiple nested routes to display different pages. 

On the Products homepage, we have a route for products search, a route to display the list of products, and another route to add a product. The routes for each of these pages will be nested upon /products, as in /products/add, products/all, and products/search.

Now that we have understood what nested routes are and have an idea of our project, let's go through the process of setting them up in a React application.

Installations

We'll use the create-react-app code generator to generate the skeleton of our React project.

You’ll need to have Node >= 14.0.0 and npm >= 5.6 on your machine. To create a project, run the following on your terminal:

This will create your React application in the demo-app folder.

To implement routing in our app, we'll also install the popular react-router-dom library from npm. React Router is a popular library for routing in React. The library enables the navigation among various components in a React application by mapping different parts of the application UI to unique URL paths.

To install the react-router package, run the following:

With the package installed, let's now go over the details for our website.

Project Structure

The overall structure of our website will look like this:

Using this diagram as a reference, create a components folder inside the src folder, and then inside components, create the Home.js and Dashboard.js files and the products folder. 

Then, inside the products folder, create the Products.js, AddProducts.js, ProductsList.js, SingleProduct.js, and ProductSearch.js files for the nested pages.  

Implementing Routes Within the App

Before we begin creating React components to render the different pages, let's first take a look at the final version of our root component, App.js.

We'll begin by making the imports:

On the first line of the code, we imported some core components to enable us to define the routes for our website. The Router component will act as a wrapper for all of our application routes, markup, and URL paths. Routes is used to group together all the routes defined within the app. Each route is defined with the Route component, which takes a URL path and maps that path to a React component. 

Below the react-router imports, we also imported the respective components to be rendered across these routes.

Now, below the imports, include the following code:

To nest routes, we simply placed all four routes as children of /products, thereby making /products the parent route. This organization will create paths like products/search, product/list, product/add, and product/2

Note that the :id slug in the last route signifies that the page is dynamic: it changes based on the id parameter.

The best part of route nesting is that you can define a base layout in the Products component. This layout will then be reused across all pages nested within /products in the routes. 

Here's the CSS code which goes inside the App.css file:

Let's take a closer look at implementing base layouts in the following section.

Implementing Base Layout for Nested Routes

We'll include the following code in the src/components/products/Products.js file:

We began by importing React, Link, and Outlet

As its name implies, Outlet allows you to "let out" the component related to the active route. For example, whenever we navigate to products/add, React Router will "let out" the AddProduct component and hide the other three nested routes.

An implication of this setup is that any other markup returned from the component (as the nav menu in our case) will be outputted across all other nested routes. So the navbar will also appear in products/list and products/2.

Dynamic Routes

Going back to our route structure in App.js, the last route was defined as follows:

This will be used to display a single product based on the product's id property. 

Include the following code in src/components/products/SingleProduct.js:

useParams allows us to use the parameters related to the active route. Here, we destructure the id property from the param and, using this id, we'll retrieve the corresponding product data from ProductsData and pass it into the template.

Even though in our case ProductsData is just fake data from a JavaScript array, it represents the database for the website. So it can be replaced with any database of your choice.

The final version of our app can be viewed on Codesandbox.

Wrapping It Up

In this tutorial, you saw how to implement nested routing in a React application using React Router. 

I hope this tutorial helped you understand nested routes in React better. You should now be able to properly structure the routes for your React Application using the react-router-dom package. 

React has become one of the main UI frameworks for working on the web. It’s not without its learning curves, and there are plenty of resources to keep you busy, as well. Source code from this tutorial is available on GitHub

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.