Skip to content

useToggle

useToggle is a React hook that simplifies managing a boolean state. It provides a function to toggle the state between true and false.

Interface

ts
function useToggle(
  initialValue: boolean = false
): [state: boolean, toggle: () => void];

Parameters

  • initialValueboolean

    The initial state value. Defaults to false.

Return Value

  • [state: boolean, toggle: () => void]

    tuple:

    • stateboolean

      The current state value.

    • toggle() => void

      A function to toggle the state.

Example

tsx
import { useToggle } from 'react-simplikit';

function Component() {
  const [open, toggle] = useToggle(false);

  return (
    <div>
      <p>Bottom Sheet state: {open ? 'opened' : 'closed'}</p>
      <button onClick={toggle}>Toggle</button>
    </div>
  );
}

Released under the MIT License.