Skip to content
Learn Netverks

Lesson

Step 14/36 39% through track

controlled-inputs

Controlled inputs

Last reviewed May 28, 2026 Content v20260528
Track mode
client_react
Means
In-browser React TSX
Reading
~2 min
Level
intermediate

This lesson

This lesson teaches Controlled inputs: the concepts, APIs, and habits you need before advancing in React.

Forms are where UX and validation meet—controlled inputs are the React default for predictable state.

You will apply Controlled inputs in contexts like: Login flows, settings panels, checkout steps, and admin CRUD screens.

Write TypeScript/TSX, click Run in browser—React 18 loads from CDN, JSX compiles in the tab, UI renders in the preview root, and printOutput feeds the terminal.

When you can explain the previous lesson's ideas without copying starter code.

A controlled input’s value comes from React state. Every keystroke fires onChange, updates state, and re-renders with the new value. React is the source of truth—not the DOM.

Text input pattern

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

Checkboxes and selects

  • Checkbox: checked={agreed} + onChange reading e.target.checked
  • Select: value={role} on <select> with matching <option value>

Important interview questions and answers

  1. Q: Controlled vs uncontrolled?
    A: Controlled ties value to state; uncontrolled reads from refs/DOM—controlled is default in React apps for forms you validate.
  2. Q: Why might input feel laggy?
    A: Expensive re-renders on every keystroke—optimize child components or debounce validation, not the controlled pattern itself.

Self-check

  1. What breaks if you forget value but keep onChange?
  2. How do you read checkbox state in an event handler?

Challenge

Live echo

  1. Type in the input and confirm the paragraph mirrors text.
  2. Add a max length of 20 characters via maxLength.

Done when: the echo updates on every keystroke and stops at 20 characters.

Challenge

Single source of truth

  1. Log the state value on every keystroke with printOutput.
  2. Confirm the input never stores its own shadow copy.

Done when: terminal reflects the same string shown in the input.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs in your browser in a sandboxed frame. Backend runners appear when this track’s profile allows them.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Controlled vs uncontrolled?
  • Single source of truth?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump