a function that returns the values to watch.
a function that will be executed whenever the watched values changed.
Optional
options: WatchOptions<Values> & { immediate?: false }additional options.
This overload is used when immediate
is not set to false
.
a function that returns the values to watch.
a function that will be executed whenever the watched values changed.
Optional
options: WatchOptions<Values>additional options.
Watches reactive values and executes a callback whenever those values change.
watch
works like this:selector
is a tracked function that shall return an array of values. Those values are usually obtained by accessing one or more reactive objects.selector
change,callback
will be executed with those values (the old values are available as well). The body ofcallback
is not reactive.The arrays returned by the selector are compared using shallow equality by default: the callback runs if the length of the array changes or if one of its entries has a different identity. Note that you can provide a custom
equal
function to change this behavior.Example:
watch
returns a handle that can be used to unsubscribe from changes. That handle'sdestroy()
function should be called to stop watching when you are no longer interested in updates: