Member-only story

Simplest way to add routing in React

Rizwan
2 min readJan 19, 2025

--

In React, you can implement routing using the React Router library. React Router is a popular and the simpler library for managing routing in single-page applications built with React.

Here are the basic steps for implementing routing in a React application using React Router:

1. Install React Router:

npm install react-router-dom

2. Wrap your application with the BrowserRouter component:

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

function App() {
return (
<BrowserRouter>
{/* Your application code goes here */}
</BrowserRouter>
);
}

3. Define your routes using the Route component:

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

function App() {
return (
<BrowserRouter>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
</BrowserRouter>
);
}

In this example, we’ve defined three routes using the Route component. The exact prop is used on the first route to ensure that it matches only the exact path (/), and not any paths that start with /.

4. Use the Link component…

--

--

Rizwan
Rizwan

Written by Rizwan

Senior Software Engineer | Mentor | Tech Enthusiast | Passionate about building scalable solutions, exploring the latest technologies.

No responses yet

Write a response