packages
useToggle

useToggle

Toggle a boolean state

Usage

export const Component = () => {
  const { on, toggle, setOn, setOff } = useToggle();
 
  return (
    <>
      <p>State: {on ? 'on' : 'off'}</p>
 
      <div>
        <button onClick={() => toggle()} type='button'>
          toggle
        </button>
        <button onClick={() => setOn()} type='button'>
          set on
        </button>
        <button onClick={() => setOff()} type='button'>
          set off
        </button>
      </div>
    </>
  );
};

Arguments


NameTypeRequired?
initialStateboolean

Returns


NameDescriptionType
onState of the hookboolean
toggleFunction that toggles current state of the hook() => void
setOnFunction that sets current state of the hook to true() => void
setOffFunction that sets current state of the hook to false() => void