×

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 Animation

React Animation The animation is a procedure in which an image is manipulated to appear as a moving image. It is widely used to create an interactive web application. In React, we have to...

4 minutes read.

Difference between Controlled and Uncontrolled component in ReactJs

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...

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 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 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.

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.

React Code Splitting

React Code Splitting React Code Splitting is the procedure of splitting the bundle files by which the files get easily loaded on the webpage. The react application bundles the files by using...

4 minutes read.

React Native vs ReactJS

Difference between React Native and ReactJS React Native React Native is also an open-source JavaScript framework that is used for the development of a mobile application for iOS Android and Windows. It...

6 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.

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.

Difference Between State and Props in React

Comparison between State and Props State The state is an updatable structure which is used for containing data or information about the component. The state in a component can be changed over...

2 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.

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.

React Higher-Order Components

React Higher-Order Components In short form, Higher-Order Components are represented as HOC. The Higher-order component is an advanced technique to use component logic. HOC is a function that takes the component and returns the new...

2 minutes read.

React Keys

React Keys A key can be defined as a unique identifier. In React, it is used for identifying the items that have changed, deleted, and updated from the lists. React Keys are helpful...

3 minutes read.

React JSX

React JSX As we know, all of the React components include a render function. The Render function specifies the HTML output of a React component. JSX is an acronym of JavaScript extension that...

3 minutes read.

React Props Validation

React Props Validation As we know, props are a suitable mechanism for passing the read-only attributes to react components. The props are required to be correctly used in the component. If it is...

5 minutes read.

React Refs

React Refs The word ‘Refs’ is an abbreviation of ‘References’ in React. It is as similar as Keys in React. Refs are a function in React which is used for accessing the DOM element and...

7 minutes read.

React Redux

How to Use Redux with React Hooks It is an open-source library of JavaScript which is used for managing the application state. React uses the Redux to build the user interface....

5 minutes read.

React CSS

What is React CSS React CSS is used to provide the style to the React applications. The style attribute adds dynamically-computed styles at render time, and it is one of the most used...

5 minutes read.