UseToggle

󰛦 useToggle.ts
import { useState, useCallback } from 'react'
export default function useToggle(initialValue = false) {
const [value, setValue] = useState(initialValue)
const toggle = useCallback(() => {
setValue((v) => !v)
}, [])
return [value, toggle]
}

Context

There are times when we have some React state that should always hold a boolean value and should only allow toggling between true and false; there is no need for an intermediary value because the setState callback already has the actual value of the hook and will not change based on scoping.