use-boolean

1.0.1 • Public • Published

useBoolean React Hook

Convenient helpers for handling boolean state.

Install

npm i use-boolean

Usage

import React from 'react'
import { useBoolean } from 'use-boolean'
 
function App() {
  const [visible, show, hide, toggle] = useBoolean(false)
 
  return (
    <div>
      {visible ? <div>Hello, World!</div> : null}
 
      <button onClick={show}>Show</button>
      <button onClick={hide}>Hide</button>
      <button onClick={toggle}>Toggle</button>
    </div>
  )
}

Live Example

API

useBoolean() call

useBoolean(valueboolean)UseBoolean

UseBoolean return type

type UseBoolean = [boolean, SetTrue, SetFalse, Toggle, SetValue]

SetTrue, SetFalse, Toggle and SetValue all wrapped with useCallback() so you don't need to do it yourself.

Package Sidebar

Install

npm i use-boolean

Weekly Downloads

162

Version

1.0.1

License

MIT

Unpacked Size

3.21 kB

Total Files

4

Last publish

Collaborators

  • garmash