Skip to content

useToggle

Category
Last Changed
3月6日6时49分

A boolean switcher with utility functions.

Demo

Value: OFF

Usage

js
import { useToggle } from 'comuse-core'

const [value, toggle] = useToggle()

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

js
import { useToggle } from 'comuse-core'
import { ref } from 'vue'

const value = ref(true)
const toggleValue = useToggle(value)

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="toggleValue" />
<!-- recommended to do this -->
<button @click="toggleValue()" />

Source

SourceDemoDocs

Changelog

v3.2.7 on 3/25/2025
8168c - feat: add useToggle

Released under the MIT License.