fn
usePrevious
v0.0.15testeddemoTrack the previous value of a ref, getter, or reactive source.
Examples
ts
const count = ref(0);
const prev = usePrevious(count);
count.value = 1; // prev.value === 0ts
const count = ref(0);
const prev = usePrevious(count, -1); // prev.value === -1 until count changests
const state = reactive({ n: 1 });
const prev = usePrevious(() => ({ ...state }), undefined, { deep: true });Demo
Loading demo…
Signatures
ts
export function usePrevious<T>(value: MaybeRefOrGetter<T>, initialValue: T, options?: UsePreviousOptions): Readonly<ShallowRef<T>>;ts
export function usePrevious<T>(value: MaybeRefOrGetter<T>, initialValue?: undefined, options?: UsePreviousOptions): Readonly<ShallowRef<T | undefined>>;Type Parameters
TParameters
| Parameter | Type | Description |
|---|---|---|
value | MaybeRefOrGetter<T> | The source value to track |
initialValue? | T | undefined | The initial previous value, or an options object |
options? | UsePreviousOptions | The initial previous value, or an options object |
Returns
Readonly<ShallowRef<T | undefined>>The previous value of the source