use-toggle
Switch between given values
Import
Source
Docs
Package
Usage
use-toggle
implements a common state pattern – it switches state between given values:
API
The hook accepts an array as single argument, the first option will be used as the default value.
The 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:
Set type
By default, TypeScript will guess your type, but in most cases it's better to use const assertion to prevent type widening: