Mastering React Routing: Learn Navigation and Routing in React Apps
Last Updated :
23 Jul, 2025
React Routing is a technique used to handle navigation within a React application. It enables users to move between different views, pages, or components without refreshing the entire page, which is a key feature of Single Page Applications (SPAs).
In this article, we will explore the essential concepts of routing in React applications. React Router provides a powerful and flexible way to handle navigation between pages in Single Page Applications (SPAs).
What is Navigation in React?
Navigation refers to the process of moving between different views or sections within a React application. In React, most applications are developed as Single Page Applications (SPA), where the entire application is loaded initially, and subsequent navigation doesn't require reloading pages from the server. Instead, routing is handled on the client side, enabling smooth transitions between different components or views without full page reloads.
To implement routing in React we do not have in-built modules but instead, we use the react-router-dom module after installing and importing it.
Syntax
// Installing
npm i react-router-dom
// Importing
import { BrowserRouter } from 'react-router-dom';
- Installing: npm i react-router-dom installs React Router for routing in React.
- Importing: import { BrowserRouter } from 'react-router-dom'; imports BrowserRouter to enable routing in your app.
Programmatically Navigate in React
Programmatic navigation allows you to navigate between different routes based on actions, such as button clicks or form submissions. This method provides more control over routing, enabling navigation in response to events triggered by the user.
Approach
- Create 2 basic pages between which you want to redirect
- Create buttons on each page to redirect the user
- Import the useNavigate hook provided with react-router-dom
- Use this hook on the onClick event button to redirect
JavaScript
// App.js
import { BrowserRouter, Routes, Route } from "react-router-dom";
import "./App.css";
import AboutUs from "./components/AboutUs";
import ContactUs from "./components/CotactUs";
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route exact path="/" element={<AboutUs />} />
<Route exact path="/contactus" element={<ContactUs />} />
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
JavaScript
// AboutUs.js
import React from "react";
import { useNavigate } from "react-router-dom";
function AboutUs() {
const nav = useNavigate();
return (
<div>
<h2>GeeksforGeeks is a computer science portal for geeks!</h2>
Read more about us at :
<a href="https://www.geeksforgeeks.org/about/">
https://www.geeksforgeeks.org/about/
</a>
<br></br>
<br></br>
<button
onClick={() => {
nav("contactus");
}}
>
Click Here to check contact details
</button>
</div>
);
}
export default AboutUs;
JavaScript
// ContactUs.js
import React from "react";
import { useNavigate } from "react-router-dom";
function ContactUs() {
const nav = useNavigate();
return (
<div>
<address>
You can find us here:
<br />
GeeksforGeeks
<br />
5th & 6th Floor, Royal Kapsons, A- 118, <br />
Sector- 136, Noida, Uttar Pradesh (201305)
</address>
<br></br>
<br></br>
<button
onClick={() => {
nav(-1);
}}
>
Click Here to Go Back
</button>
</div>
);
}
export default ContactUs;
Output

In this example
- App.js: This file displays the AboutUs component.
- AboutUs.js: This components creates the link to Contact Us page using Button.
- ContactUs.js: This component has a button to send back to previous page.
Dynamic Routing with React router
Adding Link routes gets very lengthy when there are multiple pages, so we use the concept of Dynamic Routing to reduce the lines of code and make the code shorter. Dynamic routing allows you to define routes dynamically based on certain conditions.
Approach
- Create a page which will create Link components using map
- Create a page which will display data dynamically using useParams
- Create a dynamic Route component which passes the id as a parameter
JavaScript
// App.js
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
import CourseDetails from "./components/CourseDetails";
function App() {
const courses = ["JavaScript", "React", "HTML", "DSA"];
return (
<BrowserRouter>
<h1>Dynamic Routing with React</h1>
<ul>
{courses.map((course) => {
return (
<li key={course}>
<Link to={`courses/${course}`}>{course}</Link>
</li>
);
})}
</ul>
<Routes>
<Route path="courses/:courseId" element={<CourseDetails />} />
</Routes>
</BrowserRouter>
);
}
export default App;
JavaScript
// components/CourseDetails.js
import { useParams } from "react-router-dom";
function CourseDetails() {
const { courseId } = useParams();
return (
<div>
<h1>This is {courseId} course</h1>
</div>
);
}
export default CourseDetails;
Output

In this code
- App.js: This file creates Link component dynamically and sends course name as a parameter
- CourseDetails.js: This file accesses the course as a parameter and passes it.
Handling 404 Errors (Page Not Found)
Sometimes user types a URL which does not exist in the website and the router fails and shows an error. To solve this problem we create a universal Route component which redirects to Link not found page whenever incorrect URL is passed.
Approach
- Create 3 basic pages where you want to add navigation
- Create a Navbar to handle navigation between the pages
- Create NoPageFound.js file to handle routing for all incorrect routing
- Add a path for non-configured routes which will be redirected to NoPageFound file
- the path with '*' handles all non-configured routes
JavaScript
// App.js
import logo from "./logo.svg";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import "./App.css";
import NavBar from "./components/Navbar";
import Home from "./components/Home";
import AboutUs from "./components/AboutUs";
import ContactUs from "./components/CotactUs";
import NoPageFound from "./components/NoPageFound";
function App() {
return (
<div className="App">
<BrowserRouter>
<NavBar />
<Routes>
<Route exact path="/" element={<Home />} />
<Route exact path="/about" element={<AboutUs />} />
<Route exact path="/contact" element={<ContactUs />} />
<Route path="*" element={<NoPageFound />} />
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
JavaScript
// AboutUs.js
import React from "react";
function AboutUs() {
return (
<div>
<h2>GeeksforGeeks is a computer science portal for geeks!</h2>
Read more about us at :
<a href="https://www.geeksforgeeks.org/about/">
https://www.geeksforgeeks.org/about/
</a>
</div>
);
}
export default AboutUs;
JavaScript
// ContactUs.js
import React from "react";
function ContactUs() {
return (
<address>
You can find us here:
<br />
GeeksforGeeks
<br />
5th & 6th Floor, Royal Kapsons, A- 118, <br />
Sector- 136, Noida, Uttar Pradesh (201305)
</address>
);
}
export default ContactUs;
JavaScript
// Home.js
import React from "react";
function Home() {
return <h1>Welcome to the world of Geeks!</h1>;
}
export default Home;
JavaScript
// Navbar.js
import { Link } from "react-router-dom";
export default function NavBar() {
return (
<div>
<ul className="r">
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About Us</Link>
</li>
<li>
<Link to="/contact">Contact Us</Link>
</li>
</ul>
</div>
);
}
JavaScript
// NoPageFound.js
export default function NoPageFound() {
return <h1>Error 404: Page Not Found</h1>;
}
Output
In this code
- App.js: This file imports all the components and applies routing to them
- AboutUs.js: This file contains the About Us Page
- ContactUs.js: This file displays the Contact Us Page
- Home.js: This component acts as the home page
- Navbar.js: This component has the Navbar to redirect to all linking pages
- NoPageFound.js: This page is displayed when invalid links are added.
Conclusion
Learning navigation and routing in React is essential for building smooth, interactive web applications. React Router makes it easy to manage navigation between different pages or views without reloading the page, helping you create seamless single-page applications. By understanding how to set up basic routes, work with dynamic paths, navigate programmatically, and use nested routes, you can build more efficient and user-friendly React apps that offer a smooth browsing experience for users.
Similar Reads
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
React Fundamentals
React IntroductionReactJS is a component-based JavaScript library used to build dynamic and interactive user interfaces. It simplifies the creation of single-page applications (SPAs) with a focus on performance and maintainability.React.jsWhy Use React?Before React, web development faced issues like slow DOM updates
7 min read
React Environment SetupTo run any React application, we need to first setup a ReactJS Development Environment. In this article, we will show you a step-by-step guide to installing and configuring a working React development environment.Pre-requisite:We must have Nodejs installed on our PC. So, the very first step will be
3 min read
React JS ReactDOMReactDOM is a core React package that provides methods to interact with the Document Object Model, or DOM. This package allows developers to access and modify the DOM. It is a package in React that provides DOM-specific methods that can be used at the top level of a web app to enable an efficient wa
3 min read
React JSXJSX stands for JavaScript XML, and it is a special syntax used in React to simplify building user interfaces. JSX allows you to write HTML-like code directly inside JavaScript, enabling you to create UI components more efficiently. Although JSX looks like regular HTML, itâs actually a syntax extensi
5 min read
ReactJS Rendering ElementsIn this article we will learn about rendering elements in ReactJS, updating the rendered elements and will also discuss about how efficiently the elements are rendered.What are React Elements?React elements are the smallest building blocks of a React application. They are different from DOM elements
3 min read
React ListsReact Lists are used to display a collection of similar data items like an array of objects and menu items. It allows us to dynamically render the array elements and display repetitive data.Rendering List in ReactTo render a list in React, we will use the JavaScript array map() function. We will ite
5 min read
React FormsForms are an essential part of any application used for collecting user data, processing payments, or handling authentication. React Forms are the components used to collect and manage the user inputs. These components include the input elements like text field, check box, date input, dropdowns etc.
5 min read
ReactJS KeysA key serves as a unique identifier in React, helping to track which items in a list have changed, been updated, or removed. It is particularly useful when dynamically creating components or when users modify the list. In this article, we'll explore ReactJS keys, understand their importance, how the
5 min read
Components in React
React ComponentsIn React, React components are independent, reusable building blocks in a React application that define what gets displayed on the UI. They accept inputs called props and return React elements describing the UI.In this article, we will explore the basics of React components, props, state, and render
4 min read
ReactJS Functional ComponentsIn ReactJS, functional components are a core part of building user interfaces. They are simple, lightweight, and powerful tools for rendering UI and handling logic. Functional components can accept props as input and return JSX that describes what the component should render.What are Reactjs Functio
5 min read
React Class ComponentsClass components are ES6 classes that extend React.Component. They allow state management and lifecycle methods for complex UI logic.Used for stateful components before Hooks.Support lifecycle methods for mounting, updating, and unmounting.The render() method in React class components returns JSX el
4 min read
ReactJS Pure ComponentsReactJS Pure Components are similar to regular class components but with a key optimization. They skip re-renders when the props and state remain the same. While class components are still supported in React, it's generally recommended to use functional components with hooks in new code for better p
4 min read
ReactJS Container and Presentational Pattern in ComponentsIn this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
ReactJS PropTypesIn ReactJS PropTypes are the property that is mainly shared between the parent components to the child components. It is used to solve the type validation problem. Since in the latest version of the React 19, PropeTypes has been removed. What is ReactJS PropTypes?PropTypes is a tool in React that he
5 min read
React Lifecycle In React, the lifecycle refers to the various stages a component goes through. These stages allow developers to run specific code at key moments, such as when the component is created, updated, or removed. By understanding the React lifecycle, you can better manage resources, side effects, and perfo
7 min read
React Hooks
Routing in React
Advanced React Concepts
React Projects