usePrevious
Holds the previous value of a ref.
Usage
ts
import { ref } from 'vue'
import { usePrevious } from '@vueuse/core'
const counter = ref('Hello')
const previous = usePrevious(counter)
console.log(previous.value) // undefined
counter.value = 'World'
console.log(previous.value) // Hello
import { ref } from 'vue'
import { usePrevious } from '@vueuse/core'
const counter = ref('Hello')
const previous = usePrevious(counter)
console.log(previous.value) // undefined
counter.value = 'World'
console.log(previous.value) // Hello
Type Declarations
typescript
/**
* Holds the previous value of a ref.
*
* @see {@link https://vueuse.org/usePrevious}
*/
export declare function usePrevious<T>(
value: MaybeComputedRef<T>
): Readonly<Ref<T | undefined>>
export declare function usePrevious<T>(
value: MaybeComputedRef<T>,
initialValue: T
): Readonly<Ref<T>>
/**
* Holds the previous value of a ref.
*
* @see {@link https://vueuse.org/usePrevious}
*/
export declare function usePrevious<T>(
value: MaybeComputedRef<T>
): Readonly<Ref<T | undefined>>
export declare function usePrevious<T>(
value: MaybeComputedRef<T>,
initialValue: T
): Readonly<Ref<T>>
Source
Contributors
Anthony Fu