vant/docs/markdown/use-toggle.en-US.md
2021-06-07 20:52:26 +08:00

1.2 KiB

useToggle

Intro

Used to switch between true and false.

Usage

Basic Usage

import { useToggle } from '@vant/use';

export default {
  setup() {
    const [state, toggle] = useToggle();

    toggle(true);
    console.log(state.value); // -> true

    toggle(false);
    console.log(state.value); // -> false

    toggle();
    console.log(state.value); // -> true
  },
};

Default Value

import { useToggle } from '@vant/use';

export default {
  setup() {
    const [state, toggle] = useToggle(true);
    console.log(state.value); // -> true
  },
};

API

Type Declarations

function useToggle(
  defaultValue: boolean
): [Ref<boolean>, (newValue: boolean) => void];

Params

Name Description Type Default Value
defaultValue Default value boolean false

Return Value

Name Description Type
state State Ref<boolean>
toggle Function to switch state (newValue?: boolean) => void