DEV Community

Devi
Devi

Posted on

1

Dynamic routing in React

Dynamic routing in React allows you to create routes based on dynamic data or parameters, enabling more flexible and powerful navigation within your application. This is particularly useful for applications that need to render different components based on user input or other dynamic factors.

Setting Up Dynamic Routing with React Router
You’ll typically use the react-router-dom library to implement dynamic routing in React. Here’s a step-by-step guide:

Install React Router: First, you need to install react-router-dom if you haven’t already:
npm install react-router-dom

Create Routes: Define your routes using the component. Use dynamic segments in the path to capture parameters.
JavaScript

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './Home';
import UserProfile from './UserProfile';

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

export default App;
Enter fullscreen mode Exit fullscreen mode

Access Route Parameters: Use the useParams hook to access dynamic parameters within your components.
JavaScript

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

const UserProfile = () => {
    const { id } = useParams();

    return (
        <div>
            <h1>User Profile</h1>
            <p>User ID: {id}</p>
        </div>
    );
};

export default UserProfile;
Enter fullscreen mode Exit fullscreen mode

Example: Dynamic User Profiles
Let’s create a simple example where we navigate to different user profiles based on the user ID in the URL.

Home Component: This component will have links to different user profiles.
JavaScript

import React from 'react';
import { Link } from 'react-router-dom';

const Home = () => {
    return (
        <div>
            <h1>Home</h1>
            <ul>
                <li><Link to="/user/1">User 1</Link></li>
                <li><Link to="/user/2">User 2</Link></li>
                <li><Link to="/user/3">User 3</Link></li>
            </ul>
        </div>
    );
};

export default Home;
Enter fullscreen mode Exit fullscreen mode

UserProfile Component: This component will display the user ID from the URL.
JavaScript

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

const UserProfile = () => {
    const { id } = useParams();

    return (
        <div>
            <h1>User Profile</h1>
            <p>User ID: {id}</p>
        </div>
    );
};

export default UserProfile;
Enter fullscreen mode Exit fullscreen mode

App Component: This component sets up the router and defines the routes.
JavaScript

import React from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
import Home from './Home';
import UserProfile from './UserProfile';

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

export default App;
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay