Reactivity API: Utilities
Utility functions for inspecting and working with reactive state.isRef()
Checks if a value is a ref object.The value to check.
boolean - true if the value is a ref, false otherwise
Example:
- Returns
truefor refs created byref(),shallowRef(),computed(), orcustomRef() - Type guard function that narrows the type to
Ref<T>
unref()
Returns the inner value if the argument is a ref, otherwise returns the argument itself. This is a sugar function forval = isRef(val) ? val.value : val.
A ref or plain value to be converted into the plain value.
T - The unwrapped value
Example:
- Useful for writing functions that accept both refs and plain values
- If the value is a ref, returns
.value - If the value is not a ref, returns the value as-is
toRef()
Used to normalize values / refs / getters into refs. Can also be used to create a ref for a property on a source reactive object.A getter function, an existing ref, a non-function value, or a reactive object.
(Optional) The name of the property in the reactive object.
(Optional) Default value to use if the property is undefined.
Ref<T> - A ref object
Example:
- Can normalize a value, ref, or getter function into a ref
- When passed a reactive object and key, creates a ref that stays in sync with the object’s property
- The created ref is synced with its source property: mutating the source property updates the ref, and vice-versa
- Useful for passing a single property of a reactive object to a composable while preserving reactivity
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 usingtoRef().
A reactive object to be converted into an object of refs.
ToRefs<T> - A plain object with ref properties
Example:
- Useful when returning a reactive object from a composition function
- Allows consuming components to destructure/spread the returned object without losing reactivity
- Only converts enumerable properties on the source object
- Each ref stays in sync with the corresponding property on the source object
isProxy()
Checks if an object is a proxy created byreactive(), readonly(), shallowReactive(), or shallowReadonly().
The value to check.
boolean - true if the value is a Vue reactive proxy, false otherwise
Example:
isReactive()
Checks if an object is a proxy created byreactive() or shallowReactive().
The value to check.
boolean - true if the value is a reactive proxy, false otherwise
Example:
- Returns
trueeven if the proxy is created byreadonly(), but wraps a reactive proxy created byreactive() - Also returns
truefor proxies created byshallowReactive()
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 value to check.
boolean - true if the value is readonly, false otherwise
Example:
- The proxies created by
readonly()andshallowReadonly()are both considered readonly - A computed ref without a set function is also considered readonly
toRaw()
Returns the raw, original object of a Vue-created proxy.The proxy object for which the raw value is requested.
T - The original, non-proxy object
Example:
toRaw()can return the original object from proxies created byreactive(),readonly(),shallowReactive(), orshallowReadonly()- This is an escape hatch that can be used to temporarily read without incurring proxy access/tracking overhead or write without triggering changes
- It is not recommended to hold a persistent reference to the original object. Use with caution
markRaw()
Marks an object so that it will never be converted to a proxy. Returns the object itself.The object to be marked as “raw”.
Raw<T> - The same object, marked to never be made reactive
Example:
- Useful for values that should never be made reactive, such as complex third-party class instances or Vue component objects
- Used together with shallow APIs like
shallowReactive()to selectively opt-out of deep reactive/readonly conversion - Allows embedding raw, non-proxied objects in your state graph
markRaw()and shallow APIs allow you to selectively opt-out of the default deep reactive/readonly conversion and embed raw, non-proxied objects in your state graph- However, they can be tricky to use. Consider flattening your data structure to avoid the need for them
toReactive()
Returns a reactive proxy of the object. If the value is already reactive, it is returned as-is.The object to convert to a reactive proxy.
UnwrapNestedRefs<T> - A reactive proxy of the object, or the value as-is if not an object
Example:
- This is a convenience method for normalizing values to reactive proxies
- If the input is already reactive, it returns the same reference
- If the input is not an object (primitives), it returns the value unchanged
- Internally converts the object using
reactive()if not already reactive
toReadonly()
Returns a readonly proxy of the object. If the value is already readonly, it is returned as-is.The object to convert to a readonly proxy.
DeepReadonly<UnwrapNestedRefs<T>> - A readonly proxy of the object, or the value as-is if not an object
Example:
- This is a convenience method for normalizing values to readonly proxies
- If the input is already readonly, it returns the same reference
- If the input is not an object (primitives), it returns the value unchanged
- Internally converts the object using
readonly()if not already readonly