MediumConcept

What is the difference between controlled and uncontrolled components in React?

Technology:
React
Experience:
Junior,
Mid-level

Quick answer

A controlled component keeps the input's value in React state and updates it through onChange, while an uncontrolled component lets the DOM hold the value and you read it with a ref when needed.

Detailed explanation

Controlled inputs make React the single source of truth: the value comes from state and every keystroke calls a handler that updates state. This makes validation, formatting and conditional disabling easy. The cost is a re-render on each keystroke.

Uncontrolled inputs use defaultValue and ref, so the browser manages the value and you read it on submit. They are simpler for basic forms and file inputs (which are always uncontrolled). Form libraries often use uncontrolled inputs under the hood to avoid re-rendering the whole form while typing.

Example

jsx
// controlled
const [email, setEmail] = useState("");
<input value={email} onChange={e => setEmail(e.target.value)} />

// uncontrolled
const ref = useRef(null);
<input defaultValue="" ref={ref} />
const onSubmit = () => console.log(ref.current.value);
  • ReactEasy

    What is the difference between props and state in React?

    Props are read-only inputs passed from a parent component, while state is data a component owns and can change over time, which triggers a re-render when updated.

    Intern 路 Junior 路 State Management
  • ReactMedium

    What is prop drilling and how do you avoid it in React?

    Prop drilling is passing props through several layers of components that do not use them just to reach a deeply nested child; you avoid it with composition, the Context API or a state management library.

    Junior 路 Mid-level 路 State Management
  • ReactMedium

    What is the difference between useRef and useState in React?

    Updating state triggers a re-render, while changing a ref's .current value does not; use state for data shown on screen and refs for DOM access or values that must persist without causing renders.

    Mid-level 路 Senior 路 React Hooks
  • ReactEasy

    What is the virtual DOM in React and how does it work?

    The virtual DOM is a lightweight JavaScript copy of the UI that React compares with the previous version after each update, so it only changes the parts of the real DOM that actually differ.

    Intern 路 Junior 路 Performance
  • ReactEasy

    What are React hooks and what are the rules of hooks?

    Hooks are functions such as useState and useEffect that let function components use state, lifecycle behaviour and other React features; they must be called at the top level of a component or custom hook, never inside loops, conditions or nested functions.

    Junior 路 Mid-level 路 React Hooks
  • ReactEasy

    Why do we need keys in React lists?

    Keys give each list item a stable identity so React can match items between renders and correctly add, remove or reorder them without recreating or mixing up their state.

    Junior 路 Mid-level 路 Performance