ref-default
View a nullable ref through a default value.
| Package | @vizejs/composable/ref-default |
| Own the source | vize lib pull composable:ref-default |
| Runtime exports | refDefault |
| Gzip budget | 768 B |
Minimal setup
Use a Vue 3.5+ project with Vue SFC compilation configured (see Vite+ integration).
vp install @vizejs/composable
Import the entry directly. Call lifecycle-bound utilities inside <script setup> so cleanup follows the component scope; consult the runtime contract for this entry's SSR and hydration behavior.
import { refDefault } from "@vizejs/composable/ref-default";
Runtime contract
| Utility | Category | Stability | SSR | Hydration | Cleanup | Targets | Host globals | Uses |
|---|---|---|---|---|---|---|---|---|
refDefault |
reactivity | experimental | safe | stable | none | web, server, worker, native, desktop, terminal | — | — |
API
refDefault
View a nullable ref through a default value. Reads return the source value, or the (possibly reactive) default while the source holds null or undefined, so the read type drops those members. Writes go straight to the source and accept its full type, including null/undefined to fall back to the default again. Pure derived state: SSR-safe and nothing to dispose.
function refDefault<Value>( source: Ref<Value>, defaultValue: MaybeRefOrGetter<NonNullable<Value>>, ): WritableComputedRef<NonNullable<Value>, Value>
const raw = ref<string | undefined>();
const name = refDefault(raw, "Anonymous");
name.value; // "Anonymous"
name.value = "Ada"; // raw.value === "Ada"