Category | Utilities |
---|---|
Export Size | 499 B |
Last Changed | 2 weeks ago |
Reactive clone of a ref. By default, it use JSON.parse(JSON.stringify())
to do the clone.
import { useCloned } from '@vueuse/core' const original = ref({ key: 'value' }) const { cloned } = useCloned(original) original.key = 'some new value' console.log(cloned.value.key) // 'some new value'
import { useCloned } from '@vueuse/core' const original = ref({ key: 'value' }) const { cloned, sync } = useCloned(original, { manual: true }) original.key = 'manual' console.log(cloned.value.key) // 'value' sync() console.log(cloned.value.key)// 'manual'
Using klona
for example:
import { useCloned } from '@vueuse/core' import { klona } from 'klona' const original = ref({ key: 'value' }) const { cloned, sync } = useCloned(original, { clone: klona })
export interface UseClonedOptions<T = any> extends WatchOptions { /** * Custom clone function. * * By default, it use `JSON.parse(JSON.stringify(value))` to clone. */ clone?: (source: T) => T /** * Manually sync the ref * * @default false */ manual?: boolean } export interface UseClonedReturn<T> { /** * Cloned ref */ cloned: ComputedRef<T> /** * Sync cloned data with source manually */ sync: () => void } export type CloneFn<F, T = F> = (x: F) => T export declare function cloneFnJSON<T>(source: T): T export declare function useCloned<T>( source: MaybeRefOrGetter<T>, options?: UseClonedOptions ): { cloned: Ref<UnwrapRef<T>> sync: () => void }
© 2019-present Anthony Fu
Licensed under the MIT License.
https://vueuse.org/core/useCloned/