React_Labs_Chapter_08_09_Updated
zip13 files11 kBIn React labs: chapters 8–9
React_Lab_Forms_Events_Student.md
React Lab – Forms & Events (Student Guide)
Goal
Build a simple Student Feedback Form to practice controlled inputs, onChange events, and onClick handlers using only useState.
Template Requirement
- Duplicate the provided
lab-forms-templatefolder and rename itreact-forms-<yourname>. - Run
npm installinside the duplicated folder. - Do not rename starter files.
Styling Rules
- The file
src/styles/app.csscontains basic styling. - Feel free to add your own styles to make it look nice!
Part 1 – Text Inputs with onChange
-
In
App.jsx, importuseStateand create state for:const [name, setName] = useState('') const [email, setEmail] = useState('') -
Create two text inputs:
- One for "Full Name" that updates
nameon change - One for "Email" that updates
emailon change
- One for "Full Name" that updates
-
Use the
onChangeevent to update state:<input type="text" value={name} onChange={(e) => setName(e.target.value)} /> -
Display the current values below the inputs to verify they work.
Part 2 – Select Dropdown with onChange
-
Add state for the selected course:
const [course, setCourse] = useState('') -
Create a
<select>dropdown with these options:- "" (empty - "Select a course...")
- "react" - "React Fundamentals"
- "javascript" - "JavaScript Basics"
- "css" - "CSS & Styling"
-
Use
onChangeto update thecoursestate when user selects an option. -
Show the selected course name below the dropdown.
Part 3 – Star Rating with onClick
-
Add state for the rating:
const [rating, setRating] = useState(0) -
Create 5 star buttons (you can use ★ and ☆ characters):
- When clicked, set the rating to that star's number (1-5)
- Filled stars (★) for values ≤ current rating
- Empty stars (☆) for values > current rating
-
Use
onClickto update the rating:<button onClick={() => setRating(3)}>★</button> -
Display "You rated: X/5 stars" below the buttons.
Part 4 – Textarea with onChange
-
Add state for feedback text:
const [feedback, setFeedback] = useState('') -
Create a
<textarea>for detailed feedback. -
Show a character count: "X / 500 characters"
-
Optionally change the counter color when approaching the limit.
Part 5 – Field Validation
-
Add state for error messages:
const [errors, setErrors] = useState({}) -
Create a
validate()function that checks:- Name: Must not be empty, at least 2 characters
- Email: Must not be empty, must contain
@ - Course: Must be selected (not empty string)
- Rating: Must be greater than 0
-
Return an object with error messages:
function validate() { const newErrors = {} if (name.trim().length < 2) { newErrors.name = 'Name must be at least 2 characters' } if (!email.includes('@')) { newErrors.email = 'Please enter a valid email' } // ... more validations return newErrors } -
Display error messages below each field in red.
Part 6 – Form Submission & Thank You Screen
-
Add state to track submission:
const [submitted, setSubmitted] = useState(false) -
Create a Submit button with
onClickhandler that:- Calls
validate()to get errors - If there are errors: save them to state and stop
- If valid: set
submittedtotrue
- Calls
-
When
submittedistrue:- Hide the form
- Show a "Thank You" message with all submitted info
- Display: Name, Email, Course, Rating (with stars), Feedback
-
Add a "Submit Another" button that:
- Resets all form fields
- Sets
submittedback tofalse
Part 7 – Clear Form Button (Bonus)
-
Add a "Clear Form" button next to Submit.
-
When clicked, reset all state values to their initial values.
-
This button should only be visible when the form has some data.
Example Final Layout
┌─────────────────────────────────────┐
│ 📝 Student Feedback Form │
├─────────────────────────────────────┤
│ Full Name: [__________________] │
│ Email: [__________________] │
│ │
│ Course: [Select a course... ▼] │
│ │
│ Rating: ★ ★ ★ ☆ ☆ │
│ You rated: 3/5 stars │
│ │
│ Feedback: │
│ ┌─────────────────────────────┐ │
│ │ │ │
│ │ │ │
│ └─────────────────────────────┘ │
│ 45 / 500 characters │
│ │
│ [Clear Form] [Submit Feedback] │
└─────────────────────────────────────┘
Deliverables
- Working React project with all form controls.
- All inputs are controlled (values come from state).
- Form validates before submission.
- Thank you screen shows after successful submit.
Submission Checklist
- Template duplicated, dependencies installed.
- Text inputs update state with
onChange. - Dropdown updates state with
onChange. - Star rating works with
onClick. - Textarea shows character count.
- Validation errors display below each field.
- Submit button validates and shows thank you screen with all info.
- Form resets after successful submission.
- Clear button resets the form.
Good luck! This lab focuses on the basics – take your time to understand how onChange and onClick connect your UI to React state.