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/useCloned.md
1---2category: Utilities3---45# useCloned67Reactive clone of a ref. By default, it use `JSON.parse(JSON.stringify())` to do the clone.89## Usage1011```ts12import { useCloned } from '@vueuse/core'1314const original = ref({ key: 'value' })1516const { cloned } = useCloned(original)1718original.value.key = 'some new value'1920console.log(cloned.value.key) // 'value'21```2223Changes to the source are not reflected in the cloned ref immediately.24Use `{ flush: 'sync' }` to obtain the updated value without delay.2526```ts27const { cloned } = useCloned(original, { flush: 'sync' })2829original.value.key = 'some new value'3031console.log(cloned.value.key) // 'some new value'32```3334## Manual cloning3536```ts37import { useCloned } from '@vueuse/core'3839const original = ref({ key: 'value' })4041const { cloned, sync } = useCloned(original, { manual: true })4243original.value.key = 'manual'4445console.log(cloned.value.key) // 'value'4647sync()4849console.log(cloned.value.key)// 'manual'50```5152## Custom Clone Function5354Using [`klona`](https://www.npmjs.com/package/klona) for example:5556```ts57import { useCloned } from '@vueuse/core'58import { klona } from 'klona'5960const original = ref({ key: 'value' })6162const { cloned, isModified, sync } = useCloned(original, { clone: klona })63```6465## Type Declarations6667```ts68export interface UseClonedOptions<T = any> extends WatchOptions {69/**70* Custom clone function.71*72* By default, it use `JSON.parse(JSON.stringify(value))` to clone.73*/74clone?: (source: T) => T75/**76* Manually sync the ref77*78* @default false79*/80manual?: boolean81}82export interface UseClonedReturn<T> {83/**84* Cloned ref85*/86cloned: Ref<T>87/**88* Ref indicates whether the cloned data is modified89*/90isModified: Ref<boolean>91/**92* Sync cloned data with source manually93*/94sync: () => void95}96export type CloneFn<F, T = F> = (x: F) => T97export declare function cloneFnJSON<T>(source: T): T98export declare function useCloned<T>(99source: MaybeRefOrGetter<T>,100options?: UseClonedOptions,101): UseClonedReturn<T>102```103