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```2223## Manual cloning2425```ts26import { useCloned } from '@vueuse/core'2728const original = ref({ key: 'value' })2930const { cloned, sync } = useCloned(original, { manual: true })3132original.value.key = 'manual'3334console.log(cloned.value.key) // 'value'3536sync()3738console.log(cloned.value.key)// 'manual'39```4041## Custom Clone Function4243Using [`klona`](https://www.npmjs.com/package/klona) for example:4445```ts46import { useCloned } from '@vueuse/core'47import { klona } from 'klona'4849const original = ref({ key: 'value' })5051const { cloned, isModified, sync } = useCloned(original, { clone: klona })52```5354## Type Declarations5556```ts57export interface UseClonedOptions<T = any> extends WatchOptions {58/**59* Custom clone function.60*61* By default, it use `JSON.parse(JSON.stringify(value))` to clone.62*/63clone?: (source: T) => T64/**65* Manually sync the ref66*67* @default false68*/69manual?: boolean70}71export interface UseClonedReturn<T> {72/**73* Cloned ref74*/75cloned: Ref<T>76/**77* Ref indicates whether the cloned data is modified78*/79isModified: Ref<boolean>80/**81* Sync cloned data with source manually82*/83sync: () => void84}85export type CloneFn<F, T = F> = (x: F) => T86export declare function cloneFnJSON<T>(source: T): T87export declare function useCloned<T>(88source: MaybeRefOrGetter<T>,89options?: UseClonedOptions,90): UseClonedReturn<T>91```92