React / Next.js

Forms & Controlled Components

Controlled components, uncontrolled components, form validation, React Hook Form, submit handling

20 interview questionsยท
Mid-Level
1

What is a controlled component in React?

Answer

A controlled component is a form element whose value is managed by React state via the value prop and an onChange handler. This means React becomes the single source of truth for this data. Unlike uncontrolled components where the DOM maintains its own value, controlled components provide full control over form data and facilitate real-time validation.

2

How to create a controlled input in React?

Answer

A controlled input requires two essential elements: the value prop bound to React state and an onChange handler to update that state. Without onChange, the input would be read-only as React would prevent any modification. This approach ensures React state stays synchronized with the displayed input value, enabling immediate validation or data transformation.

3

What is the difference between a controlled and an uncontrolled component?

Answer

The fundamental difference lies in the source of truth for the data. A controlled component stores its value in React state (single source of truth), while an uncontrolled component lets the DOM manage its own value, accessible via a ref. Controlled components offer more control and facilitate real-time validation, but uncontrolled components can be simpler for basic forms without complex validation.

4

How to manage multiple controlled inputs in the same form?

5

How to create a controlled checkbox in React?

+17 interview questions

Master React / Next.js for your next interview

Access all questions, flashcards, technical tests, code review exercises and interview simulators.

Start for free