Skip to content
On this page

controlledComputed

Category
Last Changed
3 months ago

Explicitly define the deps of computed.

Usage

import { controlledComputed } from '@vueuse/core'

let source = ref('foo')
let counter = ref(0)

const computedRef = controlledComputed(
  () => source.value, // watch source, same as `watch`
  () => counter.value, // computed getter, same as `computed`
)
import { controlledComputed } from '@vueuse/core'

let source = ref('foo')
let counter = ref(0)

const computedRef = controlledComputed(
  () => source.value, // watch source, same as `watch`
  () => counter.value, // computed getter, same as `computed`
)

With this, the changes of counter won't trigger computedRef to update but the source ref does.

console.log(computedRef.value) // 0

counter.value += 1

console.log(computedRef.value) // 0

source.value = 'bar'

console.log(computedRef.value) // 1
console.log(computedRef.value) // 0

counter.value += 1

console.log(computedRef.value) // 0

source.value = 'bar'

console.log(computedRef.value) // 1

Type Declarations

/**
 * Explicitly define the deps of computed.
 *
 * @param source
 * @param fn
 */
export declare function controlledComputed<T, S>(
  source: WatchSource<S>,
  fn: () => T
): ComputedRef<T>
/**
 * Explicitly define the deps of computed.
 *
 * @param source
 * @param fn
 */
export declare function controlledComputed<T, S>(
  source: WatchSource<S>,
  fn: () => T
): ComputedRef<T>

Source

SourceDocs

Contributors

Anthony Fu
Antério Vieira

Changelog

v6.6.2 on 10/18/2021
db7e6 - feat: expose invalidate function
controlledComputed has loaded