Vue: Computed documentation does not mention lazy evaluation

Created on 20 Feb 2018  路  7Comments  路  Source: vuejs/vue

What problem does this feature solve?

Truth is: Computed variables are evaluated lazily.

Documentation says: Computed props are evaluated any time the underlying data is changed.

Difference is, computed props are often not re-evaluated when underlying data changes (when in development for example, and no template yet consumes it, or, when hidden due to conditional rendering)

What does the proposed API look like?

Please add a special note/caveat (red exclamation point), explaining the lazy evaluation of computed props, and suggest that a watcher be used if continuous evaluation is required.

Most helpful comment

@yyx990803 I had a long discussion with FEA5T on the forums. What he mainly refers to is that computed props are only evaluated when they are accessed. until you access it (directly or indirectly in the render function, for example), there won't be a value in devtools, for example.

I think a mention in the docs would be fine, since otherwise people can be confused as to why a computed prop didn't update even though one of its dependencies changed.

Such a reason might be that the part of the render tree using it on the next re-render is skipped because of a conditional. People using a console.log in the computed prop might be confused as to why nothing is logging to console.

@FEA5T Documentation issues should be opened at the vuejs/vuejs.org repository.

All 7 comments

@yyx990803 This documentation does not mention lazy evaluation. Which is why I reported this. Because the documentation you linked...does not disclose lazy evaluation, and is misleading documentation.

@FEA5T if, as you described, computed properties "are often not re-evaluated when underlying data changes", then it's a bug, because we are not aware of such situations. In that case, you need to provide a bug report with reproductions.

@yyx990803 True or false:

If a computed property is not used in dom or anywhere else, it will not re-evaluate even when data changes.

@yyx990803 I had a long discussion with FEA5T on the forums. What he mainly refers to is that computed props are only evaluated when they are accessed. until you access it (directly or indirectly in the render function, for example), there won't be a value in devtools, for example.

I think a mention in the docs would be fine, since otherwise people can be confused as to why a computed prop didn't update even though one of its dependencies changed.

Such a reason might be that the part of the render tree using it on the next re-render is skipped because of a conditional. People using a console.log in the computed prop might be confused as to why nothing is logging to console.

@FEA5T Documentation issues should be opened at the vuejs/vuejs.org repository.

@LinusBorg Thanks for translating.

I do apologize for posting in the wrong repo, I didn't see that one initially, thanks for the tip.

This is related to the issue mentioned here as well: https://github.com/vuejs/vue-devtools/issues/393

User's do not understand why computed props are not updated before being accessed - which I think is critical to point out because otherwise it can be mistaken as a bug and be a source of frustration.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bfis picture bfis  路  3Comments

6pm picture 6pm  路  3Comments

aviggngyv picture aviggngyv  路  3Comments

gkiely picture gkiely  路  3Comments

robertleeplummerjr picture robertleeplummerjr  路  3Comments