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/syncRefs.md
1---2category: Reactivity3related: syncRef4---56# syncRefs78Keep target refs in sync with a source ref910## Usage1112```ts13import { syncRefs } from '@vueuse/core'14import { shallowRef } from 'vue'1516const source = shallowRef('hello')17const target = shallowRef('target')1819const stop = syncRefs(source, target)2021console.log(target.value) // hello2223source.value = 'foo'2425console.log(target.value) // foo26```2728### Sync with multiple targets2930You can also pass an array of refs to sync.3132```ts33import { syncRefs } from '@vueuse/core'34import { shallowRef } from 'vue'3536const source = shallowRef('hello')37const target1 = shallowRef('target1')38const target2 = shallowRef('target2')3940const stop = syncRefs(source, [target1, target2])4142console.log(target1.value) // hello43console.log(target2.value) // hello4445source.value = 'foo'4647console.log(target1.value) // foo48console.log(target2.value) // foo49```5051## Watch options5253The options for `syncRefs` are similar to `watch`'s `WatchOptions` but with different default values.5455```ts56export interface SyncRefOptions {57/**58* Timing for syncing, same as watch's flush option59*60* @default 'sync'61*/62flush?: WatchOptionFlush63/**64* Watch deeply65*66* @default false67*/68deep?: boolean69/**70* Sync values immediately71*72* @default true73*/74immediate?: boolean75}76```7778When setting `{ flush: 'pre' }`, the target reference will be updated at [the end of the current "tick"](https://vuejs.org/guide/essentials/watchers.html#callback-flush-timing) before rendering starts.7980```ts81import { syncRefs } from '@vueuse/core'82import { nextTick, shallowRef } from 'vue'8384const source = shallowRef('hello')85const target = shallowRef('target')8687syncRefs(source, target, { flush: 'pre' })8889console.log(target.value) // hello9091source.value = 'foo'9293console.log(target.value) // hello <- still unchanged, because of flush 'pre'9495await nextTick()9697console.log(target.value) // foo <- changed!98```99100## Type Declarations101102```ts103export interface SyncRefsOptions extends ConfigurableFlushSync {104/**105* Watch deeply106*107* @default false108*/109deep?: boolean110/**111* Sync values immediately112*113* @default true114*/115immediate?: boolean116}117/**118* Keep target ref(s) in sync with the source ref119*120* @param source source ref121* @param targets122*/123export declare function syncRefs<T>(124source: WatchSource<T>,125targets: Ref<T> | Ref<T>[],126options?: SyncRefsOptions,127): WatchHandle128```129