| Category | Reactivity |
|---|---|
| Export Size | 250 B |
| Last Changed | last week |
| Alias | controlledComputed |
| Related | refWithControl |
Explicitly define the dependencies of computed.
import { computedWithControl } from '@vueuse/core'
const source = ref('foo')
const counter = ref(0)
const computedRef = computedWithControl(
() => 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
You can also manually trigger the update of the computed by:
const computedRef = computedWithControl( () => source.value, () => counter.value, ) computedRef.trigger()
Manual triggering only works for Vue 3
export interface ComputedWithControlRefExtra {
/**
* Force update the computed value.
*/
trigger(): void
}
export interface ComputedRefWithControl<T>
extends ComputedRef<T>,
ComputedWithControlRefExtra {}
export interface WritableComputedRefWithControl<T>
extends WritableComputedRef<T>,
ComputedWithControlRefExtra {}
export declare function computedWithControl<T, S>(
source: WatchSource<S> | WatchSource<S>[],
fn: ComputedGetter<T>
): ComputedRefWithControl<T>
export declare function computedWithControl<T, S>(
source: WatchSource<S> | WatchSource<S>[],
fn: WritableComputedOptions<T>
): WritableComputedRefWithControl<T>
export { computedWithControl as controlledComputed }
© 2019-present Anthony Fu
Licensed under the MIT License.
https://vueuse.org/shared/computedWithControl/