R
fn

toReactive

v0.0.15testeddemo

Convert a ref of object to a reactive proxy. Property reads and writes pass straight through to the ref's current value, so the proxy stays in sync even if the ref is reassigned to a whole new object. Writing a plain value onto a key that currently holds a ref unwraps into that ref's `.value`. Passing a plain object simply returns `reactive(object)`.

Examples

ts
const state = ref({ count: 0 });
const reactiveState = toReactive(state);
reactiveState.count++; // state.value.count === 1
ts
// survives ref reassignment
const obj = ref({ name: 'a' });
const r = toReactive(obj);
obj.value = { name: 'b' };
r.name; // 'b'

Demo

Loading demo…

Signature

ts
export function toReactive<T extends object>(
  objectRef: MaybeRef<T>,
): UnwrapNestedRefs<T>{ ... }

Type Parameters

Textends object

Parameters

ParameterTypeDescription
objectRefMaybeRef<T>A ref of object (or a plain object)

Returns

UnwrapNestedRefs<T>A reactive proxy backed by the ref