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

When ref and reactive are used together with computed and watch, computed loses its effect. #13139

Closed
milletlovemouse opened this issue Dec 24, 2023 · 2 comments

Comments

@milletlovemouse
Copy link

Version

2.7.16-beta.2

Reproduction link

codesandbox.io

Steps to reproduce

Go to the reproduction link and first open the console to see the print information.
computed relies on ref or reactive, and watch relies on computed. At this time, computed loses its effect and cannot get the latest data. The following code replaces reactive with ref and has the same effect.

import { ref, reactive, computed, watch } from "vue";
const state = reactive({});
const com1 = computed(() => state.a || []);
const com2 = computed(() => state.b || []);
watch(
  () => com1.value,
  () => {
    console.log(com1.value, com2.value, "watch");
  }
);
window.setTimeout(() => {
  const data = { a: [{ code: 1 }], b: [{ code: 2 }] };
  Object.keys(data).forEach((key) => {
    state[key] = data[key];
  });
  console.log(com1.value, com2.value, "test");
}, 1000);

What is expected?

// print:
[Object] [Object] test
[Object] [Object] watch

What is actually happening?

print:
[] [Object] test
@yyx990803
Copy link
Member

In Vue 2.7, **all Vue 2 change detection caveats still apply.

This means reactive({}) creates an empty object that won't track newly added properties. To ensure properties can be tracked, you need to initialize them with null or undefined values just like in data().

@milletlovemouse
Copy link
Author

In Vue 2.7, **all Vue 2 change detection caveats still apply.

This means reactive({}) creates an empty object that won't track newly added properties. To ensure properties can be tracked, you need to initialize them with null or undefined values just like in data().

I thought vue2.7 would be the same as vue3🥰

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants