Close the earlier lessons and build this from the brief. It's bigger than the earlier checkpoints: two components, a form with four fields and a list. Allow about 35 minutes.
The brief
A sign-up page for a study group, with two parts:
SignUpForm, in its own component, with:
| Field | Starts as | Rule | Error |
|---|---|---|---|
| Name, text | empty | not blank | Enter your name. |
| Minutes a day, number | 30 | a whole number from 10 to 180 | Choose a whole number of minutes from 10 to 180. |
| Level, select | Beginner | Beginner or Intermediate | none |
| I agree to the group rules, checkbox | unticked | must be ticked | Tick the box to agree to the group rules. |
On Join, show every error that applies, each under its own field. If there are none, add the member and reset the name and the checkbox.
A member list in App, headed Members (2) with the current count, with
lines such as Ada: 45 min a day, beginner. With no members, show No one
has joined yet.
Acceptance checks:
- Pressing Join on the empty form shows the name and agreement errors at the same time, and no minutes error.
- Two valid sign-ups give Members (2) and two lines.
- No errors or warnings in the console.
npm install and npm run dev. Hint 1: where does each piece of state live?
The members are needed by the list, so they live in App. What's typed is only
needed by the form until it's submitted, so it lives in SignUpForm. The form
reports a new member through a callback prop.
Hint 2: several errors at once
This is the collecting pattern from Showing every error at once in Forms
and Validation, not the first-failure return. Keep the errors in one object in
state, such as { name: "...", agreed: "..." }. Build a new object in
handleSubmit, adding a key for each rule that fails, then store it. If it has
no keys, the form is valid.
Show one solution
npm install and npm run dev. Check yourself
- What two props make an input controlled, and what happens with only one?
- Why does
handleSubmitcallevent.preventDefault()? - Two sibling components need the same data. Where does the state go, and how do the siblings change it?
Completion is saved in this browser only. It does not measure mastery.