Create an Elegant Login and Register Form on React

Create an Elegant Login and Register Form on React

In this React.js tutorial we will go through the process of creating an elegant Login/Register form box on React we will be using create-react-app for easy setup and project configuration so we can quickly get up and running. If you're new to React or still not really experienced with most aspects of react plus you may need to get more familiar with designing User Interfaces on React.

Getting Started

If you're new to React or still not really experienced with most aspects of react plus you may need to get more familiar with designing User Interfaces on React so in this tutorial we will go through the process of creating an elegant Login/Register form box on React we will be using create-react-app for easy setup and project configuration so we can quickly get up and running.

You can grab the full source code of the application from this Github Repo.

You may wanna grap the CSS Style files from the Github Repo for easier follow up

If you would like to use SASS in your create-react-app generated application you need to install node sass as a dependency and change the style filename extension to either (.sass|.scss).

npm install node-sass --save-dev
Create Login and Register Box

Login and Register boxes are going to be a separate Components so later we can easily manipulate them and decide which one to render or not depending on the application state plus it is going to make it much easier for applying some cool animation while transitioning between states.

So create a components/ folder inside src/ and put inside it the login/ component folder this will hold the login and register standalone components.

/* login.jsx */
import React from "react";
import loginImg from "../../login.svg";

export class Login extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div className="base-container" ref={this.props.containerRef}>
        <div className="header">Login</div>
        <div className="content">
          <div className="image">
            <img src={loginImg} />
          </div>
          <div className="form">
            <div className="form-group">
              <label htmlFor="username">Username</label>
              <input type="text" name="username" placeholder="username" />
            </div>
            <div className="form-group">
              <label htmlFor="password">Password</label>
              <input type="password" name="password" placeholder="password" />
            </div>
          </div>
        </div>
        <div className="footer">
          <button type="button" className="btn">
            Login
          </button>
        </div>
      </div>
    );
  }
}

Also, make sure to grab the login logo used in this example you can grab it alongside many other open-source SVG Illustrations from undraw

I will be putting it under src/login.svg.

You can use any other structure for adding or either removing useless content from the login form depending on your needs.

For the Login class component, it renders a basic container and a form the style will be contained in login/style.scss

We also need to put the Register.jsx.

/* Register.jsx */
import React from "react";
import loginImg from "../../login.svg";

export class Register extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <div className="base-container" ref={this.props.containerRef}>
        <div className="header">Register</div>
        <div className="content">
          <div className="image">
            <img src={loginImg} />
          </div>
          <div className="form">
            <div className="form-group">
              <label htmlFor="username">Username</label>
              <input type="text" name="username" placeholder="username" />
            </div>
            <div className="form-group">
              <label htmlFor="email">Email</label>
              <input type="text" name="email" placeholder="email" />
            </div>
            <div className="form-group">
              <label htmlFor="password">Password</label>
              <input type="text" name="password" placeholder="password" />
            </div>
          </div>
        </div>
        <div className="footer">
          <button type="button" className="btn">
            Register
          </button>
        </div>
      </div>
    );
  }
}

We only needed to add email filed for the registration process.

Now let's create the style for the login/register components.

.base-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;

  .header {
    font-size: 24px;
    font-family: "Open Sans", sans-serif;
  }

  .content {
    display: flex;
    flex-direction: column;

    .image {
      width: 21em;
      img {
        width: 100%;
        height: 100%;
      }
    }

    .form {
      margin-top: 2em;
      display: flex;
      flex-direction: column;
      align-items: center;

      .form-group {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        width: fit-content;
        label {
          font-size: 20px;
        }
        input {
          margin-top: 6px;
          min-width: 18em;
          height: 37px;
          padding: 0px 10px;
          font-size: 16px;
          font-family: "Open Sans", sans-serif;
          background-color: #f3f3f3;
          border: 0;
          border-radius: 4px;
          margin-bottom: 31px;
          transition: all 250ms ease-in-out;
          &:hover {
            //background-color: #ffffff;
            //box-shadow: 0px 0px 14px 0.3px #0e81ce96;
          }

          &:focus {
            outline: none;
            box-shadow: 0px 0px 12px 0.8px #3474dbb2;
          }
        }
      }
    }
  }
  .footer {
    margin-top: 3em;
  }
}

The best thing about SASS or any other pre-processor is you can use nested child elements to make sure only that child element under it's a specified parent will have the style applied. However, this can also be dangerous if you're trying to applying custom style names to elements that don't meet the nested structure.

We also used flex-box for easily aligning children alongside the x and y-axis both vertically and horizontally since we need to fully centre the login box container (base-container children).

Now, let's try to re-export the Login and Register components using ES6 re-exporting power so we could easily import them from the App.jsx

So simply create index.tsx under login/ and re-export login/register from there.

/* index.tsx */
import "./style.scss";
//Import and Re-export Login/Register Components
export { Login } from "./login";
export { Register } from "./register";
Render & Change State

Now under App.jsx we will be rendering the Login/Admin components and we need to tweak the state to apply an animated transition between both components.

/* App.jsx */

import React from "react";
import "./App.scss";
import { Login, Register } from "./components/login/index";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isLogginActive: true
    };
  }

  render() {
    const { isLogginActive } = this.state;
    const current = isLogginActive ? "Register" : "Login";
    return (
      <div className="App">
        <div className="login">
          <div className="container" ref={ref => (this.container = ref)}>
            {isLogginActive && (
              <Login containerRef={ref => (this.current = ref)} />
            )}
            {!isLogginActive && (
              <Register containerRef={ref => (this.current = ref)} />
            )}
          </div>
        </div>
      </div>
    );
  }
}

We use isLogginActive state to check if we login component is currently being rendered or not (Register component is being rendered) so this way we can transition between login and register components.

We also reference the container div so we can easily use it later to apply some cool transition between moving from login to register and vice-versa.

Toggle with Animation

We need to add the right side component which will allow us to toggle between the two states of login/register components and apply to its cool moving transition.

So let's create a simple RFC (React Functional Component).

/* App.jsx */
/*We also add custom container ref to pass ref to the App Component so we can easily manipulate the right-side container*/
const RightSide = props => {
  return (
    <div
      className="right-side"
      ref={props.containerRef}
      onClick={props.onClick}
    >
      <div className="inner-container">
        <div className="text">{props.current}</div>
      </div>
    </div>
  );
};

We pass in the current prop which holds either Login or Register as the next state (Component) we want to transition to.

...
render() {
    const { isLogginActive } = this.state;
    const current = isLogginActive ? "Register" : "Login";
    return (
      <div className="App">
        <div className="login">
          <div className="container" ref={ref => (this.container = ref)}>
            {isLogginActive && (
              <Login containerRef={ref => (this.current = ref)} />
            )}
            {!isLogginActive && (
              <Register containerRef={ref => (this.current = ref)} />
            )}
          </div>
          <RightSide
            current={current}
            containerRef={ref => (this.rightSide = ref)}
          />
        </div>
      </div>
    );
  }
...

We detect the current (the next component to move to) simply by looking at the isLogginActive.

We also set the containerRef to hold the right-side container ref on the App class.

Now, we need to detect the click event on the right-side container so we could toggle the other component by applying some cool transition.

So add a new method on the App class that will handle the click that occurs on the right side component (make sure to pass the onClick event inside the component otherwise the event listener won't be registered).

changeState() {
  //ES6 Object Destructuring
  const { isLogginActive } = this.state;

  //We togglet component classes depending on the current active state 
  if (isLogginActive) {
    //Right side for login
    this.rightSide.classList.remove("right");
    this.rightSide.classList.add("left");
  } else {
    //Left side for Register 
    this.rightSide.classList.remove("left");
    this.rightSide.classList.add("right");
  }
  //Of course we need to toggel the isLogginActive by inversing it's previous state 
  this.setState(prevState => ({ isLogginActive: !prevState.isLogginActive }));
}

We simply check for the current active state which is either loggin is active or register component is active and we apply custom classes so the rightSide component could apply custom styles and transition animations, you can check the App.scss for more information on the applied style.

And most importantly we need to make sure we toggle the current state so we inverse it depending on the previous state value.

Finally, make sure to bind the click event to the current method.

...
<RightSide
  current={current}
  currentActive={currentActive}
  containerRef={ref => (this.rightSide = ref)}
  onClick={this.changeState.bind(this)}
/>
...

Make sure to bind the current class context.

and onComponentWillMount hook set a default value.

componentDidMount() {
  //Add .right by default
  this.rightSide.classList.add("right");
}

Save and reload the page you should get the cool transition playing when toggling between states.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

What’s new in HTML6

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Hire React Js Developer from Expert React JS Development Company

Hire React Js Developer from Expert React JS Development Company

Are you looking to [hire React JS developer](https://www.mobiwebtech.com/react-js-development-company/ "hire React JS developer") from a prestigious and reliable React JS development company? Visit at Mobiweb Technologies here we have a big team...

Are you looking to hire React JS developer from a prestigious and reliable React JS development company? Visit at Mobiweb Technologies here we have a big team of expert React JS developers, you can hire them on an hourly, weekly or monthly basis for the successful completion of your dream project.

Learn React.js: Front-End Web Development for Beginners

Learn React.js: Front-End Web Development for Beginners

Learn React.js - Frontend Web Development for Beginners. In this React tutorial, you will learn the core fundamentals of React JS and Modern JavaScript so that you can start building lightning fast web apps using React JS. Learn Modern JavaScript and React JS from absolute scratch. Learn to make AJAX requests to get data from remote API and display into your web application. Learn React JS, which is one of the most exciting technology of recent time

Learn React.js - Frontend Web Development for Beginners - Web Development in 2019. In this video you will learn the core fundamentals of React JS and Modern JavaScript so that you can start building lightning fast web apps using React JS.

In this video, you will learn Modern JavaScript and React JS from absolute scratch. you will learn to make AJAX requests to get data from remote API and display into your web application.

So if you are ready to learn React JS, which is one of the most exciting technology of recent time

Thanks for watching

If you liked this post, share it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

React - The Complete Guide (incl Hooks, React Router, Redux)

**☞ **Modern React with Redux [2019 Update]

**☞ **50+ Java Interview Questions for Programmers

**☞ **Top 100 Python Interview Questions and Answers

**☞ **100+ Basic Machine Learning Interview Questions and Answers

**☞ **Introduction to Java String Interview Questions and Answers

**☞ **Best 50 React Interview Questions and Answers in 2019

**☞ **Top 50+ SQL Interview Questions and Answers in 2019

**☞ **Best Java Microservices Interview Questions In 2019

**☞ **Best 50 Nodejs interview questions from Beginners to Advanced in 2019

**☞ **100+ Java Interview Questions and Answers In 2019

Top Vue.js Developers in USA

Top Vue.js Developers in USA

Vue.js is an extensively popular JavaScript framework with which you can create powerful as well as interactive interfaces. Vue.js is the best framework when it comes to building a single web and mobile apps.

We, at HireFullStackDeveloperIndia, implement the right strategic approach to offer a wide variety through customized Vue.js development services to suit your requirements at most competitive prices.

Vue.js is an open-source JavaScript framework that is incredibly progressive and adoptive and majorly used to build a breathtaking user interface. Vue.js is efficient to create advanced web page applications.

Vue.js gets its strength from the flexible JavaScript library to build an enthralling user interface. As the core of Vue.js is concentrated which provides a variety of interactive components for the web and gives real-time implementation. It gives freedom to developers by giving fluidity and eases the integration process with existing projects and other libraries that enables to structure of a highly customizable application.

Vue.js is a scalable framework with a robust in-build stack that can extend itself to operate apps of any proportion. Moreover, vue.js is the best framework to seamlessly create astonishing single-page applications.

Our Vue.js developers have gained tremendous expertise by delivering services to clients worldwide over multiple industries in the area of front-end development. Our adept developers are experts in Vue development and can provide the best value-added user interfaces and web apps.

We assure our clients to have a prime user interface that reaches end-users and target the audience with the exceptional user experience across a variety of devices and platforms. Our expert team of developers serves your business to move ahead on the path of success, where your enterprise can have an advantage over others.

Here are some key benefits that you can avail when you decide to hire vue.js developers in USA from HireFullStackDeveloperIndia:

  • A team of Vue.js developers of your choice
  • 100% guaranteed client satisfaction
  • Integrity and Transparency
  • Free no-obligation quote
  • Portal development solutions
  • Interactive Dashboards over a wide array of devices
  • Vue.js music and video streaming apps
  • Flexible engagement model
  • A free project manager with your team
  • 24*7 communication with your preferred means

If you are looking to hire React Native developers in USA, then choosing HireFullStackDeveloperIndia would be the best as we offer some of the best talents when it comes to Vue.js.