React traps · Chapter 6 of 16

Controlled and uncontrolled inputs

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: State and rendering
  • 3 tasks
  • React
  • In the all labs plan

The first 2 chapters of React traps are free. This one comes with the all labs plan.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.jsx: leave a phone number behind on purpose

    Write reproduce() in repro.jsx. Render KioskForm with any onSend function, type a phone number into the box labelled Phone, press the Clear button, and resolve to the Phone box's value: what it shows now.

    The check runs your reproduce() twice: beside the original KioskForm.jsx it must resolve to the number you typed (any text that is not empty), and beside a fixed KioskForm.jsx it must resolve to '' (an empty box). So it must really type, press Clear and read the box. React warns while it runs; the check does not mind. reproduce() must finish within 5 seconds.

  2. Task 2

    KioskForm.jsx: Clear empties the boxes, and React stays quiet

    Fix KioskForm({ onSend }) in KioskForm.jsx. Both boxes start empty. Typing shows what was typed, Clear empties both boxes, and React prints no warning at any point. Send tickets calls onSend({ name, phone }) with what the boxes show, and an empty box sends ''.

    Keep the labels (Name, Phone), the buttons (Send tickets, Clear) and the inputs' name attributes as they are: Send tickets reads the form, as it does now. The check catches React's warnings while you type and while Clear is pressed, types and clears twice, and sends after Clear; a fix that switches the boxes to defaultValue is quiet, and Clear then clears nothing at all.

  3. Task 3

    KioskForm.test.jsx: a test that would have caught it

    Write a vitest test file, KioskForm.test.jsx, that would have caught this bug before it shipped. The check runs your file three times: beside the original KioskForm.jsx, where at least one test must fail; beside the fixed KioskForm.jsx, where every test must pass; and beside a wrong fix that uses defaultValue instead of value, where at least one test must fail.

    So test what the next customer sees: type a phone number, press Clear, and expect the Phone box to be empty. A test of what Send tickets sends after Clear is welcome too. No real waiting: each test must finish within 5 seconds.

What you will learn

  • repro.jsx: leave a phone number behind on purpose
  • KioskForm.jsx: Clear empties the boxes, and React stays quiet
  • KioskForm.test.jsx: a test that would have caught it

More in State and rendering

Build "Controlled and uncontrolled inputs" yourself

Parda's web app runs on React, and Rukhsana, its senior frontend engineer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of React traps are free. This one comes with the all labs plan.