useBoolean
is a custom React hook that helps manage boolean state with built-in actions to toggle or set the value directly. It simplifies the state management when working with boolean values and provides a clean and flexible API for handling state updates.
To install the useBoolean
hook, you can use npm or yarn:
npm install @d3vtool/use-boolean
or
yarn add @d3vtool/use-boolean
Importing the hook and using
import React from 'react';
import useBoolean from "@d3vtool/use-boolean";
const ToggleComponent: React.FC = () => {
const [state, actions] = useBoolean(false);
return (
<div>
<p>The current state is: {state ? "True" : "False"}</p>
<button onClick={actions.toggle}>Toggle State</button>
<button onClick={() => actions.set(true)}>Set to True</button>
<button onClick={() => actions.set(false)}>Set to False</button>
</div>
);
};
export default ToggleComponent;