Skip to content

Checkpoint: Join the Study Group

The Course
•
35 min study

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:

FieldStarts asRuleError
Name, textemptynot blankEnter your name.
Minutes a day, number30a whole number from 10 to 180Choose a whole number of minutes from 10 to 180.
Level, selectBeginnerBeginner or Intermediatenone
I agree to the group rules, checkboxuntickedmust be tickedTick 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.
Your workspace React · 3 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); // Build the sign-up form and the member list here. // Keep the members in App, and put the form in its own component. export default function App() { return ( <main> <h1>Join the study group</h1> </main> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; } label { display: block; margin: 0.5rem 0; } input, select, button { font: inherit; } input[type="text"], input[type="number"], select { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } .error { color: #b42318; margin: 0.25rem 0; }
Runs with Vite on your computer: unzip, then 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
One solution React · 4 files
import { createRoot } from "react-dom/client"; import App from "./App.jsx"; import "./styles.css"; createRoot(document.getElementById("root")).render(<App />); import { useState } from "react"; import SignUpForm from "./SignUpForm.jsx"; export default function App() { const [members, setMembers] = useState([]); function addMember(member) { setMembers([...members, { ...member, id: Date.now() }]); } return ( <main> <h1>Join the study group</h1> <SignUpForm onJoin={addMember} /> <h2>Members ({members.length})</h2> {members.length === 0 ? ( <p className="muted">No one has joined yet.</p> ) : ( <ul> {members.map((m) => ( <li key={m.id}> {m.name}: {m.minutes} min a day, {m.level} </li> ))} </ul> )} </main> ); } import { useState } from "react"; export default function SignUpForm({ onJoin }) { const [name, setName] = useState(""); const [minutes, setMinutes] = useState("30"); const [level, setLevel] = useState("beginner"); const [agreed, setAgreed] = useState(false); const [errors, setErrors] = useState({}); function handleSubmit(event) { event.preventDefault(); const amount = Number(minutes); const found = {}; if (name.trim() === "") found.name = "Enter your name."; if (!Number.isInteger(amount) || amount < 10 || amount > 180) { found.minutes = "Choose a whole number of minutes from 10 to 180."; } if (!agreed) found.agreed = "Tick the box to agree to the group rules."; setErrors(found); if (Object.keys(found).length > 0) return; onJoin({ name: name.trim(), minutes: amount, level }); setName(""); setAgreed(false); } return ( <form onSubmit={handleSubmit}> <label> Name{" "} <input type="text" value={name} onChange={(e) => setName(e.target.value)} /> </label> {errors.name && <p className="error">{errors.name}</p>} <label> Minutes a day{" "} <input type="number" value={minutes} onChange={(e) => setMinutes(e.target.value)} /> </label> {errors.minutes && <p className="error">{errors.minutes}</p>} <label> Level{" "} <select value={level} onChange={(e) => setLevel(e.target.value)}> <option value="beginner">Beginner</option> <option value="intermediate">Intermediate</option> </select> </label> <label> <input type="checkbox" checked={agreed} onChange={(e) => setAgreed(e.target.checked)} />{" "} I agree to the group rules </label> {errors.agreed && <p className="error">{errors.agreed}</p>} <button type="submit">Join</button> </form> ); } body { font: 16px/1.5 system-ui, sans-serif; margin: 0; color: #1c2430; } main { max-width: 32rem; margin: 1.5rem auto; padding: 0 1rem; } h1 { margin: 0 0 0.75rem; } .card { border: 1px solid #d6dde6; border-radius: 8px; padding: 0.75rem 1rem; margin-bottom: 0.75rem; } .card h2 { margin: 0 0 0.25rem; font-size: 1.1rem; } .muted { color: #6b7785; } label { display: block; margin: 0.5rem 0; } input, select, button { font: inherit; } input[type="text"], input[type="number"], select { padding: 0.3rem 0.5rem; border: 1px solid #c4ccd6; border-radius: 6px; } .error { color: #b42318; margin: 0.25rem 0; }
Runs with Vite on your computer: unzip, then npm install and npm run dev.

Check yourself

  1. What two props make an input controlled, and what happens with only one?
  2. Why does handleSubmit call event.preventDefault()?
  3. Two sibling components need the same data. Where does the state go, and how do the siblings change it?

Was this article helpful?

Your response is saved on this device.