Set A Default Checkbox Value in React
defaultChecked
prop
The defaultChecked: A boolean. Specifies the initial value for type=“checkbox” and type=“radio” inputs.
to set a default checkbox value you need to use the defaultChecked
prop, because using the checked
without an onChange handler, will render an read-only checkbox.
You provided a checked prop to a form field without an onChange handler. This will render a read-only field. If the field should be mutable use defaultChecked. Otherwise, set either onChange or readOnly.
Use case
Here is an exemple on how the defaultChecked
prop can be used:
<input type="checkbox" defaultChecked={something} />