Open In App

React.js | Uncontrolled Vs Controlled Inputs

Last Updated : 29 Jan, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In HTML, form elements such as <input>, <textarea>, <select>, etc typically maintain their own state and update it based on user input. In React, a mutable (changeable) state is usually kept in the state property of components. 
In React forms input value can be of two types according to your choice: uncontrolled and controlled values.
Uncontrolled input: With uncontrolled input values, there is no updating or changing of any states. It maintains its own internal state, which basically means it remembers what you typed in the field. That value can be exploited by pulling it using the ref keyword whenever it needs to be used. In uncontrolled inputs, the value you submit is the value you get.
 

Now, Open your react project and edit the index.js file in the src folder:

src index.js:

Javascript




import React from 'react';
import ReactDOM from 'react-dom';
import NameForm from './NameForm';
 
ReactDOM.render(
  <React.StrictMode>
    <NameForm />
  </React.StrictMode>,
  document.getElementById('root')
);


Create a Component name NameForm.js in react project:

Now, Open your react project and edit the NameForm.js file in the src folder:

src NameForm.js:
 

javascript




import React,{Component} from 'react';
 
class NameForm extends  React.Component {
    handleClick = () => {
      const name = this._name.value;
      alert('Hello ', name);
    }
   
    render() {
      return (
        <div>
          <input type="text" ref=
                {inputValue => this._name = inputValue}
             
            placeholder="Enter your name" />
            <button onClick={this.handleClick}>
                Done
            </button>
        </div>
     );
    }
  }
 
export default NameForm;


Input: Tanisha 
Output: Hello Tanisha (Display on the alert box) 
 

Controlled input: In controlled inputs, there is at all times some sort of changes and amendments going on in the field, every single character entered and even something like a backspace would count as a change. The current value of the input field will be a prop of the class component (usually it will live in the state referred to by using this.state.varName.) as they don’t maintain their internal state. There will also be a callback function (like onChange, onClick, etc) that is necessary to handle the changes (in value) occurring in the input field, which makes them controllable. 
Now, Open your react project and edit the NameForm.js file in the src folder:
src NameForm.js:

javascript




import React,{Component} from 'react';
 
 
class NameForm extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            name: '',
        };
    }
    
    handleNameChange = (event) => {
        this.setState({ name: event.target.value });
    };
    
    render() {
        return (
            <div>
                <input type="text" onChange=
                        {this.handleNameChange}
                        placeholder="Enter your name" />
                <h1>{this.state.name}</h1>
            </div>
        );
    }
}
 
export default NameForm;


Input: Tanisha 
Output: Tanisha (Displaying on the screen letter by letter as the user types) 
 

Which one is better? 
It’s evident that controlled and uncontrolled form fields each have their individual benefits. You might need to use both or either depending on what the situation calls for. If you are making a simple form with little to no UI feedback and no need for instant validation (which means you only would validate and/or need the values when you submit), using uncontrolled inputs with refs is what you should go for. It would keep the source of truth in the DOM making it quicker and requiring lesser code. If you need more UI feedback and take into account every little change in the particular input field go for controlled input.
 



Similar Reads

Controlled vs Uncontrolled Components in ReactJS
In React, Controlled components refer to the components where the state and behaviors are controlled by Parent components while Uncontrolled components are the ones having control of their own state and manage the behaviors on themselves. Table of ContentUncontrolled ComponentsControlled ComponentsDifference between Controlled and Uncontrolled Comp
3 min read
When to use an uncontrolled component in React?
Uncontrolled Components are the components that are not controlled by the React state or any functions and are handled by the DOM (Document Object Model). So to access any value that has been entered we take the help of refs. In an uncontrolled component, the component manages its state internally. Common use cases for uncontrolled components inclu
3 min read
React Suite Checkbox Group (Controlled)
React suite is a library of React components that have a sensible UI design and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React Suite Checkbox Group Controlled. A Checkbox Group Controlled den
3 min read
React Suite CheckPicker Controlled
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite Checkpicker controlled. Checkpicker is very useful for selecting mul
3 min read
React Suite DateRangePicker Controlled
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. It is a set of react component libraries for enterprise system products. It is a well-thought-out and developer-friendly UI framework. DateRangePicker component allows the user to quickly select a date range. Da
3 min read
React Suite Autocomplete controlled
React Suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we’ll learn about React Suite Autocomplete controlled. The AutoComplete component auto-completes t
3 min read
React Suite Cascader Controlled
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we’ll learn about React suite Cascader Controlled. A cascader is used for a single selection of da
6 min read
React Suite DatePicker Usage Controlled
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we’ll learn about React suite DatePicker Usage Controlled. The DatePicker Component is used to cho
2 min read
React.js Blueprint Popover2 Controlled mode
Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss React.js Blueprint Popover2 Controlled mode. Popovers help in displaying the floating content next to the target element. In the controlled mod
6 min read
React.js Blueprint Select2 Controlled usage
Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss React.js Blueprint Select2 Controlled usage. Select2 component displays a list of items to choose one item and item children are wrapped in a M
5 min read