Skip to content

Docs improvement: Clarify watchEffect's watch list during conditional dependency access #13688

@ntjess

Description

@ntjess

Vue version

da1f8d7 (retrieved from play.vuejs.org)

Link to minimal reproduction

https://play.vuejs.org/#eNqFUslu4zAM/RWOLnGAjINZTh0nQDMI0JlDW7QFetHFcGhHrSwJWtIAhv+9lJzFBbpcDIvvkXx8ZMcujcl3AdkFK1xlhfHg0Aez5Eq0RlsPHVisZ/BS+mq7rmusPPRQW93ChPImXHFVaeU8tK65hEVkZ5MrlFLDo7Zy820yHTFWBwZnV4KzE5Kq36EL0p8IBP854k5s8NB8AdkUFkuIgJaYS90Q+V63mFiAiZZqjzRnQ1LHFYCoIYti810pAxVA1fgtLOHndMBhLGcgUddzBqkC8gClwy8SVsMj8ePnPEYWZ+tJYzEfbCfD6eGxNbL0SC+AYvtj2XVvvOn7Yk7RhAplgofd91ZvUC44iwI5g/mH6OqAFvNRGzZj3pGXtWjyJ6cVHUKaibNKt0ZItDfGC/Kas4vjtJyVtN6X/ynmbcDZMV5tsXp+J/7k9jHG2a1Fh3aHnJ0wX9oGaWERXt9f457+TyDJD5LYn4BkjZYhahxoq6A2JHvES2r/pXMWqnlw671H5Y5DRaFpQ4nPGV31309GP8v9lf8+bLZn/SsLzxdM

Steps to reproduce

Erase letters in the first input, then add letters in the second, and watch the reactivity work

What is expected?

From the docs descriptions and included examples, it is unclear what should happen when watchEffect is used with conditional ref access in the function body. According to the official description, watchEffect is similar to watch but computes dependencies on synchronous execution. But it is unspecified whether deps are recomputed on every synchronous run, or just on the first execution. Especially given the fact watchEffect uses immediate under the hood, a reasonable assumption might be it uses just these first-encountered deps to determine reactivity.

What is actually happening?

Current behavior (see example link above) does exhibit reactivity on the conditional dependency. But without explicit docs, I'm not sure whether this is an accidental side-effect which may disappear in future updates, or intended behavior.

Using chrome debugger breakpoints reveals the deps actually change per run -- so theoretically, is watchEffect more efficient than watch in this example? For instance, if I used watch([msgA, msgB], (newA, newB) () => {...}), then every time newB changed, whether the function depended on it or not, the callback would execute. In contrast, watchEffect(() => {...}) doesn't trigger breakpoints when msgB changes, if it isn't a dependency based on current conditionals.

These would be helpful scenarios to clarify in the docs.

Thanks!

System Info

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    need documentationNot necessarily a bug, but proper documentation is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions