@hakuna-matata-ui/hooks
Version:
React hooks for Chakra components
27 lines (20 loc) • 579 B
text/typescript
import { useCallback, useState } from "react"
type InitialState = boolean | (() => boolean)
/**
* React hook to manage boolean (on - off) states
*
* @param initialState the initial boolean state value
*/
export function useBoolean(initialState: InitialState = false) {
const [value, setValue] = useState(initialState)
const on = useCallback(() => {
setValue(true)
}, [])
const off = useCallback(() => {
setValue(false)
}, [])
const toggle = useCallback(() => {
setValue((prev) => !prev)
}, [])
return [value, { on, off, toggle }] as const
}