Creating a Sign In and Sign Up Form with React JS: A Guide to ReactJS Login and Registration Form

Posted by


Creating a sign in and sign up form using React JS is a common task in web development. In this tutorial, we will walk through the process of creating a login and registration form using ReactJS. We will create two separate components for the sign in and sign up forms and use state management to handle user input and form submission.

Step 1: Setting up the React App

First, make sure you have Node.js installed on your machine. We will be using the create-react-app tool to bootstrap our project. Open a terminal window and run the following command:

npx create-react-app react-login

This will create a new React project called ‘react-login’. Once the project is created, navigate to the project directory by running:

cd react-login

Step 2: Create the Sign In Component

Inside the src/components folder, create a new file called SignIn.js. In this file, we will create a functional component for the sign in form. Here is the initial code for the SignIn component:

import React, { useState } from 'react';

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

  const handleSubmit = (e) => {
    e.preventDefault();
    // Add your logic for signing in here
  };

  return (
    <div>
      <h2>Sign In</h2>
      <form onSubmit={handleSubmit}>
        <input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} />
        <input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} />
        <button type="submit">Sign In</button>
      </form>
    </div>
  );
};

export default SignIn;

In this component, we are using the useState hook to create state variables for the email and password inputs. We are also handling form submission with the handleSubmit function.

Step 3: Create the Sign Up Component

Now, create a new file called SignUp.js inside the src/components folder. This file will contain the functional component for the sign up form. Here is the initial code for the SignUp component:

import React, { useState } from 'react';

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

  const handleSubmit = (e) => {
    e.preventDefault();
    // Add your logic for signing up here
  };

  return (
    <div>
      <h2>Sign Up</h2>
      <form onSubmit={handleSubmit}>
        <input type="text" placeholder="Name" value={name} onChange={(e) => setName(e.target.value)} />
        <input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} />
        <input type="password" placeholder="Password" value={password} onChange={(e) => setPassword(e.target.value)} />
        <button type="submit">Sign Up</button>
      </form>
    </div>
  );
};

export default SignUp;

Just like the SignIn component, the SignUp component uses the useState hook to create state variables for the name, email, and password inputs. We are also handling form submission with the handleSubmit function.

Step 4: Create the Main App Component

Now, open the App.js file inside the src folder and update it with the following code:

import React from 'react';
import './App.css';
import SignIn from './components/SignIn';
import SignUp from './components/SignUp';

const App = () => {
  return (
    <div className="App">
      <SignIn />
      <SignUp />
    </div>
  );
};

export default App;

In this file, we are importing the SignIn and SignUp components and rendering them inside the main App component.

Step 5: Styling the Forms

To style the sign in and sign up forms, open the App.css file inside the src folder and add the following CSS:

.App {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

form {
  display: flex;
  flex-direction: column;
  margin: 20px;
}

input {
  margin: 5px 0;
  padding: 5px;
}

This CSS will center the forms on the page and add some basic styling to the input fields.

Step 6: Running the App

To run the React app, go back to the terminal and run the following command:

npm start

This will start the development server and open the app in your default web browser. You should now see the sign in and sign up forms on the page. You can test the forms by entering your information and clicking the sign in or sign up buttons.

Congratulations! You have successfully created a sign in and sign up form using React JS. Feel free to customize the forms further by adding validation, error handling, or other features to meet your project requirements.

0 0 votes
Article Rating
44 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@nonamechannel6509
2 months ago

in react we dont use those much of divs for crating a signup and sign in form

@vaddadisudheer4064
2 months ago

It is not executing

@chrisearlgracecumla3350
2 months ago

I'm lost in 3:44

@hrushikesh963
2 months ago

Helpful tutorial, Good Job

@BensonBenson-ds8ux
2 months ago

Anyone having issues with the sign up button not switching after it's switched the first time? That is when sign up is clicked, both sign up and login get the .submit css (purple) but when log in is clicked only log in gets the .submit css (purple)

@weareloved
2 months ago

Thank you!!!!!!!

@phunghoangvnuit
2 months ago

Love the way you teaching <3 Very friendly with beginner

@Fronterralloyd
2 months ago

how do i link the Login form to a button component?

@GoharAkram-gn4hs
2 months ago

Excellent video alot of React concepts are clear .

@anushadaggubati3594
2 months ago

how did you import the assets files in Login-Signup.jsx at 4:47 mins

@Mohsin-Insaniyat
2 months ago

very nice for beginner

@hassanehsani3051
2 months ago

thanks!

@access-yh4dz
2 months ago

"rafc" isn't working. Any help?

@nishudwivedi3478
2 months ago

Meri assets file ke andr images kyu nhi aa rhi h?

@Maamax
2 months ago

Best Vedio!!!

@ashwinkumar504
2 months ago

Awesome …. was very helpful , thanks

@gunelisgandarova3067
2 months ago

Thank you for such a great video

@user-rj7tr5rl8c
2 months ago

click here par click krne par only email function aaye please solution

@Bwalya_ML
2 months ago

It is successfully compiled but still won't run, what could be the reason.

@venunaik1917
2 months ago

bro i have an issue like its compiled successfully and not showing the output, try to solve these issue..!!!!