Skip to content

useToggle

Category
Export Size
200 B
Last Changed
3 minutes ago

A boolean switcher with utility functions.

Demo

Value: OFF

Usage

js
import { useToggle } from '@vueuse/core'

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

const [value, toggle] = useToggle()

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

js
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)

Note: be aware that the toggle function accepts the first argument as the override value. You might want to avoid directly passing the function to events in the template, as the event object will pass in.

html
<!-- caution: $event will be pass in -->
<button @click="toggleDark" />
<!-- recommended to do this -->
<button @click="toggleDark()" />
<!-- caution: $event will be pass in -->
<button @click="toggleDark" />
<!-- recommended to do this -->
<button @click="toggleDark()" />

Source

SourceDemoDocs

Contributors

Anthony Fu
elky
Jelf
webfansplz
Alex Kozack
Alexey Iskhakov

Changelog

v10.0.0-beta.4 on 4/13/2023
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v8.9.1 on 7/8/2022
a9ccc - feat(all): use MaybeComputedRef (#1768)
v8.4.0 on 5/3/2022
7aaeb - feat: new options (#1523)

Released under the MIT License.