How to Redirect to Another Page in ReactJS ?
Last Updated :
06 May, 2025
In ReactJS, when you need to move users from one page to another, we can use the built-in React Router library, which is designed specifically for handling navigation within a React application.
Approach
- Use the Navigate component: In React Router v6, the Navigate component helps us to redirect users from one route to another easily. It's declarative, meaning we can set up redirects directly within your component.
- Declarative Redirection: We can handle redirection directly in the component's render logic, making it straightforward and easy to understand.
- Use useNavigate for Programmatic Navigation: For more control, we can use the useNavigate hook to redirect users based on specific actions or conditions (like form submissions or state changes). This enables dynamic navigation in your app.
Steps To Redirect to Another Page
Step 1: Create a basic react app using the following command.
npm create-react-app myreactapp
cd myreactapp
Step 2: Install the required dependencies using the following command.
npm i react-router-dom
Project Structure: After creating components, the project structure will look like this
Folder structureDependencies:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-icons": "^4.11.0",
"react-router-dom": "^6.16.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Now let's understand this with the help of example:
App.js
import "./App.css";
import {
BrowserRouter as Router,
Routes,
Route,
Navigate,
} from "react-router-dom";
import Home from "./components/Home";
import About from "./components/About";
import ContactUs from "./components/ContactUs";
function App() {
return (
<>
<Router>
<Routes>
<Route
exact
path="/"
element={<Home />}
/>
<Route
path="/about"
element={<About />}
/>
<Route
path="/contactus"
element={<ContactUs />}
/>
<Route
path="*"
element={<Navigate to="/" />}
/>
</Routes>
</Router>
</>
);
}
export default App;
Home.js
import React from "react";
import { Link } from "react-router-dom";
const Home = () => {
return (
<div>
<h1>Home Page</h1>
<br />
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/contactus">Contact Us</Link>
</li>
</ul>
</div>
);
};
export default Home;
About.js
import React from "react";
const About = () => {
return (
<div>
<h1>About Page</h1>
</div>
);
};
export default About;
ContactUs.js
import React from "react";
const ContactUs = () => {
return (
<div>
<h1>Contact Us Page</h1>
</div>
);
};
export default ContactUs;
Output
In this example
- Sets up routing using React Router.
- Defines routes for Home, About, and Contact Us pages.
- Redirects to the Home page if the route doesn’t match any defined paths.
- Displays navigation links to the Home, About, and Contact Us pages.
- Displays "About Page" heading when the /about route is visited.
- Displays "Contact Us Page" heading when the /contactus route is visited.
Methods to Redirect in ReactJS
1. Using <Link> for Navigation
The most common way to navigate between pages in React is by using the <Link> component provided by React Router. This component works similarly to an anchor (<a>) tag in regular HTML but prevents a full page reload.
App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from './Home';
import About from './About';
const App = () => {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
};
export default App;
About.js
import React from 'react';
const About = () => {
return (
<div>
<h1>About Page</h1>
</div>
);
};
export default About;
Home.js
import React from 'react';
import { Link } from 'react-router-dom';
const Home = () => {
return (
<div>
<h1>Home Page</h1>
<Link to="/about">Go to About Page</Link>
</div>
);
};
export default Home;
Output
cIn this example
- Uses React Router to navigate between Home and About pages without page reloads.
- In app component, we sets up routing with React Router for Home (/) and About (/about) pages.
- About Component displays "About Page" when navigating to /about.
- Home Component displays "Home Page" and a link to the About Page.
2. Programmatic Navigation Using useNavigate()
When navigation needs to happen based on specific conditions or actions, like submitting a form or completing an event, it’s called programmatic navigation. React Router provides the useNavigate() hook to handle this. It allows redirecting users to another page automatically without requiring a link or button click.
App.js
import React from 'react';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import Home from './Home';
import About from './About';
const App = () => {
return (
<Router>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
</Routes>
</Router>
);
};
export default App;
Home.js
import React from 'react';
import { useNavigate } from 'react-router-dom';
const Home = () => {
const navigate = useNavigate();
const handleRedirect = () => {
navigate('/about');
};
return (
<div>
<h1>Home Page</h1>
<button onClick={handleRedirect}>Go to About Page</button>
</div>
);
};
export default Home;
About.js
import React from 'react';
const About = () => {
return (
<div>
<h1>About Page</h1>
</div>
);
};
export default About;
Output
In this example
- App.js: Sets up routing using BrowserRouter and Routes and defines two routes: / for Home and /about for About.
- Home.js: Displays a "Home Page" heading. Has a button that, when clicked, uses useNavigate() to redirect to /about.
- About.js: Displays a "About Page" heading.
3. Redirecting After an Event or Action (Using useEffect)
Redirecting can happen after an action is completed, like submitting a form or finishing a task. To do this, the useEffect hook can be used along with useNavigate() to automatically redirect to another page once the action is done.
App.js
import React from 'react';
import { Navigate } from 'react-router-dom';
const LoginPage = () => {
const isLoggedIn = false;
if (!isLoggedIn) {
return <Navigate to="/login" />;
}
return (
<div>
<h1>Welcome to the Dashboard</h1>
</div>
);
};
export default LoginPage;
FormPage.js
import React, { useState, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
const FormPage = () => {
const [formData, setFormData] = useState('');
const navigate = useNavigate();
const handleSubmit = (e) => {
e.preventDefault();
setFormData('Form Submitted');
};
useEffect(() => {
if (formData) {
navigate('/thank-you');
}
}, [formData, navigate]);
return (
<div>
<h1>Form Page</h1>
<form onSubmit={handleSubmit}>
<input type="text" placeholder="Enter something" />
<button type="submit">Submit</button>
</form>
</div>
);
};
export default FormPage;
ThankYouPage.js
import React from 'react';
const ThankYouPage = () => {
return (
<div>
<h1>Thank You for Submitting the Form!</h1>
</div>
);
};
export default ThankYouPage;
Output
In this example,
- LoginPage: Redirects to /login if not logged in, otherwise shows dashboard.
- FormPage: User submits a form, then redirects to /thank-you.
- ThankYouPage: Displays a thank-you message after form submission.
Conclusion
In ReactJS, navigating between pages is essential for building interactive apps, and React Router simplifies this process. We can handle navigation in several ways: using the <Navigate> component for declarative redirection, the useNavigate hook for programmatic navigation based on actions or conditions, and useEffect to redirect after events like form submissions.
Similar Reads
How to Redirect to Another Page in VueJS ? Redirecting to another page is a common requirement in web development especially in the Vue.js applications. It allows users to navigate seamlessly between the different views or pages within the application. These are the following methods: Table of Content Using router.push() methodUsing the rout
2 min read
How to prevent page from rendering before redirect in ReactJS ? ReactJS is a popular front-end framework that allows developers to create dynamic and responsive web applications. One common issue that developers face when working with ReactJS is preventing a page from rendering before a redirect is complete. This can lead to issues such as slow loading times, br
4 min read
How to Redirect to Another Webpage using JavaScript? JavaScript can redirects the users from current page to another webpage (different URL) with and without requiring any manual action (like clicking a link). To redirect to another weboage in JavaScript we need to manipulate the window.location object and it will allow us to navigate programmatically
2 min read
How to Redirect 404 errors to a page in Express.js ? Express Js is a web application framework based on Node.js web server functionality that helps us to create web servers with lesser complexity and in a well-organized manner. Express provides routing services that help us in creating application endpoints that respond based on the HTTP request metho
3 min read
How to Redirect in Next.js ? NextJS is a React framework that is used to build full-stack web applications. It is used both for front-end as well as back-end. It comes with a powerful set of features to simplify the development of React applications. In this article, we will learn about How to Redirect in NextJS with different
4 min read
How to setup 404 page in React Routing? When you're building a React app with routing, it's important to handle cases where users land on a page that doesn't exist. A 404 page is a good way to inform users that the page theyâre looking for cannot be found. Prerequisites:NPM & Node JSReact JSReact-router-domApproach to Set Up a 404 Pag
4 min read
How to force redirect to a particular route in angular? Introduction: We can use the property binding concept and we can pass query parameters to routerLink. Using Property binding we can bind queryParams property and can provide the required details in object. Property Binding: It is a concept where we use square bracket notation to bind data to Documen
3 min read
How to redirect in React with Typescript ? Navigating users seamlessly through a React application is a fundamental aspect of creating a smooth and intuitive user experience. In this article, we delve into the world of redirects in React, specifically addressing the use of TypeScript for enhanced type safety and developer productivity.Prereq
2 min read
How to redirect back to original URL in Node.js ? Node.js with the help of Express, supports web page routing. This means that when the client makes different requests, the application is routed to different web pages depending upon the request made and the routing methods defined. To learn more about Node.js routing and its implementation, refer t
3 min read
How to handle redirects in Express JS? Express JS uses redirects to send users from one URL to another. This can be done for various reasons, such as handling outdated URLs or guiding users through a process. In ExpressJS, you define routes and use them to direct users to the desired URL. It's a way to keep your application organized and
2 min read