React Forms Checkbox

React Forms – Checkbox

Checkboxes in React can be used in two main ways:

  1. Single checkbox (true/false value)

  2. 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.


 

export default SingleCheckbox;


🧠 Key Notes

  • Use checked instead of value

  • The onChange handler reads event.target.checked (boolean)


2️⃣ Multiple Checkboxes Example

Useful when selecting hobbies, skills, toppings, etc.


 

export default MultiCheckbox;


🧠 Logic Explanation

Action Result
If item checked Add value to array
If unchecked Remove value from array

✅ Checkbox in a Form with Submit


 

export default SurveyForm;


🧠 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

You may also like...