Skip to content
On this page

useToggle

Category
Last Changed
2 months ago

A boolean switcher with utility functions.

Demo

Value: OFF

Usage

import { useToggle } from '@vueuse/core'

const [value, toggle] = useToggle()
import { useToggle } from '@vueuse/core'

const [value, toggle] = useToggle()

When you pass a ref, useTogglewill return a simple toggle function instead:

import { useDark, useToggle } from '@vueuse/core'

const isDark = useDark()
const toggleDark = useToggle(isDark)
import { useDark, useToggle } from '@vueuse/core'

const isDark = useDark()
const toggleDark = useToggle(isDark)

Type Declarations

export interface UseToggleOptions<Truthy, Falsy> {
  truthyValue?: MaybeRef<Truthy>
  falsyValue?: MaybeRef<Falsy>
}
export declare function useToggle<Truthy, Falsy, T = Truthy | Falsy>(
  initialValue: Ref<T>,
  options?: UseToggleOptions<Truthy, Falsy>
): (value?: T) => T
export declare function useToggle<
  Truthy = true,
  Falsy = false,
  T = Truthy | Falsy
>(
  initialValue?: T,
  options?: UseToggleOptions<Truthy, Falsy>
): [Ref<T>, (value?: T) => T]
export interface UseToggleOptions<Truthy, Falsy> {
  truthyValue?: MaybeRef<Truthy>
  falsyValue?: MaybeRef<Falsy>
}
export declare function useToggle<Truthy, Falsy, T = Truthy | Falsy>(
  initialValue: Ref<T>,
  options?: UseToggleOptions<Truthy, Falsy>
): (value?: T) => T
export declare function useToggle<
  Truthy = true,
  Falsy = false,
  T = Truthy | Falsy
>(
  initialValue?: T,
  options?: UseToggleOptions<Truthy, Falsy>
): [Ref<T>, (value?: T) => T]

Source

SourceDemoDocs

Contributors

Anthony Fu
Jelf
webfansplz
Alex Kozack
Alexey Iskhakov

Changelog

v8.4.0 on 5/3/2022
7aaeb - feat: new options (#1523)
v7.6.2 on 2/13/2022
8d190 - fix: toggle function return value (#1256)
useToggle has loaded