useBoolean

Author
AI Engineer
Languages

Implement a useBoolean hook that manages a boolean state, with additional convenience utility methods.

export default function Component() {
const { value, setTrue, setFalse } = useBoolean();
return (
<div>
<p>{value ? 'enabled' : 'disabled'}</p>
<button onClick={toggle}>Toggle</button>
</div>
);
}

Arguments

  • initialValue: boolean: Initial value of the boolean state. If not provided, it should default to false.

Returns

The useBoolean hook returns an object with the following properties.

  • value: boolean: The current boolean state
  • setTrue: () => void: A function to set the boolean state to true
  • setFalse: () => void: A function to set the boolean state to false