React Forms: Controlled and Uncontrolled Components.
Tags: webdev, react, javascript, frontend
Published: 2024-01-16 10:36:41

React provides developers with two main approaches to handling form data: controlled and uncontrolled forms. In most cases, the recommended method by the React teams for forms are the controlled method, although the uncontrolled method is still valid, it gives less flexibility compared to the controlled form.

## Controlled Forms
A controlled form in React is a form where the form elements (like input fields) are controlled by the state of the React component. In other words, the values of the form elements are stored in the component's state, and any changes to the form elements are handled by updating the state.

React encourages the use of controlled components for forms, where the component state manages the values of form elements. The provided code snippet illustrates the concept of a controlled form using React hooks.

![React Controlled Form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xhj9ito4okzdmjkwj995.png)
In this example, the useState hook is employed to initialize a piece of state named value with an initial value of an empty string. The setValue function is later used to update this state.

Input Element Binding
The input element is bound to the value state, ensuring that its value is always controlled by the React component's state. The onChange event is connected to the handleChange function, which updates the state with the latest value as the user types.

### Benefits of Controlled Forms
1. Predictable State: The state of the form elements is predictable and can be easily managed by React.

2. Single Source of Truth: The component state serves as a single source of truth for the form data, making it easier to track and manage.

3. Dynamic Updates: Reacting to user input is seamless, allowing for dynamic updates based on the component state.

### Uncontrolled Forms

![React Uncontrolled Form](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wrxvl63xjav5i9u83b4u.png)
In this example, an uncontrolled form is created using a combination of useRef and traditional HTML form elements. The inputRef is utilized to reference the DOM input element without managing its state through React.

### Uncontrolled Form Characteristics
1. No React State Management: Unlike controlled forms where form elements are connected to React state, uncontrolled forms do not rely on state for managing form data.

2. Direct DOM Interaction: The ref is used to directly access the DOM element, allowing developers to interact with the form elements without involving React state.

3. No Event Listeners for Value Changes: Uncontrolled forms do not attach onChange event listeners to track input changes. Instead, the value is directly accessed when needed, as shown in the handleSubmit function.

### Benefits of Uncontrolled Forms
1. Simplicity: Uncontrolled forms are simpler and might be preferred for scenarios where React's state management is not necessary.
2. Ref-Based Interaction: Directly interacting with the DOM through ref provides a straightforward way to access form data without the need for state updates.
Compatibility with Non-React Code:

Uncontrolled forms can be useful when integrating React into projects with existing non-React code, as they align more closely with traditional HTML forms.

### Considerations
1. Limited React Features: Uncontrolled forms sacrifice some of React's powerful features, such as easy state management and dynamic updates.
2. Validation and Testing: Implementing form validation and testing might be more challenging in uncontrolled forms compared to controlled ones.


##Conclusion
Controlled forms provide a structured way to handle form data in React applications. By managing the form elements through component state, developers gain more control over the form's behavior and can easily integrate it with other React features.