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

Mutating a shallowRef object using $patch doesn't trigger reactivity #2861

Open
nguyenalter opened this issue Dec 19, 2024 · 1 comment
Open
Labels
✨ enhancement New feature or request

Comments

@nguyenalter
Copy link

nguyenalter commented Dec 19, 2024

Reproduction

https://stackblitz.com/edit/github-5ayhfs4m?file=src%2FApp.vue

Steps to reproduce the bug

  1. Define a shallowRef state (e.g. const counter = shallowRef({ count: 0 })) using setup stores syntax
  2. Use the store in a Vue component
  3. Update the state using $patch({ counter: { count: 1 }) syntax
  4. The state is updated in store but the reactivity is not triggered

Expected behavior

At step 4, the Vue's reactivity system should trigger

Actual behavior

At step 4, the Vue's reactivity system does not trigger (no watcher/computed run after the state change)

Additional information

In the playground, I tested with two other ways to update the shallowRef's state, and the reactivity still works normally:

const counterStore = useCounterStore();
// Using $patch with update function: trigger reactivity
counterStore.$patch((state) => {
    state.counter2 = { count: state.counter2.count + 1 };
  });
// Direct mutate the state: trigger reactivity
counterStore.counter3 = { count: ... };
Copy link
Member

posva commented Dec 19, 2024

This is inherent to how shallowRef work. You can use the other ways to call triggerRef().

That being said, it's possible to make this work by calling triggerRef when a key of a shallow object is traversed for the $patch(patchObject)`. For the function syntax, it's up to the user to properly handle this.

@posva posva added the ✨ enhancement New feature or request label Dec 19, 2024 — with Volta.net
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants