Skip to content

computedWithControl

Category
Export Size
251 B
Last Changed
2 months ago
Alias
controlledComputed
Related

Explicitly define the dependencies of computed.

Usage

ts
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`
)
js
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,
)

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

ts
console
.
log
(
computedRef
.
value
) // 0
counter
.
value
+= 1
console
.
log
(
computedRef
.
value
) // 0
source
.
value
= 'bar'
console
.
log
(
computedRef
.
value
) // 1

Manual Triggering

You can also manually trigger the update of the computed by:

ts
const 
computedRef
=
computedWithControl
(
() =>
source
.
value
,
() =>
counter
.
value
,
)
computedRef
.
trigger
()

Deep Watch

Unlike computed, computedWithControl is shallow by default. You can specify the same options as watch to control the behavior:

ts
const 
source
=
ref
({
name
: 'foo' })
const
computedRef
= computedWithControl(
source
,
() => counter.value, {
deep
: true },
)

Type Declarations

ts
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 type
ComputedWithControlRef
<
T
= any> =
|
ComputedRefWithControl
<
T
>
|
WritableComputedRefWithControl
<
T
>
export declare function
computedWithControl
<
T
,
S
>(
source
:
WatchSource
<
S
> |
WatchSource
<
S
>[],
fn
:
ComputedGetter
<
T
>,
options
?:
WatchOptions
,
):
ComputedRefWithControl
<
T
>
export declare function
computedWithControl
<
T
,
S
>(
source
:
WatchSource
<
S
> |
WatchSource
<
S
>[],
fn
:
WritableComputedOptions
<
T
>,
options
?:
WatchOptions
,
):
WritableComputedRefWithControl
<
T
>
export {
computedWithControl
as
controlledComputed
}

Source

SourceDocs

Contributors

Anthony Fu
Anthony Fu
SerKo
Ronnie Dutta
broBinChen
Robin
Yun Hao
sun0day
kongmoumou

Changelog

v13.4.0 on
ffc1a - fix: allow deeply watching source (#4786)
v13.1.0 on
c1d6e - feat(shared): ensure return types exists (#4659)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.0.0-beta.3 on
5725a - fix: allow optional oldValue parameter in computedWithControl getter (#4132)
v10.8.0 on
a086e - fix: stricter types
v10.0.0-beta.5 on
cb644 - refactor!: remove isFunction and isString utils

Released under the MIT License.

FREE WEEKEND
48 Hours of Unlimited Official Vue.js Certification Training
Reserve Your Spot
23-24 August