Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchEffect is called twice on initialisation #2125

Closed
cexbrayat opened this issue Sep 16, 2020 · 3 comments
Closed

watchEffect is called twice on initialisation #2125

cexbrayat opened this issue Sep 16, 2020 · 3 comments
Labels
🐞 bug Something isn't working

Comments

@cexbrayat
Copy link
Member

cexbrayat commented Sep 16, 2020

Version

3.0.0-rc.11

Reproduction link

https://jsfiddle.net/qrh0ubaj/

Steps to reproduce

Consider this component

setup() {
  const price = ref(10);
  const history = ref<Array<string>>([]);
  watchEffect(() => {
    history.value.push(`Price changed to ${price.value}`);
  });
  return { price, history };
}

What is expected?

In rc.10, it only displays one change when initialised (Price changed to 10)

What is actually happening?

In rc.11, it displays this change twice.

@posva posva added the 🐞 bug Something isn't working label Sep 16, 2020
@HcySunYang
Copy link
Member

Related to this: 5f40539
Consider the following use cases, they can all reproduce the issue:

watchEffect(() => {
      // track
      price.value
      // trigger
      price.value = 20
});
watch(price, () => {
      // trigger
      price.value = 20
}, { immediate: true })  // immediate

@unbyte
Copy link
Contributor

unbyte commented Sep 16, 2020

As a supplement to what @HcySunYang pointed out, calling some methods can both trigger and track in one effect, which can lead to an infinite recursion in some scenarios.

e.g. Array.prototype.push, gets .length and sets values,

  const a = reactive([])

  const b = ref(0)
  const c = ref(0)

  watchEffect(() => {
    if (b < 10)
      a.push(1)
  })

  watchEffect(() => {
    if (c > -10)
      a.push(1)
  })

@unbyte
Copy link
Contributor

unbyte commented Sep 16, 2020

Oh, it was fixed while I was typing 😁

@github-actions github-actions bot locked and limited conversation to collaborators Nov 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐞 bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants