useToggle

A hook to toggle a boolean value with useToggle.

Install:

npm i @uidotdev/usehooks

Description:

Basically, what this hook does is that, it takes a parameter with value true or false and toggles that value to opposite. It’s useful when we want to take some action into its opposite action, for example: show and hide modal, show more/show less text, open/close side menu.

Parameters

NameTypeDescription
initialValueboolean(Optional) The initial value of the toggle state.

Return Value

The useToggle hook returns an array with the following elements:

IndexTypeDescription
0booleanThe current state of the toggle.
1functionA function to toggle the state of the toggle.

Demo:

Example:

More Hooks:

Sort: