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 |
Usage
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"