Member-only story

Building Forms in React: Controlled vs. Uncontrolled Components

Ruchir Gupta
3 min readJan 2, 2025

Forms are a crucial part of web applications, enabling users to input data. In React, managing form elements can be achieved through controlled or uncontrolled components, each with distinct characteristics and use cases.

What are Controlled Components?

A controlled component is a form element where the component’s state controls its value. The React component manages the form data via state, ensuring a single source of truth.

Key Features:

  1. React State Control: The input’s value is tied to the component’s state.
  2. Real-time Updates: Changes in the input immediately update the state, and vice versa.
  3. Full Control: Allows validation and transformations on the input value as the user types.

How Controlled Components Work:

  • Use value to bind the input field to a state variable.
  • Use onChange to update the state whenever the input value changes.

Example: A Controlled Input

import React, { useState } from 'react';

function ControlledForm() {
const [inputValue, setInputValue] = useState("");
const handleChange = (event) => {…

--

--

Ruchir Gupta
Ruchir Gupta

Written by Ruchir Gupta

Software Developer at Accenture India, Ex-Software Engineer at Roblox Corporation.

No responses yet