Reactivity API: Core
Core APIs for creating and managing reactive state in Vue.js.ref()
Takes an inner value and returns a reactive and mutable ref object, which has a single property.value that points to the inner value.
The value to wrap in the ref. Can be any type.
Ref<UnwrapRef<T>> - A reactive ref object with a .value property
Example:
- The ref object is mutable - you can assign new values to
.value - It’s also reactive - any read operations to
.valueare tracked, and write operations will trigger associated effects - If an object is assigned as a ref’s value, the object is made deeply reactive with
reactive() - To avoid deep conversion, use
shallowRef()instead
reactive()
Returns a reactive proxy of the object.The source object to make reactive.
Reactive<T> - A reactive proxy of the original object
Example:
- The reactive conversion is “deep”: it affects all nested properties
- A reactive object also deeply unwraps any properties that are refs while maintaining reactivity
- The returned proxy is not equal to the original object. It’s recommended to work exclusively with the reactive proxy and avoid relying on the original object
- To avoid deep conversion, use
shallowReactive()instead
- Only works with object types (objects, arrays, and collection types like
MapandSet) - Cannot replace the entire object - the reactivity connection to the first reference is lost
- Not destructure-friendly - primitive properties lose reactivity when destructured
readonly()
Takes an object (reactive or plain) or a ref and returns a readonly proxy to the original.The source object (reactive or plain) or ref to make readonly.
DeepReadonly<UnwrapNestedRefs<T>> - A readonly proxy of the original
Example:
- A readonly proxy is deep: any nested property accessed will be readonly as well
- It has the same ref-unwrapping behavior as
reactive(), except the unwrapped values will also be made readonly - To avoid deep conversion, use
shallowReadonly()instead
computed()
Takes a getter function and returns a readonly reactive ref object for the returned value from the getter. It can also take an object with get and set functions to create a writable ref object.A function that produces the computed value. The function receives the previous computed value as an optional argument.
ComputedRef<T> or WritableComputedRef<T, S> - A readonly or writable computed ref
Example:
- Computed properties are cached based on their reactive dependencies
- A computed property will only re-evaluate when some of its reactive dependencies have changed
- Creating a computed ref without a setter makes it readonly
watch()
Watches one or more reactive data sources and invokes a callback function when the sources change.The watcher’s source. Can be:
- A getter function that returns a value
- A ref
- A reactive object
- An array of the above types
- A function (for watchEffect-style usage)
The callback to call when the source changes. Receives three arguments:
value: the new valueoldValue: the previous valueonCleanup: a function for registering cleanup callbacks
Optional watch options.
Trigger the callback immediately on watcher creation.
Force deep traversal of the source if it is an object. Can be a number to specify max depth.
Run the callback only once.
Custom scheduler for controlling when the callback is invoked.
Called when a reactive property or ref is tracked (dev mode only).
Called when the watcher callback is triggered (dev mode only).
WatchHandle - A handle with methods to control the watcher
Example:
watch()is lazy by default - the callback is only called when the watched source has changed- The first argument is the watcher’s source, which can be a ref, reactive object, getter function, or array of sources
- The second argument is the callback that will be called when the source changes
- The third argument is an optional options object
watchEffect()
Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies change.The effect function to run. Receives an
onCleanup callback for registering cleanup logic.Optional watch options.
Custom scheduler for controlling when the effect is re-run.
Called when a reactive property or ref is tracked (dev mode only).
Called when the effect is triggered (dev mode only).
WatchHandle - A handle to stop the watcher
Example:
watchEffect()runs immediately, unlikewatch()which is lazy- Automatically tracks dependencies during its execution
- Re-runs whenever any tracked dependency changes
- The effect function receives an
onCleanupcallback for registering cleanup logic
- No need to separate source and callback
- Runs immediately on creation
- Doesn’t provide old/new values to the callback
- More convenient for effects that don’t need the previous value