Creating Nested Routes and Passing Parameters

Tutorial 3 of 5

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!