Reactivity API: Advanced
Last updated
Was this helpful?
Last updated
Was this helpful?
Shallow version of .
Type
Details
Unlike ref()
, the inner value of a shallow ref is stored and exposed as-is, and will not be made deeply reactive. Only the .value
access is reactive.
shallowRef()
is typically used for performance optimizations of large data structures, or integration with external state management systems.
Example
Type
Example
Creates a customized ref with explicit control over its dependency tracking and updates triggering.
Type
Details
customRef()
expects a factory function, which receives track
and trigger
functions as arguments and should return an object with get
and set
methods.
In general, track()
should be called inside get()
, and trigger()
should be called inside set()
. However, you have full control over when they should be called, or whether they should be called at all.
Example
Creating a debounced ref that only updates the value after a certain timeout after the latest set call:
Type
Details
Unlike reactive()
, there is no deep conversion: only root-level properties are reactive for a shallow reactive object. Property values are stored and exposed as-is - this also means properties with ref values will not be automatically unwrapped.
Use with Caution
Shallow data structures should only be used for root level state in a component. Avoid nesting it inside a deep reactive object as it creates a tree with inconsistent reactivity behavior which can be difficult to understand and debug.
Example
Type
Details
Unlike readonly()
, there is no deep conversion: only root-level properties are made readonly. Property values are stored and exposed as-is - this also means properties with ref values will not be automatically unwrapped.
Use with Caution
Shallow data structures should only be used for root level state in a component. Avoid nesting it inside a deep reactive object as it creates a tree with inconsistent reactivity behavior which can be difficult to understand and debug.
Example
Returns the raw, original object of a reactivity-created proxy.
Type
Details
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.
Example
Marks an object so that it will never be converted to a proxy. Returns the object itself.
Type
Example
Use with Caution
markRaw()
and shallow APIs such as shallowReactive()
allow you to selectively opt-out of the default deep reactive/readonly conversion and embed raw, non-proxied objects in your state graph. They can be used for various reasons:
Some values simply should not be made reactive, for example a complex 3rd party class instance.
Skipping proxy conversion can provide performance improvements when rendering large lists with immutable data sources.
They are considered advanced because the raw opt-out is only at the root level, so if you set a nested, non-marked raw object into a reactive object and then access it again, you get the proxied version back. This can lead to identity hazards - i.e. performing an operation that relies on object identity but using both the raw and the proxied version of the same object:
Identity hazards are in general rare. However, to properly utilize these APIs while safely avoiding identity hazards requires a solid understanding of how the reactivity system works.
Type
Example
Type
This method can be used as a non-component-coupled replacement of onUnmounted
in reusable composition functions, since each Reactive component's setup()
function is also invoked in an effect scope.
Type
Force trigger effects that depends on a . This is typically used after making deep mutations to the inner value of a shallow ref.
Shallow version of .
Shallow version of .
toRaw()
can return the original object from proxies created by , , or .
Creates an effect scope object which can capture the reactive effects (i.e. computed and watchers) created within it so that these effects can be disposed together. For detailed use cases of this API, please consult its corresponding .
Returns the current active if there is one.
Registers a dispose callback on the current active . The callback will be invoked when the associated effect scope is stopped.