# Controlled vs Uncontrolled

> Who owns the state? Learn the difference between components that manage themselves versus components managed by their parents.

Subject: React | Lesson 19 of 48 | 5 min read
Interactive lesson: https://codewithanimation.in/react/controlled-vs-uncontrolled
Previous: [Refs & useRef](https://codewithanimation.in/react/refs-and-useref.md) | Next: [Lifting State Up](https://codewithanimation.in/react/lifting-state-up.md)

## What do the terms mean?

An **uncontrolled** component keeps its own local state. It decides what to do when interacted with. You just render it and let it do its thing.

A **controlled** component has no local state. Its parent passes down the current state via a prop (like `value` or `isOpen`), and a callback function (like `onChange` or `onToggle`). The parent is in complete control.

## Why does it matter?

Uncontrolled components are very easy to use (less boilerplate). If you just need a standard Accordion that opens and closes when clicked, uncontrolled is perfect.

But what if you want a button *outside* the Accordion to open it? Or what if you want to ensure only one Accordion panel is open at a time? You can't do that if the Accordion manages its own state in secret. You have to 'control' it from the parent.

## Native HTML Forms

The most common place you see this is with HTML `<input>` tags.

**Uncontrolled Input:** `<input type='text' />` (The browser's DOM manages the text).

**Controlled Input:** `<input type='text' value={text} onChange={e => setText(e.target.value)} />` (React manages the text). If you omit the `onChange`, the input becomes completely read-only because React forces the value to stay the same!

## Common mistake: The 'Single Source of Truth' rule

A component should ideally be entirely controlled or entirely uncontrolled.

If you try to mix them (e.g., passing `initialValue` as a prop but then copying it into local state), you create two sources of truth. If the parent's `initialValue` updates later, the child won't react because it's looking at its local state. This causes terrible sync bugs.

## Tip: The Naming Convention

Controlled components usually take a value prop and an 'on-change' callback prop. For forms, it's `value` and `onChange`. For UI elements like modals, it's usually `isOpen` and `onClose` or `onToggle`.

## Try it: The Accordion Test

Interact with both accordions. The Uncontrolled accordion handles its own clicks independently. The Controlled accordion is wired to the external buttons at the top.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/react/controlled-vs-uncontrolled

## Check yourself

### Question 1

What makes a React component 'controlled'?

- A. It uses Redux or Context for state management.
- B. It relies on props from its parent to dictate its behavior and state, rather than using its own local `useState`.
- C. It forces the user to fill out a form before proceeding.

Correct answer: B

A controlled component delegates its state management to the component that renders it.

- Why not A: Those are global state managers, unrelated to the controlled/uncontrolled component pattern.
- Why not C: This has nothing to do with component architecture.

### Question 2

You write `<input value={searchQuery} />` but forget to add an `onChange` handler. What happens when the user types?

- A. React throws a fatal error and the app crashes.
- B. The input works normally, but `searchQuery` doesn't update.
- C. The user cannot type anything. The input appears frozen/read-only.

Correct answer: C

You told the input its value *must* be `searchQuery`. Without an `onChange` handler to update `searchQuery`, the value is permanently frozen.

- Why not A: React will log a warning to the console, but it won't crash.
- Why not B: Because it's a controlled input, the DOM is forced to match the React state.

### Question 3

When should you choose to build a controlled component over an uncontrolled one?

- A. When you need other components or the parent to know about or change its state.
- B. Always. Uncontrolled components are considered bad practice.
- C. When you want better rendering performance.

Correct answer: A

If a Modal needs to be closed by an external 'Cancel' button, or a Form Submit button, the parent needs access to the Modal's state, requiring it to be controlled.

- Why not B: Uncontrolled components are great for simple UI elements that don't need to coordinate with anything else.
- Why not C: Controlled components usually trigger slightly more renders because state lives higher up.

## Remember this

- Uncontrolled = manages its own local state.
- Controlled = parent passes state down via props.
- Native HTML inputs can be controlled or uncontrolled.
- If a component needs to coordinate with other components, it must be controlled.
