Watches a single reactive value and executes a callback whenever that value changes.
This function is the synchronous variant of watchValue.
It will re-execute after every fine grained change, even if those changes occur in immediate succession.
syncWatchValue
should therefore be considered a low level primitive, for most use cases watchValue should be the right tool instead.
Example:
import { reactive, syncWatchValue } from "@conterra/reactivity-core";
const v1 = reactive(1);
const v2 = reactive(2);
// Executes whenever the _sum_ of the two values changes.
syncWatchValue(() => v1.value + v2.value, (sum) => {
console.log("new sum", sum);
});
syncWatchValue
returns a handle that can be used to unsubscribe from changes.
That handle's destroy()
function should be called to stop watching when you are no longer interested in updates:
const handle = syncWatchValue(() => someReactive.value, () => {
// ...
});
// later:
handle.destroy();
NOTE: You must not modify the parameters that get passed into
callback
.
a function that returns the value to watch.
a function that will be executed whenever the watched value changes.
Optional
options: Omit<WatchOptions<T>, "dispatch"> & { immediate?: false }additional options.
Use watchValue with option dispatch: "sync"
instead.
This overload is used when immediate
is not set to false
.
a function that returns the value to watch.
a function that will be executed whenever the watched value changes.
Optional
options: Omit<WatchOptions<T>, "dispatch">additional options.
Use watchValue with option dispatch: "sync"
instead.
Deprecated
Use watchValue with option
dispatch: "sync"
instead.