Lesson 41 of 60 – React Router Setup
68%

React Router Setup

In the previous lesson, we learned what React Router is and why routing is important. In this lesson, we will learn how to install React Router and set up routing in a React application.

Note: React Router is commonly installed with the react-router-dom package when building React applications for the web.

1. What is React Router Setup?

React Router setup means preparing a React application to use routes, navigation, and URL-based components.

The basic setup usually includes:

  • Installing React Router
  • Importing router components
  • Creating a router
  • Creating routes
  • Connecting routes with components

2. Check Your React Project

Before installing React Router, make sure you already have a React project.

npm create vite@latest my-react-app

Then move into the project folder:

cd my-react-app

Install the project dependencies:

npm install

3. Install React Router

Open the terminal inside your React project and install react-router-dom.

npm install react-router-dom

This adds the React Router package to your project.

4. Check package.json

After installation, the package is added to the project's dependencies.

{
  "dependencies": {
    "react-router-dom": "..."
  }
}

The exact version depends on the version installed in your project.

5. Import BrowserRouter

The first important router component is BrowserRouter.

import {
    BrowserRouter
} from "react-router-dom";

It provides routing functionality for a browser-based React application.

6. Import Routes

The Routes component is used to contain the route definitions.

import {
    BrowserRouter,
    Routes
} from "react-router-dom";

Individual Route components are placed inside Routes.

7. Import Route

The Route component connects a URL path with a React element.

import {
    BrowserRouter,
    Routes,
    Route
} from "react-router-dom";

8. Create a Home Component

First, create a simple component that will represent the home page.

function Home() {

    return (
        <h1>Home Page</h1>
    );
}

export default Home;

9. Create an About Component

Create another component for the About page.

function About() {

    return (
        <h1>About Page</h1>
    );
}

export default About;

Now the application has two page components.

10. Create Routes

Routes are created using the Route component.

<Routes>

    <Route
        path="/"
        element={<Home />}
    />

    <Route
        path="/about"
        element={<About />}
    />

</Routes>

11. Understanding the path Prop

The path prop specifies the URL path for a route.

<Route
    path="/about"
    element={<About />}
/>

The path in this example is /about.

12. Understanding the element Prop

The element prop specifies what React element should be displayed when the route matches.

<Route
    path="/about"
    element={<About />}
/>

Here, the About component is rendered for the /about path.

13. Wrap Routes with BrowserRouter

The routes should be used within a router context.

<BrowserRouter>

    <Routes>

        <Route
            path="/"
            element={<Home />}
        />

        <Route
            path="/about"
            element={<About />}
        />

    </Routes>

</BrowserRouter>

14. Basic App.jsx Setup

A basic App.jsx can contain the routing configuration.

import {
    BrowserRouter,
    Routes,
    Route
} from "react-router-dom";

import Home from "./Home";
import About from "./About";

function App() {

    return (
        <BrowserRouter>

            <Routes>

                <Route
                    path="/"
                    element={<Home />}
                />

                <Route
                    path="/about"
                    element={<About />}
                />

            </Routes>

        </BrowserRouter>
    );
}

export default App;

15. Creating a Contact Route

You can add more routes to your application.

function Contact() {

    return (
        <h1>Contact Page</h1>
    );
}

Then add its route:

<Route
    path="/contact"
    element={<Contact />}
/>

16. Importing Page Components

If your page components are stored in a pages folder, they can be imported like this:

import Home from "./pages/Home";
import About from "./pages/About";
import Contact from "./pages/Contact";

This helps keep larger React projects organized.

17. Creating a Pages Folder

A common project structure is:

src/
├── pages/
│   ├── Home.jsx
│   ├── About.jsx
│   └── Contact.jsx
│
├── App.jsx
└── main.jsx

The pages folder can contain components that represent application pages.

18. Adding Link Navigation

After creating routes, you can add navigation using the Link component.

import { Link } from "react-router-dom";

function Navbar() {

    return (
        <nav>

            <Link to="/">
                Home
            </Link>

            <Link to="/about">
                About
            </Link>

        </nav>
    );
}

19. Complete Navigation Setup

import { Link } from "react-router-dom";

function Navbar() {

    return (
        <nav>

            <Link to="/">
                Home
            </Link>

            {" | "}

            <Link to="/about">
                About
            </Link>

            {" | "}

            <Link to="/contact">
                Contact
            </Link>

        </nav>
    );
}

export default Navbar;

20. Adding Navbar to App

The navigation component can be placed above the routes.

<BrowserRouter>

    <Navbar />

    <Routes>

        <Route
            path="/"
            element={<Home />}
        />

        <Route
            path="/about"
            element={<About />}
        />

    </Routes>

</BrowserRouter>

21. Router Setup in main.jsx

Another common setup is to place BrowserRouter around App in main.jsx.

import React from "react";
import ReactDOM from "react-dom/client";

import {
    BrowserRouter
} from "react-router-dom";

import App from "./App";

ReactDOM.createRoot(
    document.getElementById("root")
).render(
    <BrowserRouter>
        <App />
    </BrowserRouter>
);

22. Avoiding Duplicate BrowserRouter

If BrowserRouter is already placed around App in main.jsx, you normally should not wrap App with another BrowserRouter.

// main.jsx

<BrowserRouter>
    <App />
</BrowserRouter>

Then App.jsx can contain the route definitions directly.

23. Routes Inside App.jsx

import {
    Routes,
    Route
} from "react-router-dom";

import Home from "./pages/Home";
import About from "./pages/About";

function App() {

    return (
        <Routes>

            <Route
                path="/"
                element={<Home />}
            />

            <Route
                path="/about"
                element={<About />}
            />

        </Routes>
    );
}

export default App;

This structure works when BrowserRouter is provided above App.

24. Test the Routes

Start your React development server:

npm run dev

Then open the development URL and test the routes.

/
 /about
 /contact

Each URL should display the component associated with its route.

25. Common Installation Error

If React Router is not installed, you may see an error when importing it.

Failed to resolve import "react-router-dom"

Install the package using:

npm install react-router-dom

26. Common Import Error

Make sure router components are imported from the correct package.

import {
    BrowserRouter,
    Routes,
    Route,
    Link
} from "react-router-dom";

A spelling mistake in the package name or component name can cause an error.

27. Common Router Context Error

Components such as Link need router context.

For example, this type of structure is correct:

<BrowserRouter>

    <Navbar />

    <Routes>
        <Route
            path="/"
            element={<Home />}
        />
    </Routes>

</BrowserRouter>

28. Recommended Project Structure

src/
│
├── components/
│   └── Navbar.jsx
│
├── pages/
│   ├── Home.jsx
│   ├── About.jsx
│   └── Contact.jsx
│
├── App.jsx
└── main.jsx

This structure is simple and can be expanded as the application grows.

29. Complete Router Setup Example

// main.jsx

import React from "react";
import ReactDOM from "react-dom/client";

import {
    BrowserRouter
} from "react-router-dom";

import App from "./App";

ReactDOM.createRoot(
    document.getElementById("root")
).render(
    <BrowserRouter>
        <App />
    </BrowserRouter>
);


// App.jsx

import {
    Routes,
    Route,
    Link
} from "react-router-dom";

function Home() {
    return <h1>Home Page</h1>;
}

function About() {
    return <h1>About Page</h1>;
}

function App() {

    return (
        <>

            <nav>

                <Link to="/">
                    Home
                </Link>

                {" | "}

                <Link to="/about">
                    About
                </Link>

            </nav>

            <Routes>

                <Route
                    path="/"
                    element={<Home />}
                />

                <Route
                    path="/about"
                    element={<About />}
                />

            </Routes>

        </>
    );
}

export default App;

30. React Router Setup Best Practices

  • Install react-router-dom before using React Router.
  • Keep page components organized.
  • Use meaningful URL paths.
  • Use Link for internal navigation.
  • Keep BrowserRouter in one appropriate place.
  • Do not unnecessarily nest multiple BrowserRouter components.
  • Keep route definitions easy to understand.
  • Test every important route.
  • Use reusable navigation components.
  • Handle invalid routes appropriately as the application grows.
Install
   ↓
BrowserRouter
   ↓
Routes
   ↓
Route
   ↓
Component
   ↓
Link Navigation

📌 Key Points

  • Install React Router using npm install react-router-dom.
  • BrowserRouter provides routing context.
  • Routes contains route definitions.
  • Route connects a path with a React element.
  • Link is used for navigation between application routes.
  • Router setup can be placed around the application in main.jsx.
  • Page components can be organized inside a pages folder.
  • Always test the routes after setting up React Router.

🧠 Quick Quiz

Question: Which command is used to install React Router for a React web application?