Skip to content

usePrevious

Category
Export Size
249 B
Last Changed
6 months ago

Holds the previous value of a ref.

Demo

counter: 1

previous: 0

Usage

ts
import { 
usePrevious
} from '@vueuse/core'
import {
shallowRef
} from 'vue'
const
counter
=
shallowRef
('Hello')
const
previous
=
usePrevious
(
counter
)
console
.
log
(
previous
.
value
) // undefined
counter
.
value
= 'World'
console
.
log
(
previous
.
value
) // Hello

Type Declarations

ts
/**
 * Holds the previous value of a ref.
 *
 * @see   {@link https://vueuse.org/usePrevious}
 */
export declare function 
usePrevious
<
T
>(
value
:
MaybeRefOrGetter
<
T
>,
):
Readonly
<
ShallowRef
<
T
| undefined>>
export declare function
usePrevious
<
T
>(
value
:
MaybeRefOrGetter
<
T
>,
initialValue
:
T
,
):
Readonly
<
ShallowRef
<
T
>>

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
Anthony Fu
青椒肉丝

Changelog

v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
10e98 - feat(toRef)!: rename resolveRef to toRef

Released under the MIT License.

Join the Biggest FREE AI-Driven Development Event for Vue Developers
Save My Seat