Create Login Page In React Js

Designing a login page is a crucial aspect of any online platform, as it ensures a safe entry point for individuals to log into their accounts and safeguard their confidential data. In this article, I will walk you through the steps of developing a login page with React JS, a widely-used JavaScript framework for constructing user interfaces.

Setting up the Project

Before we dive into the implementation details, let’s make sure we have the necessary tools and dependencies installed. Firstly, we need to set up a new React project. Open your terminal and navigate to the desired directory where you want to create your project. Run the following command:

npx create-react-app login-page

This command will create a new folder named “login-page” containing the basic structure and dependencies for a React project.

Next, navigate into the project folder:

cd login-page

Now, we can start our React development server:

npm start

This will open the application in your default web browser and display the default React home page.

Creating the Login Component

Now that we have our project set up, let’s start creating our login page component. In the src folder, create a new folder called components. Inside the components folder, create a new file called Login.js.

Open the Login.js file and import React:

import React from 'react';

Next, create a functional component named Login:

function Login() {
  return (
    <div>
      <h2>Login</h2>
    </div>
  );
}

Here, we have created a simple login component with a heading displaying “Login”. This is just the starting point, and we will add more functionality to it as we proceed.

Adding Form Inputs

A login page typically consists of input fields for the user to enter their credentials. Let’s add these input fields to our login component.

function Login() {
  return (
    <div>
      <h2>Login</h2>
      <form>
        <label htmlFor="email">Email:</label>
        <input type="email" id="email" name="email" />
        <label htmlFor="password">Password:</label>
        <input type="password" id="password" name="password" />
        <button type="submit">Log in</button>
      </form>
    </div>
  );
}

In this code, we have added two input fields, one for the email and another for the password. We have also added a submit button to log in.

Handling Form Submission

Now that we have our form inputs, we need to handle the form submission. Let’s update our login component to handle this.

function Login() {
  const handleSubmit = (event) => {
    event.preventDefault();
    // Handle form submission logic here
  }

  return (
    <div>
      <h2>Login</h2>
      <form onSubmit={handleSubmit}>
        <label htmlFor="email">Email:</label>
        <input type="email" id="email" name="email" />
        <label htmlFor="password">Password:</label>
        <input type="password" id="password" name="password" />
        <button type="submit">Log in</button>
      </form>
    </div>
  );
}

In the above code, we have added a handleSubmit function that prevents the default form submission behavior and logs a message to the console. You can replace the console log with your desired login logic.

Styling the Login Page

Our login page is functional, but it lacks visual appeal. Let’s add some styling to make it more attractive. Create a new file called Login.css inside the components folder.

In the Login.css file, add the following CSS code:

.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 50px;
}

Now, import the Login.css file in the Login.js file:

import './Login.css';

Finally, add the login-container class to the outermost div in the Login component:

<div className="login-container">

With this CSS, we have centered the login form vertically and horizontally, giving it a clean and organized look.

Conclusion

Creating a login page in React JS involves creating a login component, adding form inputs, handling form submission, and styling the page. We have covered the basics in this article, and you can further enhance the login page by adding validation, connecting it to a backend server, and implementing authentication.

Remember, the login page is often the first interaction users have with your application, so it’s essential to create a seamless and secure experience. By following the steps outlined in this article, you’ll be on your way to building a robust login page in React JS.

For a live example and the complete source code of the login page, you can visit the GitHub repository.