Understanding Nested Routing in React

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 respectively.

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. 

Back 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 inside of 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 folder src, then inside components, create the Home.js, Dashboard.js files and the products folder. 

Then inside the folder products, 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 define the routes for our website. The Router component will act as wrapper for all of our application routes, markup and URL paths. Routes is used to group together all 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 inside 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 component AddProduct and hide the other three nested routes.

An implication of this setup is that any other markup return 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 inside of 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 a 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 here.

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 Codesandbox


This content originally appeared on Envato Tuts+ Tutorials and was authored by Kingsley Ubah

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 respectively.

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. 

Back 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 inside of 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 folder src, then inside components, create the Home.js, Dashboard.js files and the products folder. 

Then inside the folder products, 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 define the routes for our website. The Router component will act as wrapper for all of our application routes, markup and URL paths. Routes is used to group together all 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 inside 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 component AddProduct and hide the other three nested routes.

An implication of this setup is that any other markup return 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 inside of 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 a 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 here.

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 Codesandbox


This content originally appeared on Envato Tuts+ Tutorials and was authored by Kingsley Ubah


Print Share Comment Cite Upload Translate Updates
APA

Kingsley Ubah | Sciencx (2016-12-31T08:04:32+00:00) Understanding Nested Routing in React. Retrieved from https://www.scien.cx/2016/12/31/understanding-nested-routing-in-react/

MLA
" » Understanding Nested Routing in React." Kingsley Ubah | Sciencx - Saturday December 31, 2016, https://www.scien.cx/2016/12/31/understanding-nested-routing-in-react/
HARVARD
Kingsley Ubah | Sciencx Saturday December 31, 2016 » Understanding Nested Routing in React., viewed ,<https://www.scien.cx/2016/12/31/understanding-nested-routing-in-react/>
VANCOUVER
Kingsley Ubah | Sciencx - » Understanding Nested Routing in React. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2016/12/31/understanding-nested-routing-in-react/
CHICAGO
" » Understanding Nested Routing in React." Kingsley Ubah | Sciencx - Accessed . https://www.scien.cx/2016/12/31/understanding-nested-routing-in-react/
IEEE
" » Understanding Nested Routing in React." Kingsley Ubah | Sciencx [Online]. Available: https://www.scien.cx/2016/12/31/understanding-nested-routing-in-react/. [Accessed: ]
rf:citation
» Understanding Nested Routing in React | Kingsley Ubah | Sciencx | https://www.scien.cx/2016/12/31/understanding-nested-routing-in-react/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.