R
fn

useCached

v0.0.1testeddemo

Caches the value of an external ref and updates it only when the value changes

Examples

ts
const externalValue = ref(0);
const cachedValue = useCached(externalValue);
ts
const externalValue = ref(0);
const cachedValue = useCached(externalValue, (a, b) => a === b, { immediate: true });

Demo

Loading demo…

Signature

ts
export function useCached<Value = unknown>(
  externalValue: MaybeRefOrGetter<Value>,
  comparator: Comparator<Value> = (a, b) => a === b,
  watchOptions?: WatchOptions,
): Ref<Value>{ ... }

Type Parameters

Value= unknown

Parameters

ParameterTypeDescription
externalValueMaybeRefOrGetter<Value>Ref to cache
comparator?Comparator<Value>Comparator function to compare the values
watchOptions?WatchOptions<boolean> | undefinedWatch options

Returns

Ref<Value, Value>Cached ref