Brian Dantonio | 15 Jul, 2025

Master React Router: A Step-by-Step Tutorial

Ready to transform your single-page React app into a seamless, multi-view experience? If you've ever wondered how to create different "pages" without a full-page reload, you've come to the right place. The key to building professional and user-friendly web applications lies in smooth, intuitive navigation.

This guide is your easy tutorial for React Router, the industry-standard library for handling routing in React. We'll take you from the absolute basics of setting up your first routes to advanced techniques like handling dynamic content and protected pages. Let's get started and make your application's navigation feel effortless.

That should help with all of your React projects.

What is React Router and Why Do You Need It? πŸ€”

A standard React application exists on a single HTML page. This is what we call a Single-Page Application (SPA). While this makes the app fast, it also means there are no separate "pages" by default. If you use a traditional <a> tag to navigate, the browser will perform a full refresh, defeating the purpose of a SPA.

React Router solves this by enabling client-side routing. It intercepts browser navigation events and dynamically renders the React components you want to display for a specific URL, all without a page refresh. This gives you the speed of a SPA with the familiar user experience of a multi-page website.

Getting Started: Installation and Setup

First, you need to add React Router to your project. Open your terminal in your project's root directory and run the following command:
npm install react-router-dom

Once installed, you need to "wrap" your entire application with a router component. The most common one is BrowserRouter, which uses the browser's History API. You can read more about the Route component, too.

import { BrowserRouter, Routes, Route, Link } from 'react-router-dom';
import HomePage from './HomePage';
import AboutPage from './AboutPage';

function App() {
  return (
    <BrowserRouter>
      <nav>
        <Link to="/">Home</Link> | <Link to="/about">About</Link>
      </nav>
      <Routes>
        <Route path="/" element={<HomePage />} />
        <Route path="/about" element={<AboutPage />} />
      </Routes>
    </BrowserRouter>
  );
}

Let's break this down:

  • BrowserRouter: The parent component that enables routing.

// In your App.js Routes component
<Route path="/profile/:userId" element={<ProfilePage />} />
Now, your ProfilePage component can access the userId from the URL using the useParams hook.
// In your ProfilePage.js file
import { useParams } from 'react-router-dom';

function ProfilePage() {
  let { userId } = useParams(); // Gets the value from the URL

  return (
    <div>
      <h2>Profile for User: {userId}</h2>
      {/* You can now fetch data for this specific user */}
    </div>
  );
}

Now, navigating to /profile/123 will display "Profile for User: 123", and /profile/jane-doe will display "Profile for User: jane-doe".

Advanced Navigation: Programmatic Redirects πŸš€

Sometimes you need to navigate a user based on an action, like after they submit a form or log in successfully. You can't use a <Link> component for this because it requires a user click. Instead, you use the useNavigate hook.

Here’s a simple login form that redirects the user to their dashboard upon success:

// In your LoginPage.js file
import { useNavigate } from 'react-router-dom';

function LoginPage() {
  const navigate = useNavigate();

  const handleLogin = (event) => {
    event.preventDefault();
    // Your authentication logic would go here...
    const loginSuccessful = true; // Assume login was successful

    if (loginSuccessful) {
      navigate('/dashboard'); // Redirects the user to the dashboard
    }
  };

  return (
    <form onSubmit={handleLogin}>
      <button type="submit">Log In</button>
    </form>
  );
}

Wrap-up

You've now traveled from setting up basic page routes to handling dynamic content with URL parameters and controlling user flow with programmatic navigation. By mastering React Router, you've unlocked the ability to create complex, feature-rich, and highly professional web applications.

The path forward is clear: start integrating these routing concepts into your own projects. The hands-on experience of building a multi-view application is the best way to solidify your knowledge and dramatically improve your portfolio.

 
By Brian Dantonio

Brian Dantonio (he/him) is a news reporter covering tech, accounting, and finance. His work has appeared on hackr.io, Spreadsheet Point, and elsewhere.

View all post by the author

Disclosure: Hackr.io is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission.

In this article

Learn More