Reactivity API: Utils

isRef()

Checks if a value is a ref object.

  • Type

    function isRef<T>(r: Ref<T> | unknown): r is Ref<T>

    Note the return type is a type predicate, which means isRef can be used as a type guard:

    let foo: unknown
    if (isRef(foo)) {
      // foo's type is narrowed to Ref<unknown>
      foo.value
    }

unref()

Returns the inner value if the argument is a ref, otherwise return the argument itself. This is a sugar function for val = isRef(val) ? val.value : val.

  • Type

    function unref<T>(ref: T | Ref<T>): T
  • Example

    function useFoo(x: number | Ref<number>) {
      const unwrapped = unref(x)
      // unwrapped is guaranteed to be number now
    }

toRef()

Can be used to create a ref for a property on a source reactive object. The created ref is synced with its source property: mutating the source property will update the ref, and vice-versa.

  • Type

  • Example

    Note this is different from:

    The above ref is not synced with state.foo, because the ref() receives a plain number value.

    toRef() is useful when you want to pass the ref of a prop to a composable function:

toRefs()

Converts a reactive object to a plain object where each property of the resulting object is a ref pointing to the corresponding property of the original object. Each individual ref is created using toRef().

  • Type

  • Example

    toRefs is useful when returning a reactive object from a composable function so that the consuming component can destructure/spread the returned object without losing reactivity:

    toRefs will only generate refs for properties that are enumerable on the source object at call time. To create a ref for a property that may not exist yet, use toRef instead.

isProxy()

Checks if an object is a proxy created by reactive(), readonly(), shallowReactive() or shallowReadonly().

  • Type

isReactive()

Checks if an object is a proxy created by reactive() or shallowReactive().

  • Type

isReadonly()

Checks whether the passed value is a readonly object. The properties of a readonly object can change, but they can't be assigned directly via the passed object.

The proxies created by readonly() and shallowReadonly() are both considered readonly, as is a computed() ref without a set function.

  • Type

Last updated

Was this helpful?