Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useRefHistory.md
1---2category: State3related: useManualRefHistory4---56# useRefHistory78Track the change history of a ref, also provides undo and redo functionality910<CourseLink href="https://vueschool.io/lessons/ref-history-with-vueuse?friend=vueuse">Learn useRefHistory with this FREE video lesson from Vue School!</CourseLink>1112## Usage1314```ts {5} twoslash include usage15import { useRefHistory } from '@vueuse/core'16import { shallowRef } from 'vue'1718const counter = shallowRef(0)19const { history, undo, redo } = useRefHistory(counter)20```2122Internally, `watch` is used to trigger a history point when the ref value is modified. This means that history points are triggered asynchronously batching modifications in the same "tick".2324```ts25// @include: usage26// ---cut---27counter.value += 12829await nextTick()30console.log(history.value)31/* [32{ snapshot: 1, timestamp: 1601912898062 },33{ snapshot: 0, timestamp: 1601912898061 }34] */35```3637You can use `undo` to reset the ref value to the last history point.3839```ts40// @include: usage41// ---cut---42console.log(counter.value) // 143undo()44console.log(counter.value) // 045```4647### Objects / arrays4849When working with objects or arrays, since changing their attributes does not change the reference, it will not trigger the committing. To track attribute changes, you would need to pass `deep: true`. It will create clones for each history record.5051```ts52import { useRefHistory } from '@vueuse/core'53// ---cut---54const state = ref({55foo: 1,56bar: 'bar',57})5859const { history, undo, redo } = useRefHistory(state, {60deep: true,61})6263state.value.foo = 26465await nextTick()66console.log(history.value)67/* [68{ snapshot: { foo: 2, bar: 'bar' } },69{ snapshot: { foo: 1, bar: 'bar' } }70] */71```7273#### Custom Clone Function7475`useRefHistory` only embeds the minimal clone function `x => JSON.parse(JSON.stringify(x))`. To use a full featured or custom clone function, you can set up via the `clone` options.7677For example, using [structuredClone](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone):7879```ts80import { useRefHistory } from '@vueuse/core'8182const refHistory = useRefHistory(target, { clone: structuredClone })83```8485Or by using [lodash's `cloneDeep`](https://lodash.com/docs/4.17.15#cloneDeep):8687```ts88import { useRefHistory } from '@vueuse/core'89import { cloneDeep } from 'lodash-es'9091const refHistory = useRefHistory(target, { clone: cloneDeep })92```9394Or a more lightweight [`klona`](https://github.com/lukeed/klona):9596```ts97import { useRefHistory } from '@vueuse/core'98import { klona } from 'klona'99100const refHistory = useRefHistory(target, { clone: klona })101```102103#### Custom Dump and Parse Function104105Instead 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.106107```ts108import { useRefHistory } from '@vueuse/core'109110const refHistory = useRefHistory(target, {111dump: JSON.stringify,112parse: JSON.parse,113})114```115116### History Capacity117118We 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.119120```ts121import { useRefHistory } from '@vueuse/core'122// ---cut---123const refHistory = useRefHistory(target, {124capacity: 15, // limit to 15 history records125})126127refHistory.clear() // explicitly clear all the history128```129130### History WatchOptionFlush Timing131132From [Vue's documentation](https://vuejs.org/guide/essentials/watchers.html#callback-flush-timing): Vue's reactivity system buffers invalidated effects and flush them asynchronously to avoid unnecessary duplicate invocation when there are many state mutations happening in the same "tick".133134In the same way as `watch`, you can modify the flush timing using the `flush` option.135136```ts137import { useRefHistory } from '@vueuse/core'138// ---cut---139const refHistory = useRefHistory(target, {140flush: 'sync', // options 'pre' (default), 'post' and 'sync'141})142```143144The default is `'pre'`, to align this composable with the default for Vue's watchers. This also helps to avoid common issues, like several history points generated as part of a multi-step update to a ref value that can break invariants of the app state. You can use `commit()` in case you need to create multiple history points in the same "tick"145146```ts147import { useRefHistory } from '@vueuse/core'148// ---cut---149const r = shallowRef(0)150const { history, commit } = useRefHistory(r)151152r.value = 1153commit()154155r.value = 2156commit()157158console.log(history.value)159/* [160{ snapshot: 2 },161{ snapshot: 1 },162{ snapshot: 0 },163] */164```165166On the other hand, when using flush `'sync'`, you can use `batch(fn)` to generate a single history point for several sync operations167168```ts169import { useRefHistory } from '@vueuse/core'170// ---cut---171const r = ref({ names: [], version: 1 })172const { history, batch } = useRefHistory(r, { flush: 'sync' })173174batch(() => {175r.value.names.push('Lena')176r.value.version++177})178179console.log(history.value)180/* [181{ snapshot: { names: [ 'Lena' ], version: 2 },182{ snapshot: { names: [], version: 1 },183] */184```185186If `{ flush: 'sync', deep: true }` is used, `batch` is also useful when doing a mutable `splice` in an array. `splice` can generate up to three atomic operations that will be pushed to the ref history.187188```ts189import { useRefHistory } from '@vueuse/core'190// ---cut---191const arr = ref([1, 2, 3])192const { history, batch } = useRefHistory(arr, { deep: true, flush: 'sync' })193194batch(() => {195arr.value.splice(1, 1) // batch ensures only one history point is generated196})197```198199Another option is to avoid mutating the original ref value using `arr.value = [...arr.value].splice(1,1)`.200201## Recommended Readings202203- [History and Persistence](https://patak.dev/vue/history-and-persistence.html) - by [@patak-dev](https://github.com/patak-dev)204205## Type Declarations206207```ts208export interface UseRefHistoryOptions<Raw, Serialized = Raw>209extends ConfigurableEventFilter, ConfigurableFlush {210/**211* Watch for deep changes, default to false212*213* When set to true, it will also create clones for values store in the history214*215* @default false216*/217deep?: boolean218/**219* Maximum number of history to be kept. Default to unlimited.220*/221capacity?: number222/**223* Clone when taking a snapshot, shortcut for dump: JSON.parse(JSON.stringify(value)).224* Default to false225*226* @default false227*/228clone?: boolean | CloneFn<Raw>229/**230* Serialize data into the history231*/232dump?: (v: Raw) => Serialized233/**234* Deserialize data from the history235*/236parse?: (v: Serialized) => Raw237/**238* Function to determine if the commit should proceed239* @param oldValue Previous value240* @param newValue New value241* @returns boolean indicating if commit should proceed242*/243shouldCommit?: (oldValue: Raw | undefined, newValue: Raw) => boolean244}245export interface UseRefHistoryReturn<246Raw,247Serialized,248> extends UseManualRefHistoryReturn<Raw, Serialized> {249/**250* A ref representing if the tracking is enabled251*/252isTracking: Ref<boolean>253/**254* Pause change tracking255*/256pause: () => void257/**258* Resume change tracking259*260* @param [commit] if true, a history record will be create after resuming261*/262resume: (commit?: boolean) => void263/**264* A sugar for auto pause and auto resuming within a function scope265*266* @param fn267*/268batch: (fn: (cancel: Fn) => void) => void269/**270* Clear the data and stop the watch271*/272dispose: () => void273}274/**275* Track the change history of a ref, also provides undo and redo functionality.276*277* @see https://vueuse.org/useRefHistory278* @param source279* @param options280*/281export declare function useRefHistory<Raw, Serialized = Raw>(282source: Ref<Raw>,283options?: UseRefHistoryOptions<Raw, Serialized>,284): UseRefHistoryReturn<Raw, Serialized>285```286