use-toggle
Switch between given values
Import
Source
Docs
Package
Usage
use-toggle
hook implements a common state pattern – it switches state between given values:
API
Hook accepts an array as single argument, the first option will be used as the default value.
Hook returns an array with state value and toggle function:
If you do not provide an array with options, then use-toggle
will use boolean values with false
as default:
TypeScript
Set type
By default, TypeScript will guess your type, but in most cases it's better to use const assertion to prevent type widening: