Reactivity API: Advanced

shallowRef()

Shallow version of ref().

  • Type

    function shallowRef<T>(value: T): ShallowRef<T>
    
    interface ShallowRef<T> {
      value: T
    }
  • 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

    const state = shallowRef({ count: 1 })
    
    // does NOT trigger change
    state.value.count = 2
    
    // does trigger change
    state.value = { count: 2 }

triggerRef()

Force trigger effects that depends on a shallow ref. This is typically used after making deep mutations to the inner value of a shallow ref.

  • Type

  • Example

customRef()

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:

shallowReactive()

Shallow version of reactive().

  • 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

shallowReadonly()

Shallow version of readonly().

  • 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

toRaw()

Returns the raw, original object of a reactivity-created proxy.

  • Type

  • Details

    toRaw() can return the original object from proxies created by reactive(), readonly(), shallowReactive() or shallowReadonly().

    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

markRaw()

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.

effectScope()

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 RFC.

  • Type

  • Example

getCurrentScope()

Returns the current active effect scope if there is one.

  • Type

onScopeDispose()

Registers a dispose callback on the current active effect scope. The callback will be invoked when the associated effect scope is stopped.

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

Last updated

Was this helpful?