Hooks
Toggle

useToggle

A hook that manage toggle. A simple abstraction to play with a boolean, don't repeat yourself.

API

demo.js
import { useToggle } from 'easy-hooks-ts'
 
export const Component = () => {
  const [value, toggle, setValue] = useToggle()
 
  const customToggle = () => setValue((x: boolean) => !x)
 
  return (
    <>
      <p>
        Value is <code>{value.toString()}</code>
      </p>
      <button onClick={() => setValue(true)}>set true</button>
      <button onClick={() => setValue(false)}>set false</button>
      <button onClick={toggle}>toggle</button>
      <button onClick={customToggle}>custom toggle</button>
    </>
  )
}

Demo