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

Can't Set Reactive Property Asynchronously #487

Closed
bendytree opened this issue Aug 21, 2020 · 3 comments · Fixed by #488
Closed

Can't Set Reactive Property Asynchronously #487

bendytree opened this issue Aug 21, 2020 · 3 comments · Fixed by #488
Assignees
Labels
bug Something isn't working

Comments

@bendytree
Copy link

Apologies if I'm totally missing something here but can't get reactivity to work. For example:

<div>{{ count }}</div>
setup() {
    const state = reactive({ count: 1 });
    setTimeout(() => { state.count = 2; }, 1);
    return state;
}

In Vue 3 this works. Count is updated to 2:
https://jsfiddle.net/8ogjahcb/

In composition-api (v1.0.0-beta.10) count never gets updated. It stays at 1.
https://jsfiddle.net/msrz7w4e/

This example was inspired by the Vue 3 docs.

@danielroe
Copy link
Member

danielroe commented Aug 21, 2020

@bendytree Try not returning a reactive object as it will be destructured at that point (and therefore lose reactivity) but rather return an object with refs inside - or even have your reactive object within the returned object.

@bendytree
Copy link
Author

Thanks @danielroe - I should have mentioned it does work if you add return toRefs(state);. I didn't need toRefs in Vue 3 so I was just trying to raise awareness in case it's something they'd want to fix

@antfu
Copy link
Member

antfu commented Aug 22, 2020

Confirmed as a bug, the top-level reactive return is not be handled correctly. If you change to this it will work

   setup() {
    const state = reactive({ count: 1 });
	
     setTimeout(() => {
       state.count = 2;
     }, 1);

    return { state };
   }

Working on the fix

@antfu antfu self-assigned this Aug 22, 2020
@antfu antfu added the bug Something isn't working label Aug 22, 2020
antfu added a commit to antfu/composition-api that referenced this issue Aug 22, 2020
antfu added a commit that referenced this issue Aug 22, 2020
* fix(setup): handle updates for directly return a reactive object, fix #487

* chore: type fix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants