Hooks/State Management/useToggle

useToggle

A custom React hook to toggle between given values.

Example

Loading...

Install

npx shadcn@latest add https://unlogg.com/r/use-toggle.json
pnpm dlx shadcn@latest add https://unlogg.com/r/use-toggle.json
bunx shadcn@latest add https://unlogg.com/r/use-toggle.json

Notes

  • Allows toggling between any set of values, not just true and false. You can provide an array of values to toggle between.

  • Default to toggling between false and true if no options are provided.

  • The toggle function can optionally accept a value to set directly, bypassing the toggle logic.

API Reference

Parameters

PropTypeDefault
options?
readonly T[]
[false, true]

Returns

PropTypeDefault
toggle?
(value?: React.SetStateAction<T>) => void
-
value?
T
-