intro-frontend-course

Lesson: Advanced Routing Techniques with React Router


1. Introduction to Advanced Routing Techniques

React Router is a powerful library for handling routing in React applications. Beyond basic route definitions, React Router allows for more advanced routing techniques such as nested routes, dynamic routing, and programmatic navigation. These techniques are essential for building complex, scalable applications with organized and maintainable code.

Key Concepts:


2. Nested Routes in React Router

Nested routes are used to render child components within a parent component’s layout. This is useful for creating layouts like dashboards, where multiple sections need to be rendered within the same page.

2.1. Defining Nested Routes

Example:

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

function App() {
  return (
    <Router>
      <nav>
        <Link to="/dashboard">Dashboard</Link>
      </nav>

      <Switch>
        <Route path="/dashboard" component={Dashboard} />
      </Switch>
    </Router>
  );
}

function Dashboard() {
  let { path, url } = useRouteMatch(); // Hook to get the current path and url

  return (
    <div>
      <h2>Dashboard</h2>
      <nav>
        <Link to={`${url}/overview`}>Overview</Link>
        <Link to={`${url}/settings`}>Settings</Link>
      </nav>

      <Switch>
        <Route exact path={path}>
          <h3>Please select a section.</h3>
        </Route>
        <Route path={`${path}/overview`} component={Overview} />
        <Route path={`${path}/settings`} component={Settings} />
      </Switch>
    </div>
  );
}

function Overview() {
  return <h3>Overview Section</h3>;
}

function Settings() {
  return <h3>Settings Section</h3>;
}

export default App;

In this example:

2.2. Benefits of Nested Routes

3. Dynamic Routing in React Router

Dynamic routing allows routes to be created based on dynamic segments, such as user IDs or search queries. This is particularly useful for pages that need to display specific content based on a parameter.

3.1. Defining Dynamic Routes

Example:

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

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/user/:id" component={UserProfile} />
      </Switch>
    </Router>
  );
}

function UserProfile() {
  let { id } = useParams(); // Hook to access the dynamic parameter

  return (
    <div>
      <h2>User Profile for ID: {id}</h2>
      {/* Fetch and display user data based on the ID */}
    </div>
  );
}

export default App;

In this example:

3.2. Use Cases for Dynamic Routing

4. Programmatic Navigation

Programmatic navigation allows you to change the route in response to user actions without requiring the user to click a link. This is useful for scenarios like redirecting after form submission or conditionally navigating based on user input.

4.1. Implementing Programmatic Navigation

Example:

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

function App() {
  return (
    <Router>
      <Switch>
        <Route path="/" exact component={Home} />
        <Route path="/dashboard" component={Dashboard} />
      </Switch>
    </Router>
  );
}

function Home() {
  let history = useHistory(); // Hook to get history object

  const handleLogin = () => {
    // Perform login logic...
    history.push('/dashboard'); // Navigate to dashboard after login
  };

  return (
    <div>
      <h2>Home</h2>
      <button onClick={handleLogin}>Login</button>
    </div>
  );
}

function Dashboard() {
  return <h2>Dashboard</h2>;
}

export default App;

In this example:

4.2. Use Cases for Programmatic Navigation

5. Implementing Advanced Routing Scenarios in the Course Project

For this part of the lesson, apply what you’ve learned to your course project:

Example Tasks:


6. Video Resources

To reinforce your understanding of advanced routing techniques, here are some helpful videos:

React Router Nested Routes (9:45)
Dynamic Routing in React Router (8:29)
Programmatic Navigation with React Router (7:14)

7. External Resources

For further reading and exploration: