الانتقال إلى المحتوى
FrontendReact· 6 دقائق للقراءة

Controlled vs Uncontrolled Inputs: Pick One Source of Truth per Field

value says what a field is now, defaultValue only what it starts as. Choose one owner per field.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
  • React 19
محتويات المقال

The "Edit profile" page ships with two bugs. The name field is empty even though the profile clearly loaded, since the page title shows the user's name. The bio box is empty too, and the first letter typed into it prints a console error: "A component is changing an uncontrolled input to be controlled." Both fields were written as if value and defaultValue were interchangeable ways to fill an input, and both read their starting values on the first render, before the profile request had finished.

The common misconception

The belief has two parts. First, every input in React should be controlled: put the text in useState, pass it as value, update it in onChange. Second, value and defaultValue both just put text in the box, so you can switch between them when one of them complains.

Smart developers hold it because controlled inputs are what most tutorials teach first, and the pattern is genuinely useful. It also looks true on screen: on the first render, <input value="Sara" /> and <input defaultValue="Sara" /> display exactly the same thing. The difference only shows up later, when the user types or the data changes, which is where the bugs live.

What actually happens

Every field has exactly one owner

Every input already has a current value: the browser keeps it in the DOM. With an , it stays that way. defaultValue (or defaultChecked for checkboxes and radios) tells React what to put in the field at the start, and after that React leaves it alone. You read the value when you need it, usually on submit, by giving the input a name and reading the form data.

A moves ownership into React. When you pass value (or checked), React forces the field to show that value on every render. That's why a controlled input needs an onChange that updates the state synchronously with e.target.value: if the state doesn't change, React puts the old text back after every keystroke. React's docs describe the choice in general terms: uncontrolled components need less setup, while controlled ones are the most flexible but must be fully configured by whoever owns the state.

The rules that follow from it

Once you see two different owners, the rules stop looking arbitrary:

  • defaultValue is read on the first render. If the data arrives later, the field ignores it, just like useState ignores its initial value after the first render.
  • A controlled value must never be undefined or null. React treats a missing value as uncontrolled, and an input can't switch between controlled and uncontrolled during its lifetime. Start with "" or pass value={bio ?? ""}.
  • value without onChange makes a read-only field. React logs an error that suggests defaultValue, onChange, or readOnly if read-only is what you meant.
  • Checkboxes use checked and defaultChecked, and a checked value must always be a boolean.

That's the profile page: the name used defaultValue while the profile was still null, and the bio held useState(profile?.bio), which started as undefined and became a string on the first keystroke.

TSX ProfileForm.tsx
import { useState } from "react";export function ProfileForm({ profile }: { profile: Profile | null }) {  // undefined while loading: the textarea starts uncontrolled  const [bio, setBio] = useState(profile?.bio);  return (    <form>      {/* Read once, on a render where profile is still null */}      <input name="name" defaultValue={profile?.name} />      <textarea value={bio} onChange={(e) => setBio(e.target.value)} />      <p>{bio?.length ?? 0}/160</p>    </form>  );}

The parent now renders the form only after the profile has loaded, so every starting value is real, and the key gives the form fresh fields whenever a different profile is opened. Each field has one owner. The name is uncontrolled, because nothing on screen depends on it until the user saves. The bio is controlled, because the counter needs its length on every keystroke, and on submit the code reads it from state rather than from the form data.

Uncontrolled (defaultValue)Controlled (value + onChange)
Who holds the current textThe browser's DOMYour React state
When you can read itOn submit, via name and form data, or with a refDuring every render
Changing it from codeReset the form, or remount with a new keySet the state
Re-renders while typingNoneOne per keystroke, from the state owner down
A good fit forFields you only need on submitLive validation, counters, previews, dependent fields

When each one fits

Make a field controlled when the UI needs its value while the user is typing: a character counter, a submit button that enables itself, validation on every keystroke, a live preview, or a second field that depends on the first. Keep it uncontrolled when its value only matters at submit time. React 19 makes that case pleasant: pass a function to <form action> and it receives the form's data, and after the action succeeds React resets every uncontrolled field in the form.

How to apply it in your project

  1. Ask what each field needs while typingIf nothing on screen reads a field until submit, make it uncontrolled with a name and a defaultValue. Control it only when the UI reacts to it as the user types.
  2. Never pass undefined as a valueInitialize controlled state with "", or write value={x ?? ""} for data from an API, and keep checked a boolean. This removes the uncontrolled-to-controlled error at its source.
  3. Render forms after their dataShow a loading state until the record arrives, then render the form with key={record.id}, so starting values are real and switching records gives fresh fields.
  4. Treat both console errors as bugs"Changing an uncontrolled input to be controlled" and "a value prop without an onChange handler" each point to a field with two owners or none. Fix the field instead of silencing the message.

Key takeaways

  • value says what a field is now and makes React state its owner; defaultValue only says how it starts.
  • A controlled input needs a string value from the first render and an onChange that updates it synchronously.
  • Uncontrolled fields are simpler when you only need the value on submit, and React 19 form actions hand it to you.
  • Give every field one owner, and render forms after their data loads, with a key to reset them.

📚 المصادر

  1. <input> — React DOM Components
  2. Sharing State Between Components — React
  3. <form> — React DOM Components

التعليقات (0)