syncRef
Keep target refs in sync with a source ref
Demo
Usage
import { syncRef } from '@vueuse/core'
const source = ref('hello')
const target = ref('target')
const stop = syncRef(source, target)
console.log(target.value) // hello
source.value = 'foo'
console.log(target.value) // foo
import { syncRef } from '@vueuse/core'
const source = ref('hello')
const target = ref('target')
const stop = syncRef(source, target)
console.log(target.value) // hello
source.value = 'foo'
console.log(target.value) // foo
Watch options
The options for syncRef
are similar to watch
's WatchOptions
but with different default values.
export interface SyncRefOptions {
/**
* Timing for syncing, same as watch's flush option
*
* @default 'sync'
*/
flush?: WatchOptions['flush']
/**
* Watch deeply
*
* @default false
*/
deep?: boolean
/**
* Sync values immediately
*
* @default true
*/
immediate?: boolean
}
export interface SyncRefOptions {
/**
* Timing for syncing, same as watch's flush option
*
* @default 'sync'
*/
flush?: WatchOptions['flush']
/**
* Watch deeply
*
* @default false
*/
deep?: boolean
/**
* Sync values immediately
*
* @default true
*/
immediate?: boolean
}
When setting { flush: 'pre' }
, the target reference will be updated at the end of the current "tick" before rendering starts.
import { syncRef } from '@vueuse/core'
const source = ref('hello')
const target = ref('target')
syncRef(source, target, { flush: 'pre' })
console.log(target.value) // hello
source.value = 'foo'
console.log(target.value) // hello <- still unchanged, because of flush 'pre'
await nextTick()
console.log(target.value) // foo <- changed!
import { syncRef } from '@vueuse/core'
const source = ref('hello')
const target = ref('target')
syncRef(source, target, { flush: 'pre' })
console.log(target.value) // hello
source.value = 'foo'
console.log(target.value) // hello <- still unchanged, because of flush 'pre'
await nextTick()
console.log(target.value) // foo <- changed!
Related Functions
biSyncRef
Type Declarations
export interface SyncRefOptions extends ConfigurableFlushSync {
/**
* Watch deeply
*
* @default false
*/
deep?: boolean
/**
* Sync values immediately
*
* @default true
*/
immediate?: boolean
}
/**
* Keep target ref(s) in sync with the source ref
*
* @param source source ref
* @param targets
*/
export declare function syncRef<T>(
source: WatchSource<T>,
targets: Ref<T> | Ref<T>[],
{ flush, deep, immediate }?: SyncRefOptions
): WatchStopHandle
export interface SyncRefOptions extends ConfigurableFlushSync {
/**
* Watch deeply
*
* @default false
*/
deep?: boolean
/**
* Sync values immediately
*
* @default true
*/
immediate?: boolean
}
/**
* Keep target ref(s) in sync with the source ref
*
* @param source source ref
* @param targets
*/
export declare function syncRef<T>(
source: WatchSource<T>,
targets: Ref<T> | Ref<T>[],
{ flush, deep, immediate }?: SyncRefOptions
): WatchStopHandle
Source
Contributors
Anthony Fu
zmtlwzy
Matias Capeletto