@react-hook/toggle
TypeScript icon, indicating that this package has built-in type declarations

2.0.1 • Public • Published

useToggle()

Bundlephobia Types NPM Version MIT License

npm i @react-hook/toggle

A React hook for toggling between two values

Quick Start

import useToggle from '@react-hook/toggle'

const Component = (props) => {
  const [value, toggle] = useToggle(false, true)

  return (
    <button onClick={toggle}>
      {value === false ? 'Toggle on' : 'Toggle off'}
    </button>
  )
}

API

useToggle(off?, on?, defaultValue?)

function useToggle<Off extends any, On extends any>(
  off: Off,
  on: On,
  defaultValue: Off | On = off
): [Off | On, () => void]
Argument Type Default Required? Description
off Off false No The value of the toggle in its off state
on On true No The value of the toggle in its on state
defaultValue `Off On` off No

LICENSE

MIT

Package Sidebar

Install

npm i @react-hook/toggle

Weekly Downloads

256

Version

2.0.1

License

MIT

Unpacked Size

14.1 kB

Total Files

20

Last publish

Collaborators

  • jaredlunde