Skip to content

computedEager

Category
Export Size
189 B
Last Changed
2 weeks ago
Alias
eagerComputed

Eager computed without lazy evaluation.

INFO

This function will be removed in future version.

TIP

Note💡: If you are using Vue 3.4+, you can use computed right away, you no longer need this function. In Vue 3.4+, if the computed new value does not change, computed, effect, watch, watchEffect, render dependencies will not be triggered. See: https://github.com/vuejs/core/pull/5912

Learn more at Vue: When a computed property can be the wrong tool.

  • Use computed() when you have a complex calculation going on, which can actually profit from caching and lazy evaluation and should only be (re-)calculated if really necessary.
  • Use computedEager() when you have a simple operation, with a rarely changing return value – often a boolean.

Demo

Lazy Computed
Is over 5: false
Renders: 0
Eager Computed
Is over 5: false
Renders: 0
Count: 0

Usage

ts
import { 
computedEager
} from '@vueuse/core'
const
todos
=
ref
([])
const
hasOpenTodos
=
computedEager
(() => !!
todos
.length)
console
.
log
(
hasOpenTodos
.
value
) // false
toTodos.value.push({
title
: 'Learn Vue' })
console
.
log
(
hasOpenTodos
.
value
) // true

Type Declarations

ts
export type 
ComputedEagerOptions
=
WatchOptionsBase
export type
ComputedEagerReturn
<
T
= any> =
Readonly
<
ShallowRef
<
T
>>
/** * * @deprecated This function will be removed in future version. * * Note: If you are using Vue 3.4+, you can straight use computed instead. * Because in Vue 3.4+, if computed new value does not change, * computed, effect, watch, watchEffect, render dependencies will not be triggered. * refer: https://github.com/vuejs/core/pull/5912 * * @param fn effect function * @param options WatchOptionsBase * @returns readonly shallowRef */ export declare function
computedEager
<
T
>(
fn
: () =>
T
,
options
?:
ComputedEagerOptions
,
):
ComputedEagerReturn
<
T
>
/** @deprecated use `computedEager` instead */ export declare const
eagerComputed
: typeof
computedEager

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
IlyaL
Vida Xie
SerKo
Robin
IlyaL
briwa
Doctorwu
Jonathan Tovar Diaz
wheat

Changelog

v14.0.0-alpha.3 on
26a7d - feat: deprecate computedEager (#5034)
v14.0.0-alpha.2 on
e5f74 - feat!: deprecate alias exports in favor of original function names (#5009)
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)
v10.7.2 on
b6d8f - fix: adapt to changes in vue3.4+ (#3689)

Released under the MIT License.

Write AI-Powered code you can trust
Grab 45% off for AIDD Masterclass before it's gone.
Claim Now
20
hours
:
48
minutes
:
35
seconds
: