React · Chapter 9 of 26

Forms

By the end of this chapter you can build forms whose every field is controlled by state (text, select, checkbox, radio buttons and textarea), serve many fields with one handler, submit without a reload, work out validation messages and a disabled button from state, and explain React's warnings about read-only 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
  • 4 tasks
  • React
  • In the all labs plan

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

Why you need this

Every booking on Parda ends in a form. For Wada Chirebandi Returns (Sat 7 Nov, 8:00 pm, Natya Ghar, 15 seats left) you give a name, a phone, how many seats and a tick for the terms, and Datta's team wants a phone that really has 10 digits: a wrong number is a ticket nobody receives and a call for him on the night. There is a promo code box that Ira wants in capitals as you type, filters for finding another show, and a feedback form after the show.

Chapter 5 read a form once, on submit, with FormData. That cannot change what is typed while you type, cannot show "Enter a 10-digit phone number" the moment it becomes true, and cannot switch a button off until the form is right. For that, React has to know every field's value on every keystroke: the value must live in state.

What you build: 4 tasks

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

  1. Task 1

    BookingForm.jsx: the booking form

    BookingForm gets one prop, onBook, a function. It returns a <form> that holds, in this order: a <label> Name for a text <input>; a <label> Phone for an <input> with type tel; a <label> Seats for a <select> with the options 1, 2, 3, 4, 5 and 6, starting at 2; a checkbox whose label says I agree to the terms; and a button Book. Every field is controlled: its value (checked, for the checkbox) comes from state and its onChange sets the state. Each label must belong to its field (htmlFor and the field's id, or the field inside the label), because the Check finds every field by its label.

    A click on Book must not reload the page. If the form is valid, call onBook once with { name, phone, seats }: the name without spaces at the ends, the phone as typed, and seats as a number (3, not "3"). The form is valid when the name is not empty, the phone is exactly 10 digits, and the box is ticked. If it is not valid, call nothing and show a paragraph for each problem, in this order: Enter your name, Enter a 10-digit phone number, Accept the terms to book.

    Show no problem before the first click on Book. After it, work the problems out from state during render, so each one goes away as soon as its field is fixed. Give the <form> className="booking-form" and the problems' <div> className="errors" for app.css; the Check does not look at classes.

  2. Task 2

    PromoCode.jsx: 8 capitals as you type

    PromoCode gets one prop, onApply, a function. It returns a <form> that holds, in this order: a <label> Promo code for a text <input>, a button Apply, and a paragraph that says how many characters there are so far out of 8 (0 of 8, then 3 of 8 after typing par).

    The input is controlled and changes what is typed: letters become capitals as you type (typing diwali shows DIWALI), and it stops at 8 characters (typing diwali26x shows DIWALI26). The value itself must be in capitals, not only look like it. Apply is disabled until there are exactly 8 characters. A click on Apply calls onApply with the code (DIWALI26), and must not reload the page. Give the <form> className="promo" for app.css.

  3. Task 3

    Filters.jsx: filters that report every change

    Filters gets one prop, onChange, a function. Keep the filters in state, an object with a language and kinds. Show a <label> Language for a <select> with the options All, Hindi, Marathi and English, starting at All, and four checkboxes labelled Film, Play, Comedy and Music, all ticked at first (KINDS in the starter has their ids and labels).

    On every change, call onChange once with the new filters: { language, kinds }, where kinds holds the ids of the ticked kinds, always in the order film, play, comedy, music. Choosing Marathi calls onChange({ language: 'Marathi', kinds: ['film', 'play', 'comedy', 'music'] }); then unticking Comedy calls onChange({ language: 'Marathi', kinds: ['film', 'play', 'music'] }). Never call onChange while rendering. Give the outer <div> className="filters" and the checkboxes' <div> className="kinds" for app.css.

  4. Task 4 · interview question

    Interview: Feedback.jsx, the form that warns

    Feedback gets one prop, onSend, a function. It shows three radio buttons under How was the show? (Loved it, It was fine, Not for me), an Email box, a Comments box and a button Send, and one handler, handleChange, serves every field by its name. Type a letter in Email and React warns: A component is changing an uncontrolled input to be controlled. Fix it so that no warning appears, keeping the fields and their labels.

    The Check types asha@example.com in Email and Lovely evening in Comments, picks Loved it, and clicks Send: onSend must get { rating: 'loved', email: 'asha@example.com', comments: 'Lovely evening' }, with no warning at any step.

    Rukhsana will ask: React warns "A component is changing an uncontrolled input to be controlled". What does it mean, and how do you fix it?

What you will learn

  • Controlled inputs: value from state, onChange sets the state, so state is the one truth.
  • The other fields: select, checkbox and radio buttons (checked), and textarea.
  • One handler for many fields, with each field's name.
  • Submitting: onSubmit on the form and e.preventDefault().
  • Validation from state: messages and a disabled button worked out during render.
  • Labels, which people and the Check use to find a field.
  • React's warnings about a value without onChange, and an input that switches from uncontrolled to controlled.

More in State

Build "Forms" yourself

Parda sells tickets for Pune's own cinemas and stages, and its booking page broke on opening night. You rebuild it in React. 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 are free. This one comes with the all labs plan.