Skip to content
On this page

useState

管理 state 的 Hook

Demo

1
<template>
<div>{{ state }}</div>
<button @click="setState(5)">setState(5)</button>
<button @click="setState(c => c + 1)">+</button>
<button @click="setState(c => c - 1)">-</button>
</template>

<script lang="ts" setup>
import { useState } from 'v3-use'
const [state, setState] = useState(1)
</script>

Usage

import { useState } from 'v3-use'
const [state, setState] = useState(1)
import { useState } from 'v3-use'
const [state, setState] = useState(1)

Reference

const [state, setState] = useState<T>(initialValue?: T | (() => T))
const [state, setState] = useState<T>(initialValue?: T | (() => T))

Type Declarations

export declare function useState<T>(
  initialValue: T | (() => T)
): [Ref<UnwrapRef<T>>, (value: T | ((c: UnwrapRef<T>) => T)) => void]
export declare function useState<T = any>(): [
  Ref<T>,
  (value: T | ((c: T) => T)) => void
]
export declare function useState<T>(
  initialValue: T | (() => T)
): [Ref<UnwrapRef<T>>, (value: T | ((c: UnwrapRef<T>) => T)) => void]
export declare function useState<T = any>(): [
  Ref<T>,
  (value: T | ((c: T) => T)) => void
]