React Forms Checkbox
React Forms – Checkbox
Checkboxes in React can be used in two main ways:
-
Single checkbox (true/false value)
-
Multiple checkboxes (selecting multiple items)
Since React uses controlled components, the checkbox state is managed using state + onChange handler.
✅ 1️⃣ Single Checkbox Example
Useful for terms & conditions, subscription toggle, etc.
🧠 Key Notes
-
Use
checkedinstead ofvalue -
The
onChangehandler readsevent.target.checked(boolean)
✅ 2️⃣ Multiple Checkboxes Example
Useful when selecting hobbies, skills, toppings, etc.
🧠 Logic Explanation
| Action | Result |
|---|---|
| If item checked | Add value to array |
| If unchecked | Remove value from array |
✅ Checkbox in a Form with Submit
🧠 Best Practices
✔ Use checked instead of value
✔ Store multiple selections in an array
✔ Use event.target.checked to read boolean value
✔ Use consistent naming for readability
🎯 Summary
| Type | Data Stored | Example |
|---|---|---|
| Single Checkbox | Boolean (true/false) |
Accept Terms |
| Multiple Checkbox | Array of values | Select hobbies |
