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/refWithControl.md
1---2category: Reactivity3alias: controlledRef4related: computedWithControl5---67# refWithControl89Fine-grained controls over ref and its reactivity.1011## Usage1213`refWithControl` uses `extendRef` to provide two extra functions `get` and `set` to have better control over when it should track/trigger the reactivity.1415```ts16import { refWithControl } from '@vueuse/core'1718const num = refWithControl(0)19const doubled = computed(() => num.value * 2)2021// just like normal ref22num.value = 4223console.log(num.value) // 4224console.log(doubled.value) // 842526// set value without triggering the reactivity27num.set(30, false)28console.log(num.value) // 3029console.log(doubled.value) // 84 (doesn't update)3031// get value without tracking the reactivity32watchEffect(() => {33console.log(num.peek())34}) // 303536num.value = 50 // watch effect wouldn't be triggered since it collected nothing.37console.log(doubled.value) // 100 (updated again since it's a reactive set)38```3940### `peek`, `lay`, `untrackedGet`, `silentSet`4142We also provide some shorthands for doing the get/set without track/triggering the reactivity system. The following lines are equivalent.4344```ts45import { refWithControl } from '@vueuse/core'46// ---cut---47const foo = refWithControl('foo')48```4950```ts51import { refWithControl } from '@vueuse/core'5253const foo = refWithControl('foo')54// ---cut---55// getting56foo.get(false)57foo.untrackedGet()58foo.peek() // an alias for `untrackedGet`59```6061```ts62import { refWithControl } from '@vueuse/core'6364const foo = refWithControl('foo')65// ---cut---66// setting67foo.set('bar', false)68foo.silentSet('bar')69foo.lay('bar') // an alias for `silentSet`70```7172## Configurations7374### `onBeforeChange()`7576`onBeforeChange` option is offered to give control over if a new value should be accepted. For example:7778```ts79import { refWithControl } from '@vueuse/core'80// ---cut---81const num = refWithControl(0, {82onBeforeChange(value, oldValue) {83// disallow changes larger then ±5 in one operation84if (Math.abs(value - oldValue) > 5)85return false // returning `false` to dismiss the change86},87})8889num.value += 190console.log(num.value) // 19192num.value += 693console.log(num.value) // 1 (change been dismissed)94```9596### `onChanged()`9798`onChanged` option offers a similar functionally as Vue's `watch` but being synchronized with less overhead compared to `watch`.99100```ts101import { refWithControl } from '@vueuse/core'102// ---cut---103const num = refWithControl(0, {104onChanged(value, oldValue) {105console.log(value)106},107})108```109110## Type Declarations111112```ts113export interface ControlledRefOptions<T> {114/**115* Callback function before the ref changing.116*117* Returning `false` to dismiss the change.118*/119onBeforeChange?: (value: T, oldValue: T) => void | boolean120/**121* Callback function after the ref changed122*123* This happens synchronously, with less overhead compare to `watch`124*/125onChanged?: (value: T, oldValue: T) => void126}127/**128* Fine-grained controls over ref and its reactivity.129*130* @__NO_SIDE_EFFECTS__131*/132export declare function refWithControl<T>(133initial: T,134options?: ControlledRefOptions<T>,135): ShallowUnwrapRef<{136get: (tracking?: boolean) => T137set: (value: T, triggering?: boolean) => void138untrackedGet: () => T139silentSet: (v: T) => void140peek: () => T141lay: (v: T) => void142}> &143Ref<T, T>144/** @deprecated use `refWithControl` instead */145export declare const controlledRef: typeof refWithControl146```147