Skip to content

useToggle()

useToggle(initialValue?): [boolean, () => void]

Defined in: src/hooks/state/useToggle.ts:30

A custom React hook for managing a boolean toggle state. Provides the current value and a function to toggle it between true and false.

Parameters

initialValue?

boolean = false

The initial state of the toggle. Defaults to false.

Returns

[boolean, () => void]

An array containing:

  • value: The current boolean state.
  • toggle: A function to toggle the state between true and false.

Example

tsx
import { useToggle } from '@zl-asica/react';

const ToggleComponent = () => {
  const [isOn, toggle] = useToggle(false);

  return (
    <div>
      <p>The toggle is {isOn ? 'ON' : 'OFF'}</p>
      <button onClick={toggle}>Toggle</button>
    </div>
  );
};

Released under the MIT License.