How to Navigate on Path by Button Click in React Router ?
Last Updated :
09 Jan, 2025
Navigation in React JS is done by implementing the routing between components using react-router-dom. To set navigation for components or events like button click, we can use the useHistory Hook provided in the react-router-dom v5.
Prerequisites:
Approach
Navigation in single-page applications is crucial for user experience. To navigate on the path by clicking a button we use the useHistory hook from react-router-dom v5 (i.e. useNavigate in v6). We will set the routes for specific components and navigate between them using a button.
Note: In react-router-dom v6 useHistory is replaced by useNavigate hook.
Steps to Create React Application and Install Modules
Step 1: Make a project directory, head over to the terminal, and create a react app named “ cs portal ” using the following command.
npx create-react-app cs-portal
Step 2: Move to the project directory.
cd cs-portal
Project Structure:

Final Project structure
The updated dependencies in package.json file.
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"bootstrap": "^4.6.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^5.3.4",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: This example uses useHistory hook to navigate when the button is clicked.
CSS
/* Filename: App.css */
* {
text-align: center;
}
.logo {
margin: auto;
}
.jumbotron {
margin: 100px auto;
max-width: 50%;
text-align: center;
}
.card {
width: 18rem;
margin: 100px auto;
}
JavaScript
// Filename - App.js
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
} from "react-router-dom";
import Homepage from "./Components/Homepage";
import Courses from "./Components/Courses";
import "bootstrap/dist/css/bootstrap.min.css";
import "./App.css";
import Navbar from "./Components/Navbar";
function App() {
return (
<>
<Navbar />
<Router>
<Switch>
<Route
exact
path="/"
component={Homepage}
/>
<Route
exact
path="/courses"
component={Courses}
/>
</Switch>
</Router>
</>
);
}
export default App;
JavaScript
// Filename - Components/Navbar
import React from "react";
function Navbar() {
return (
<div>
<nav className="navbar navbar-light bg-light">
<img
src=
"https://media.geeksforgeeks.org/wp-content/uploads/20220125183244/g.png"
width="300"
height="75"
className="d-inline-block align-top logo"
alt="GfG.png"
/>
GeeksforGeeks
</nav>
</div>
);
}
export default Navbar;
JavaScript
// Filename - Components/Homepage.js
import React from 'react'
import { useHistory } from "react-router-dom";
import "../App.css";
function Homepage() {
const history = useHistory();
const coursesPage = () => {
history.push("/courses")
}
return (
<>
<div className="jumbotron text-center">
<h1 className="display-4">Hello,Geeks</h1>
<p className="lead">
Geeks for Geeks is a Computer Science portal.
It contains well written, well thought and well
explained computer science and programming articles
</p>
<hr className="my-4" />
<p>
Real-time Live and self paced courses carefully
curated for you !
</p>
<p className="lead">
<button className="btn btn-success"
onClick={coursesPage}>Explore Courses
</button>
</p>
</div>
</>
)
}
export default Homepage
JavaScript
// Filename - Components/Courses
import React from "react";
import "../App.css";
import { useHistory } from "react-router-dom";
function Courses() {
const history = useHistory();
const home = () => {
history.push("/");
};
return (
<>
<div className="card ">
<h1>Courses </h1>
<ul className="list-group list-group-flush">
<li className="list-group-item">
Data Structures & Algorithms
</li>
<li className="list-group-item">
Competitive Programming
</li>
<li className="list-group-item">
Full Stack Development
</li>
<li className="list-group-item">
Java Backend
</li>
</ul>
</div>
<button
className="btn btn-success"
onClick={home}
>
Back to Home
</button>
</>
);
}
export default Courses;
Step to run the application: Now let us run our application by using the following command.
npm start
Output: By default, the React project will run on port 3000. You can access it at localhost:3000 on your browser.
You can watch the GeeksforGeeks video to learn more about React JS routing.
Conclusion
To navigate on path using components and events we can define the routes first and implement the navigation using useHistory and useNavigate depending on the verson or react router used.
Similar Reads
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
React Tutorial
React is a JavaScript Library known for front-end development (or user interface). It is popular due to its component-based architecture, Single Page Applications (SPAs), and Virtual DOM for building web applications that are fast, efficient, and scalable. Applications are built using reusable compo
8 min read
REST API Introduction
REST API stands for REpresentational State Transfer API. It is a type of API (Application Programming Interface) that allows communication between different systems over the internet. REST APIs work by sending requests and receiving responses, typically in JSON format, between the client and server.
7 min read
HTML Interview Questions and Answers
HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers
NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
What is an API (Application Programming Interface)
In the tech world, APIs (Application Programming Interfaces) are crucial. If you're interested in becoming a web developer or want to understand how websites work, you'll need to familiarize yourself with APIs. Let's break down the concept of an API in simple terms. What is an API?An API is a set of
10 min read