React.js / React Routing and Navigation

Creating Nested Routes and Passing Parameters

This tutorial will teach you how to create nested routes and pass parameters through routes. You'll learn how to create a more complex routing structure and make your routes more …

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers implementing client-side routing and navigation with React Router.

1. Introduction

In this tutorial, we will learn how to create nested routes and pass parameters through them. Nested routes allow us to create a more structured hierarchy in our web application, and passing parameters make our routes dynamic and flexible.

By the end of this tutorial, you should be able to:

  • Understand what nested routes are and when to use them.
  • Understand how to pass parameters through routes.
  • Create and implement nested routes and route parameters in your own applications.

Prerequisites: Basic understanding of web development, familiarity with JavaScript, and exposure to any web development framework like React, Vue, or Angular.

2. Step-by-Step Guide

Nested Routes

Nested routes, also known as child routes, allow us to create a hierarchical structure in our application. This is particularly useful when we have data that is nested or related in some way.

For example, consider a blog application. You might have a /posts route to show all posts, and then a /posts/:id route to show a specific post.

Passing Parameters

Parameters allow us to make our routes dynamic. In the above example, :id is a parameter. When a user navigates to /posts/1, they'll see the post with an ID of 1. Navigate to /posts/2, and they'll see the post with an ID of 2.

3. Code Examples

Below we will see how to create nested routes and pass parameters in a simple React application.

Example 1: Creating a nested route

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';

const Post = ({ match }) => <p>{match.params.id}</p>;

const Posts = ({ match }) => (
  <div>
    <h2>Posts</h2>
    <Link to={`${match.url}/1`}>Post 1</Link>
    <Route path={`${match.path}/:id`} component={Post} />
  </div>
);

const App = () => (
  <Router>
    <Link to="/posts">Posts</Link>
    <Route path="/posts" component={Posts} />
  </Router>
);

export default App;

In this example, we first create a Post component that displays the id of a post. We then create a Posts component that contains a link to Post 1 and a Route that renders the Post component when the path is /posts/:id.

Example 2: Passing parameters through a route

import React from 'react';
import { BrowserRouter as Router, Route, Link } from 'react-router-dom';

const User = ({ match }) => <p>{match.params.username}</p>;

const App = () => (
  <Router>
    <Link to="/user/johndoe">John Doe</Link>
    <Route path="/user/:username" component={User} />
  </Router>
);

export default App;

Here, :username is a parameter. When a user navigates to /user/johndoe, the User component is rendered, and match.params.username will be johndoe.

4. Summary

In this tutorial, we've learned about nested routes and how to pass parameters through routes. These are powerful concepts that allow us to create more complex and dynamic routing structures in our web applications.

For further learning, consider exploring more about the react-router-dom library, and how routing works in other web development frameworks.

5. Practice Exercises

  1. Create a Users component that renders a list of links to individual User components. Each User component should display the user's username.

  2. Extend the Posts component from the previous example to include a Comments component. This component should be rendered when the path is /posts/:id/comments, and should display a list of comments for the specified post.

  3. Create a Profile component that is rendered when the path is /user/:username/profile, and displays the user's profile information.

Remember to follow the same pattern as the examples above, and make sure to test your work as you go along!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Fake User Profile Generator

Generate fake user profiles with names, emails, and more.

Use tool

Lorem Ipsum Generator

Generate placeholder text for web design and mockups.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help