Getting Started with React Router

Ikokwu prosper
LearnFactory Nigeria
4 min readMay 13, 2024

Ever dream of building a dynamic web app where users can seamlessly jump between different sections or pages without bulky page reloads? React Router is your secret weapon! It’s a powerful library that injects superpowers into your React app, allowing you to craft a smooth and intuitive navigation experience.

This guide will be your compass as you embark on your React Router journey. I’ll break down the basics, navigate some common routes, and get you started on building a simple page-navigating and user-friendly app.

Installation and Setup

Before we dive into the codes, let’s get React Router on board. Open up your terminal and type the following installation commands:

bash
npm install react-router-dom

This installs the latest version of React Router specifically designed for working within React applications.

Now, we need to introduce React Router to our app’s main captain, the index.js(main.jsx) file. Import the BrowserRouter component from react-router-dom. This component acts as the mastermind behind the navigation, keeping track of the current URL and rendering the corresponding content or component.

Wrap your entire application (usually the App.jsx) inside the BrowserRouter. This ensures that all your components can leverage the routing magic.

Routes and Components

Imagine your app as a collection of islands, each representing a specific view or section. React Router’s Route component acts as your ship, transporting users between these islands.

Here’s a simple example:

import {Routes, Route } from 'react-router-dom';
import Home from './components/Home';
import About from './components/About';
function App() {
return (

<Routes>
<Route path="/" element={<Home />} /> {/* Home route */}
<Route path="/about" element={<About />} /> {/* About route */}
</Routes>

);
}

export default App;

In this example, we have two routes:

1. The first route (path=”/”) points to the home page, represented by the Home component.
2. The second route (path=”/about”) leads to the about page, represented by the About component.

When a user visits your app’s root URL, the home page will be displayed. If they navigate to /about, the About component takes the helm.

Navigation with Links

So, how do users explore these different islands? React Router provides the Link component, which acts like a magical anchor. this is synonymous with the anchor tag in html. Clicking on a Link tells React Router to set sail for the specified route.

Here’s how you can use it:


import { Routes, Route, Link } from 'react-router-dom';
function App() {
return (

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


<Routes>
<Route path="/" element={<Home />} /> {/* Home route */}
<Route path="/about" element={<About />} /> {/* About route */}
</Routes>

);
}

export default App;

This code creates a simple navigation bar with two links. Clicking on the “Home” link will navigate the user to the home page, while clicking on “About” will take them to the about page, all handled seamlessly by React Router.

How about Not Found Routes?

What happens if a user tries to venture off the beaten path and enters a URL that doesn’t match any defined routes? React Router provides a handy way to handle these situations. You can add a catch-all route to display a custom “404 Not Found” page:


<Route path="*" element={<NotFound />} />

This route will match any URL that doesn’t match the previously defined routes, ensuring your users don’t get lost at sea. The asterisk (*) acts as a wildcard, signifying that this route will match any URL path, regardless of what comes after the base URL of your application.

This is just a taste of what React Router offers. As your app grows, you can explore more advanced features like nested routes, dynamic parameters, and programmatic navigation. React Router empowers you to build complex and user-friendly navigation experiences, making your web app a joy to explore.

Remember, this is just the beginning of your React Router adventure. There are many more features to discover, and with practice, you’ll be a master navigator in no time!

--

--