Reactivity API: Core
Last updated
Was this helpful?
Last updated
Was this helpful?
Takes an inner value and returns a reactive and mutable ref object, which has a single property .value
that points to the inner value.
Type
Details
The ref object is mutable - i.e. you can assign new values to .value
. It is also reactive - i.e. any read operations to .value
are 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 . This also means if the object contains nested refs, they will be deeply unwrapped.
To avoid the deep conversion, use instead.
Example
Type
Example
Creating a readonly computed ref:
Creating a writable computed ref:
Debugging:
Returns a reactive proxy of the object.
Type
Details
It should also be noted that there is no ref unwrapping performed when the ref is accessed as an element of a reactive array or a native collection type like Map
.
Example
Creating a reactive object:
Ref unwrapping:
Note that refs are not unwrapped when accessed as array or collection elements:
Type
Details
A readonly proxy is deep: any nested property accessed will be readonly as well. It also has the same ref-unwrapping behavior as reactive()
, except the unwrapped values will also be made readonly.
Example
Runs a function immediately while reactively tracking its dependencies and re-runs it whenever the dependencies are changed.
Type
Details
The first argument is the effect callback function to be run. Optionally this effect callback can return a cleanup function.
The second argument is an optional options object that can be used to adjust the effect's flush timing or to debug the effect's dependencies. By default effects run synchronously, but when they are used in the component scope "type" can be set to update or layout. When the "type" is set to "update" the effect will run after the component is updated and rendered with the latest state. When the "type" is set to "layout" the effect will run after the component is updated but before it's painted. You can relate their timings with useEffect and useLayoutEffect hooks.
The return value is a disposer function that can be called to stop the effect from running again.
Example
Stopping the watcher:
Cleanup effect:
Options:
Watches one or more reactive data sources and invokes a callback function when the sources change.
Type
Details
watch()
is lazy by default - i.e. the callback is only called when the watched source has changed.
The first argument is the watcher's source. The source can be one of the following:
A getter function that returns a value
A ref
A reactive object
The second argument is the callback that will be called when the source changes. The callback receives two arguments: the new value and the old value.
The third optional argument is an options object that supports the following options:
immediate
: trigger the callback immediately on watcher creation. Old value will be undefined
on the first call.
deep
: force deep traversal of the source if it is an object, so that the callback fires on deep mutations.
onTrack / onTrigger
: debug the watcher's dependencies.
Perform the side effect lazily;
Be more specific about what state should trigger the watcher to re-run;
Access both the previous and current value of the watched state.
Example
Watching a getter:
Watching a ref:
When using a getter source, the watcher only fires if the getter's return value has changed. If you want the callback to fire even on deep mutations, you need to explicitly force the watcher into deep mode with { deep: true }
. Note in deep mode, the new value and the old will be the same object if the callback was triggered by a deep mutation:
When directly watching a reactive object, the watcher is automatically in deep mode:
Stopping the watcher:
Takes a getter function and returns a readonly reactive 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.
The reactive conversion is "deep": it affects all nested properties. A reactive object also deeply unwraps any properties that are while maintaining reactivity.
To avoid the deep conversion and only retain reactivity at the root level, use instead.
The returned object and its nested objects are wrapped with and not equal to the original objects. It is recommended to work exclusively with the reactive proxy and avoid relying on the original object.
When assigning a to a reactive
property, that ref will also be automatically unwrapped:
Takes an object (reactive or plain) or a and returns a readonly proxy to the original.
To avoid the deep conversion, use instead.
Alias of with type: 'update'
option.
Alias of with type: 'layout'
option.
type
: adjust the callback's flush timing. See .
Compared to , watch()
allows us to:
watch()
shares the same flush timing and debugging options with