R
fn

watchOnce

v0.0.15testeddemo

Shorthand for `watch` that automatically stops after the callback fires once.

Examples

ts
const count = ref(0);
watchOnce(count, value => console.log('fired once with', value));
ts
watchOnce([a, b], ([a, b]) => console.log(a, b));

Demo

Loading demo…

Signatures

ts
export function watchOnce<T>(
  source: WatchSource<T>,
  cb: WatchCallback<T, T | undefined>,
  options?: WatchOnceOptions<true>,
): WatchHandle;
ts
export function watchOnce<T extends Readonly<MultiWatchSources>>(
  source: [...T],
  cb: WatchCallback<MapSources<T>, MapOldSources<T, true>>,
  options?: WatchOnceOptions<true>,
): WatchHandle;
ts
export function watchOnce<T extends object>(
  source: T,
  cb: WatchCallback<T, T | undefined>,
  options?: WatchOnceOptions<true>,
): WatchHandle;

Parameters

ParameterTypeDescription
sourceobject | WatchSource<unknown> | MultiWatchSourcesThe reactive source (ref, getter, reactive object, or array of sources) to watch
cbWatchCallbackInvoked once with the new value, old value, and `onCleanup`
options?WatchOnceOptions<boolean> | undefinedWatch options (`immediate`, `deep`, `flush`); `once` is forced on

Returns

WatchHandleA handle to stop watching before the first trigger