REACT MASTER CLASS /// DYNAMIC ROUTING /// USEPARAMS HOOK /// SCALABLE ARCHITECTURE /// REACT MASTER CLASS /// DYNAMIC ROUTING /// USEPARAMS HOOK /// SCALABLE ARCHITECTURE ///

React Dynamic Routes

Learn how to build scalable single-page applications by injecting variables directly into your URLs and extracting them with React Router.

App.jsx
1 / 15
12345
πŸ—ΊοΈ

Tutor:Imagine an e-commerce site. You don't want to create 10,000 separate components for 10,000 products. Instead, you create a single dynamic route to handle them all.


Skill Matrix

UNLOCK NODES BY MASTERING ROUTING.

Path Params

A dynamic segment allows multiple URLs to match the same Route. It acts as a variable placeholder in the path.

System Check

What character indicates a dynamic segment in React Router?


Community Holo-Net

Showcase Your Architecture

ACTIVE

Built an elaborate routing system? Share your React Router setups with the collective.

React Dynamic Routes

Author

Pascual Vila

Senior Frontend Engineer // Code Syllabus

Dynamic routing is the cornerstone of scalable React applications. Instead of hardcoding every possible URL (like /user/1, /user/2, etc.), you define a single route template.

The Dynamic Segment

In React Router v6, you create a dynamic segment in a path by prefixing a word with a colon (:). For instance, <Route path="/profile/:userId" /> tells the router that userId is a variable. When a user navigates to /profile/42, the router matches this route and stores the value "42" under the key userId.

Extracting with useParams

Once matched, the component rendered by that route needs access to the dynamic data. This is where the useParams hook comes in. It returns an object containing all dynamic segments mapped to their URL values.

import { useParams } from 'react-router-dom';

function Profile() {
  const { userId } = useParams();
  return <div>User ID: {userId}</div>;
}

Linking to Dynamic Routes

When linking to a dynamic route, you don't use the colon syntax. The colon is only for defining the route pattern. To link, you provide the actual value, usually by injecting a variable into a string using template literals: <Link to={'/profile/' + user.id}>.

View Routing Patterns+

Multiple Parameters: path="/blog/:year/:month/:slug"

Optional Parameters: In React Router v6, optional params aren't natively supported via regex like v5. You usually define multiple routes or use trailing splats *.

Query Parameters: Data after the ? (e.g., ?sort=asc) is NOT handled by useParams. For that, you use the useSearchParams hook.

Routing Glossary

Dynamic Segment

A portion of a URL path designed to capture a variable value, denoted by a colon (e.g., `:id`).

snippet.jsx

useParams()

A React Router hook that returns an object of key/value pairs of the dynamic params from the current URL.

snippet.jsx

<Link>

A component from React Router used to navigate between views without triggering a full page reload.

snippet.jsx

<Route>

A component that renders a specific UI element when its path matches the current URL.

snippet.jsx

Template Literal

A JavaScript string literal allowing embedded expressions, crucial for generating dynamic links.

snippet.jsx

useSearchParams()

A hook used to read and modify the query string in the URL for the current location (e.g., ?sort=name).

snippet.jsx