Form & File Handling
useCheckboxInputState
About
Simple checkbox state management hook that provides a boolean state and props that can be spread directly onto a checkbox input element. This hook simplifies checkbox handling by providing the checked state and an onChange handler in a format that can be directly spread onto input elements.
Examples
Basic checkbox usage
Multiple checkboxes
Todo list with checkboxes
Arguments
Argument | Type | Description | Default |
---|---|---|---|
initialValue | boolean | The initial boolean value for the checkbox | - |
Returns
Returns an object with the following properties:
Return value | Type | Description | Default |
---|---|---|---|
checked | boolean | Whether the checkbox is currently checked | false |
toggle | () => void | Function to toggle the checkbox state | - |
setChecked | (checked: boolean) => void | Function to explicitly set the checked state | - |
inputProps | CheckboxInputProps | Props to spread on the checkbox input element | - |
InputProps
The inputProps
object contains:
Property | Type | Description |
---|---|---|
checked | boolean | Whether the checkbox is checked |
onChange | (event: ChangeEvent<HTMLInputElement>) => void | Change event handler |
TypeScript Support
Performance Notes
- The hook uses
useMemo
anduseCallback
to prevent unnecessary re-renders - All returned functions maintain stable references across renders
- The
inputProps
object is memoized and only updates when the checked state changes