use-toggle hook implements a common state pattern – it switches state between two given values:
Hook accepts an array with 2 elements 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:
By default, TypeScript will guess your type, but in most cases it's better to use const assertion to prevent type widening: