React Js Redirect To Another Page After Login

Web Development Software

In this article, I will explain how to redirect to another page after a successful login in a React.js application. As a developer who has worked extensively with React.js, I have encountered this scenario multiple times and found a reliable solution. Redirecting the user to a specific page after login is a common requirement in web applications, and React.js provides a straightforward way to accomplish this task.

Setting Up the Login Page

Before we dive into the details of redirecting after login, let’s first set up a basic login page. We need a form that collects the user’s credentials and a submit button to trigger the login process. Here’s a simple example:

const LoginPage = () => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');

const handleSubmit = (e) => {
// Log in logic goes here

return (

setEmail(} />

setPassword(} />


Now that we have our basic login page set up, let’s move on to the redirect logic.

Redirecting After Login

In order to redirect the user to another page after a successful login, we need to keep track of the login status. One common approach is to use React’s useState hook to store the login status. Let’s add a new state variable called isLoggedIn and set it to false initially:

const [isLoggedIn, setIsLoggedIn] = useState(false);

Next, we need to update the handleSubmit function to handle the login logic and update the isLoggedIn state variable when the login is successful. Once the login is successful, we can redirect the user to another page using React Router’s useHistory hook:

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

const LoginPage = () => {
const history = useHistory();

const handleSubmit = (e) => {

// Perform login logic here

history.push('/dashboard'); // Redirect to the dashboard page after login

// of the code

Here, we import the useHistory hook from the ‘react-router-dom’ package and initialize it with const history = useHistory();. Then, inside the handleSubmit function, after the login logic is successful and we set isLoggedIn to true, we use history.push('/dashboard'); to navigate to the dashboard page.


Redirecting to another page after login in a React.js application is a common requirement, and with React Router and the useState and useHistory hooks, it becomes a straightforward task. By keeping track of the login status using the useState hook, and using the useHistory hook to redirect the user to the desired page, we can provide a smooth user experience after a successful login.

Remember to ensure that the user is redirected to a page that is appropriate for their access level and role within your application. Security is paramount, and proper authentication and authorization measures should be implemented to protect sensitive data and ensure a secure user experience.

We have covered the basics of redirecting after login in a React.js application, and I hope this article has been helpful in understanding the process. Happy coding!