×

React Forms

React Forms

Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the users. Forms can perform various tasks that depend on the nature of the requirement of your business and logic, such as user authentication, adding user, searching, filtering, booking, etc. A form can include text fields, buttons, check boxes, radio buttons, etc.

Creating forms

React provides you a Stateful and reactive approach to create a form. In React, the controlled components usually implemented the forms.

The form has mainly two types that are as follows:

  1. Uncontrolled component.
  2. Controlled component.

Uncontrolled component

Uncontrolled components are similar to the traditional HTML form inputs. The DOM is itself responsible for handling the form data. Here, the elements of HTML maintain their state, which will update when the value of input changes. For writing an uncontrolled component, you have to use a ref to get form values from the DOM. In other words, it is not important to write an event handler for updating of every state.

Example:

import React, { Component } from 'react'; 
 class App extends React.Component { 
  constructor(props) { 
  super(props); 
  this.updateSubmit = this.updateSubmit.bind(this); 
  this.input = React.createRef(); 
  } 
  updateSubmit(event) {  
  alert('Data submitted successfully.'); 
  event.preventDefault(); 
  } 
  render() { 
  return ( 
  

Uncontrolled Form Example



); } } export default App;

Output:

Uncontrolled component

Once the submit button gets clicked, you will get the following output:

submit button gets clicked

Controlled component

In the controlled component, the input of the form element is handled by the component rather than the DOM. Controlled components have the functions that govern the data, which passes to them on every onChange event. Here, the changeable (mutable) state is kept inside the state property and updated only with the setState() method.

Like an onChange event, the controlled component also takes its current value by props and notifies the changes by the callbacks.

Controlled components consist of the functions that govern the data, which passes into them on every onChange event. After that, this data will save into state and updated with the setState() method. It makes better control of the component over the elements and data of the forms.

Example:

import React, { Component } from 'react'; 
 class App extends React.Component { 
  constructor(props) { 
  super(props); 
  this.state = {value: ''}; 
  this.handleChange = this.handleChange.bind(this); 
  this.handleSubmit = this.handleSubmit.bind(this); 
  } 
  handleChange(event) {  
  

Controlled Form Example

); } } export default App;

Output:

Controlled form example

Once the submit button gets clicked, you will get the following output:

Once the submit button

Multiple Input Fields

You can handle more than one input field value by adding a name attribute to each element.

If you want to access the fields in the event handler, use the syntax event.target.name and event.target.value.

Example:

import React, { Component } from 'react'; 
 class App extends React.Component { 
  constructor(props) { 
  super(props); 
  this.state = { 
  quali1: true, 
  quali2: true, 
  quali3: true, 
  quali4: false, 
  name: 'Madhav' , 
  age:20
  }; 
  this.handleInputChange = this.handleInputChange.bind(this); 
  } 
  handleInputChange(event) { 
  const target = event.target; 
  const value = target.type === 'checkbox' ? target.checked : target.value;  
  const name = target.name; 
  const age = target.age; 
  this.setState({ 
  [name]: value,
  [age]: value
  }); 
  } 
  render() {  
  return ( 
  

Multiple Input Controlled Form Example





); } } export default App;

Output:

Multiple Input Fields

Conditional Rendering

If you do not wish to display the element before the input provided by the user, you can add if statement.

In the following example, we are creating an empty variable that is stated as the top. We are also adding if statement for inserting the data to the top variable if the user provides any input.

Example:

import React, { Component } from 'react'; 
 class App extends React.Component {
  constructor(props) {
  super(props);
  this.state = { name: '' };
  }
  myChangeHandler = (event) => {
  this.setState({name: event.target.value}); 
  }
  render() {
  let top = '';
  if (this.state.name) {
  top = 

Hello Mr. {this.state.name}

; } else { top = ''; } return (
{top}

Your name:

); } } export default App;

Output:

Before any input given by the user, you will see the following output.

Conditional Rendering

When input provided by the user, you will see the following output.

Conditional Rendering

Related Topics

React Component API

React Component API It is a top-level API. It provides reusability to the code in the application and makes it completely individual. It has several methods for: Creating Elements.Transforming Elements.Fragments. Now, we are explaining the three...

2 minutes read.

Features of ReactJS

ReactJS Features As of now, within the developers, ReactJS is gaining much popularity as the best JavaScript framework. It is crucial for the front-end ecosystem. There are some of the crucial features of ReactJS that are...

2 minutes read.

ReactJS Versions

ReactJS Versions The Complete set of release history of ReactJS is elaborated as follows. The complete set of full documentation of recent releases is on GitHub. S.noVersionRelease DateExplanation1.0.3.029/05/2013Initially released for public.2.0.4.029/07/2013Supporting comments...

4 minutes read.

Difference Between React Flux and MVC

React Flux vs MVC MVC MVC is an acronym of 'Model View Controller.' It is the architectural pattern that is used to develop the user interface. It has three different logical components: The...

2 minutes read.

Comparison between React and Vue

Comparison between React and Vue React, and Vue both are the two most famous libraries of JavaScript that are used for creating thousands of websites today. The React and Vue both...

3 minutes read.

React Error Boundaries

React Error Boundaries In the past, the errors of JavaScript within the components were used to corrupt the internal state of react and cause it to emit the cryptic errors on the next...

3 minutes read.

React Props

React Props A prop is an abbreviation of ‘properties.' State and props are mainly different from each other because props are immutable. Props are the read-only components. It is the object that stores the attribute...

3 minutes read.

React Router

What is React Router? Routing is a process by which a user can direct to different pages based on their actions and requests. ReactJS router is generally used to develop single...

10 minutes read.

React Components

React Components Former, the developers used to write more than thousands of lines of code to develop a single-page application. These applications follow the structure of traditional DOM, and it was very challenging to make...

2 minutes read.

React Conditional Rendering

React Conditional Rendering In React, the working of the conditional rendering is similar to the condition works in JavaScript. We use JavaScript operators for creating elements that represent the current state, and then the React...

3 minutes read.

ReactJS vs AngularJS

Difference between ReactJS and AngularJS AngularJS AngularJS is the JavaScript framework that is open source, and it is also used to build a dynamic web application. It is developed in 2009 by...

5 minutes read.

How to use React Context

React Context React Context is used to pass the data through the component tree without passing the props down manually at the every level. In React Application, the data is passed in a top-down approach...

4 minutes read.

Pros and Cons of ReactJS

Pros and Cons of ReactJS There are various advantages, and disadvantages of ReactJS are as follows: Benefits of ReactJS 1. Easy to Learn and Use: ReactJS is very easy to use and learn. It has a good...

3 minutes read.

React Lists

React Lists Lists are used for displaying the data in an ordered format and mainly used to display the menus on websites. The creating of lists in React is as similar...

2 minutes read.

ReactJS Tutorial

ReactJS Introduction React Tutorial helps you to understand the basic and some advanced concepts of ReactJS. As of now, it is the essential front-end library that is developed by a...

3 minutes read.

React Table

React Table The table represents an arrangement that organizes the information in the form of rows and columns. The table is mainly used for storing and displaying the data within a structured format. Features of...

2 minutes read.

React Environment Setup Step by Step

React Environment Setup This section will provide you the information about how to set up an environment for the successful development of ReactJS application: Pre-Requisite for ReactJS NodeJS and NPMReact and React DOMWebpackBabel There are two...

5 minutes read.

React Forms

React Forms Forms are the important part of any web application. It allows the interaction of the user with the app as well as the gathering of the information from the...

3 minutes read.

React Map

React Map The map() is the standard function of JavaScript, which can be called at any array. The map() method is used for traversing and displaying a list of the similar objects of...

2 minutes read.

React Component Life-Cycle

React Component Life-Cycle In ReactJS, the creation process of every component includes several lifecycle methods. These methods, together stated as component's lifecycle. Four phases of the component's life cycle are as...

4 minutes read.