Member-only story

Understanding the Difference Between Controlled and Uncontrolled Components in React

CodeByUmar
4 min readDec 10, 2024

Introduction

In React, form elements such as input fields, checkboxes, and selects can be handled in two distinct ways: controlled or uncontrolled. Choosing between these two approaches depends on the level of control you need over the form data and how you want to interact with it. In this post, we’ll explore the key differences between controlled and uncontrolled components, provide code examples, and discuss when to use each.

1. What Are Controlled Components?

A controlled component is a component whose form elements are controlled by React. This means the value of the form element is bound to the component’s state. Any changes to the form element’s value will trigger a state update, making React the “single source of truth” for that value.

Key Features of Controlled Components:

  • The form element’s value is tied to the component’s state.
  • The state is the source of truth, and any change in the form field is reflected in the state.
  • React updates the form value based on state changes.
  • You typically handle changes using onChange event handlers.

--

--

CodeByUmar
CodeByUmar

Written by CodeByUmar

Full Stack Developer sharing insights on JavaScript, React, and web development. Passionate coder and problem solver exploring new tech. 🚀

No responses yet