Logo

Developer learning path

React

Uncontrolled Components in React

Uncontrolled Components

82

#description

Uncontrolled components are a type of form element in React that do not rely on state to store or update their values. Instead, you rely on the actual value of the form control, as it is in the DOM, to obtain and manipulate its value.

In other words, with uncontrolled components, the data is stored not in the React state, but in the DOM itself. This approach can be used in cases when the data being entered is not important or when you just need a faster way to create forms.

For example, an input in an uncontrolled component would be written like this:

                    
<input type="text" ref={this.inputRef} />
                  

And a submit button would be written like this:

                    
<button onClick={this.handleSubmit}>Submit</button>
                  

In this example, the ref attribute is used to obtain the value of the input field and the onClick attribute is used to handle the submit event.

Uncontrolled components can be useful for simple forms or for forms that do not require validation or complex user interactions. However, if you need to validate data or ensure its accuracy and consistency, controlled components may be a better option.

Overall, uncontrolled components can be a faster and simpler way to manage form data without the need for additional state management, but they come with certain limitations in terms of data validation and management.

March 25, 2023

If you don't quite understand a paragraph in the lecture, just click on it and you can ask questions about it.

If you don't understand the whole question, click on the buttons below to get a new version of the explanation, practical examples, or to critique the question itself.