Skip to content
On this page

useDefault

Returns the default value when state is null or undefined.

Demo

value: 100 default: 0
<template>
<div>value: {{ value }} default: 0</div>
<button @click="setValue(1)">set 1</button>
<button @click="setValue(null)">set null</button>
<button @click="setValue(undefined)">set undefined</button>
</template>

<script setup lang="ts">
import { useDefault } from 'v3-use'

const [value, setValue] = useDefault<number | undefined | null>(0, 100)
</script>

Usage

const [state, setState] = useDefault(0, 100)
const [state, setState] = useDefault(0, 100)

Reference

Type Declarations

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