Skip to content

VueUseCollection of Vue Composition Utilities

Collection of Essential Vue Composition Utilities

useManualRefHistory

Category
Export Size
505 B
Last Changed
last year
Related

Manually track the change history of a ref when the using calls commit(), also provides undo and redo functionality

Demo

Count: 0
/

History (limited to 10 records for demo)
2026-03-21 13:56:47{ value: 0 }

Usage

ts
import { 
useManualRefHistory
import {
shallowRef
const
counter
=
shallowRef
(0)
const {
history
,
commit
,
undo
,
redo
} =
useManualRefHistory
(
counter
)
counter
.
value
+= 1
commit
()
console
.
log
(
history
.
value
)

You can use undo to reset the ref value to the last history point.

ts
console
.
log
(
counter
.
value
) // 1
undo
()
console
.
log
(
counter
.
value
) // 0
ts
import { 
useManualRefHistory
import {
ref
const
counter
=
ref
({
foo
: 1,
bar
: 2 })
const {
history
,
commit
,
undo
,
redo
} =
useManualRefHistory
(
counter
, {
clone
: true })
counter
.
value
.
foo
+= 1
commit
()

Custom Clone Function

To use a full featured or custom clone function, you can set up via the clone options.

For example, using structuredClone:

ts
import { 
useManualRefHistory
const
refHistory
=
useManualRefHistory
(target, {
clone
:
structuredClone
})

Or by using lodash's cloneDeep:

ts
import { 
useManualRefHistory
import {
cloneDeep
const
refHistory
=
useManualRefHistory
(target, {
clone
:
cloneDeep
})

Or a more lightweight klona:

ts
import { 
useManualRefHistory
import {
klona
const
refHistory
=
useManualRefHistory
(target, {
clone
:
klona
})

Custom Dump and Parse Function

Instead of using the clone options, you can pass custom functions to control the serialization and parsing. In case you do not need history values to be objects, this can save an extra clone when undoing. It is also useful in case you want to have the snapshots already stringified to be saved to local storage for example.

ts
import { 
useManualRefHistory
const
refHistory
=
useManualRefHistory
(target, {
dump
:
JSON
.
stringify
,
parse
:
JSON
.
parse
,
})

History Capacity

We will keep all the history by default (unlimited) until you explicitly clear them up, you can set the maximal amount of history to be kept by capacity options.

ts
import { 
useManualRefHistory
const
refHistory
=
useManualRefHistory
(target, {
capacity
: 15, // limit to 15 history records
})
refHistory
.
clear
() // explicitly clear all the history

Type Declarations

Show Type Declarations
ts
export interface 
UseRefHistoryRecord
<
T
> {
snapshot
:
T
timestamp
: number
} export interface
UseManualRefHistoryOptions
<
Raw
,
Serialized
=
Raw
> {
capacity
?: number
clone
?: boolean |
CloneFn
<
Raw
>
dump
?: (
v
:
Raw
) =>
Serialized
parse
?: (
v
:
Serialized
) =>
Raw
setSource
?: (
source
:
Ref
<
Raw
>,
v
:
Raw
) => void
} export interface
UseManualRefHistoryReturn
<
Raw
,
Serialized
> {
source
:
Ref
<
Raw
>
history
:
Ref
<
UseRefHistoryRecord
<
Serialized
>[]>
last
:
Ref
<
UseRefHistoryRecord
<
Serialized
>>
undoStack
:
Ref
<
UseRefHistoryRecord
<
Serialized
>[]>
redoStack
:
Ref
<
UseRefHistoryRecord
<
Serialized
>[]>
canUndo
:
ComputedRef
<boolean>
canRedo
:
ComputedRef
<boolean>
undo
: () => void
redo
: () => void
clear
: () => void
commit
: () => void
reset
: () => void
export declare function
useManualRefHistory
<
Raw
,
Serialized
=
Raw
>(
source
:
Ref
<
Raw
>,
options
?:
UseManualRefHistoryOptions
<
Raw
,
Serialized
>,
):
UseManualRefHistoryReturn
<
Raw
,
Serialized
>
Anthony Fu
Matias Capeletto
Anthony Fu
SerKo
Robin
IlyaL
trent
Lov`u`e
Егор
丶远方
azaleta
Eduardo Wesley
Sahin D
vaakian X
Hollis Wu
wheat
Alex Kozack

Changelog

v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.2.0 on
b46d2 - fix: canUndo and canRedo typing to be computed ref (#4261)

Released under the MIT License.

The new era of AI engineering.
Workflows, artifacts, and judgment that scale.
Request Early Access