AI Workshop: learn to build apps with AI →
Forms: React, how to make a checked checkbox editable

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


I had a checkbox in a React component:

<input name="enable" type="checkbox" />

and I wanted it to be checked by default, yet the user could change its value.

Using

<input name="enable" type="checkbox" checked="checked" />

didn’t work. The checkbox state could not be changed.

The solution was to use the defaultChecked attribute:

<input name="enable" type="checkbox" defaultChecked={true} />

If the checkbox needs to be checked depending on whether the value was checked in a variable (for example in an editing form when you are getting the actual value from the database) you can use

<input name="enable" type="checkbox" defaultChecked={existing_enable_value} />

Lessons in this unit:

0: Introduction
1: Managing forms in React
2: Form Actions
3: ▶︎ React, how to make a checked checkbox editable
4: How I fixed an issue with a React login form state and Browser autofill
5: How to get the value of an input element in React