Reactivity API: Advanced
Advanced APIs for fine-grained control over the reactivity system.shallowRef()
Shallow version ofref(). Unlike ref(), the inner value of a shallow ref is stored and exposed as-is, and will not be made deeply reactive.
The value to wrap in a shallow ref.
ShallowRef<T> - A shallow ref object
Example:
- Only the
.valueaccess is reactive, not the nested properties - Unlike
ref(), the inner value is stored as-is without deep reactive conversion - Use for performance optimization when you have large objects or arrays where only the root-level reactivity is needed
- Useful when integrating with external state management systems
triggerRef()
Force triggers effects that depend on a shallow ref. This is typically used after making deep mutations to the inner value of a shallow ref.The shallow ref whose tied effects should be executed.
void
Example:
- Used in conjunction with
shallowRef()to manually trigger effects after deep mutations - Not needed for normal refs - they automatically trigger when mutated
customRef()
Creates a customized ref with explicit control over its dependency tracking and updates triggering.A factory function that receives
track and trigger callbacks and returns an object with get and set methods.Ref<T> - A custom ref object
Example:
track()should be called inside theget()method to register the dependencytrigger()should be called inside theset()method to trigger effects- Provides fine-grained control over when dependency tracking and effect triggering occur
- Useful for implementing custom reactive behaviors like debouncing, throttling, or async updates
shallowReactive()
Shallow version ofreactive(). Unlike reactive(), there is no deep conversion: only root-level properties are reactive.
The source object to make shallowly reactive.
ShallowReactive<T> - A shallow reactive proxy
Example:
- Only root-level properties are made reactive
- Property values are stored and exposed as-is
- Properties with ref values will not be automatically unwrapped
- Use for performance optimization when you only need reactivity at the root level
- Shallow data structures should only be used for root level state in a component
- Avoid nesting them inside deep reactive objects as it creates inconsistent reactivity behavior
shallowReadonly()
Shallow version ofreadonly(). Unlike readonly(), there is no deep conversion: only root-level properties are made readonly.
The source object to make shallowly readonly.
Readonly<T> - A shallow readonly proxy
Example:
- Only root-level properties are made readonly
- Property values are stored and exposed as-is
- Nested objects remain mutable
- Properties with ref values will not be automatically unwrapped
effectScope()
Creates an effect scope object which can capture the reactive effects (computed and watchers) created within it so that these effects can be disposed together.If
true, the scope will not be automatically collected by parent scopes.EffectScope - An effect scope instance
Example:
- Captures reactive effects (computed refs, watchers) created within the scope
- All effects can be disposed together by calling
scope.stop() - Useful for organizing and cleaning up side effects in composables
- Nested scopes are automatically collected by parent scopes unless detached
- Provides
pause()andresume()methods for temporarily suspending effects
getCurrentScope()
Returns the current active effect scope if there is one. Type:EffectScope | undefined - The current active effect scope or undefined
Example:
- Returns
undefinedif called outside of an effect scope - Useful for conditionally registering effects or cleanup callbacks based on scope context
onScopeDispose()
Registers a dispose callback on the current active effect scope. The callback will be invoked when the associated effect scope is stopped.The cleanup callback function to register.
If
true, will not throw a warning when called without an active effect scope.void
Example:
- Similar to
onUnmounted()in component lifecycle, but works with effect scopes - Can be called multiple times to register multiple cleanup callbacks
- Throws a warning in development if called without an active scope (unless
failSilentlyis true) - Useful for cleanup logic in composables that may be used outside of component contexts