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

Error with storeToRefs in Vue 2 #852

Closed
Djaler opened this issue Nov 29, 2021 · 10 comments
Closed

Error with storeToRefs in Vue 2 #852

Djaler opened this issue Nov 29, 2021 · 10 comments
Labels
contribution welcome good first issue Good for newcomers upstream The fix depends on a dependency vue 2.x Specific to Vue 2 usage
Projects

Comments

@Djaler
Copy link
Contributor

Djaler commented Nov 29, 2021

Reproduction

https://github.com/Djaler/pinia-vue-2-store-to-refs

Steps to reproduce the behavior

  1. npm install
  2. npm run dev
  3. Open in browser

Expected behavior

Two numbers displayed beside a button.

Actual behavior

Value, created using storeToRefs is undefined

Additional information

If we debug storeToRefs function, we'll see that store['counter'] is just a plain number, not a ref.

@posva
Copy link
Member

posva commented Nov 29, 2021

This is because of vuejs/composition-api#844, use toRefs() as a workaround for the moment.

@posva posva added the upstream The fix depends on a dependency label Nov 29, 2021
@posva posva added the vue 2.x Specific to Vue 2 usage label Jan 27, 2022
@posva posva added this to To do in 2.1 Feb 9, 2022
@awacode21

This comment was marked as off-topic.

@awacode21

This comment was marked as off-topic.

@posva

This comment was marked as off-topic.

@wujekbogdan
Copy link

If this is an issue on Vue end, and it won't work until this bug is fixed, then it would be great to update Pinia docs so that people are not expecting this feature to work with Vue 2.x

@Sjoerd82
Copy link

Sjoerd82 commented Feb 19, 2022

This is because of vuejs/composition-api#844, use toRefs() as a workaround for the moment.

Does not solve the issue for me, or I'm doing it wrong. Are you saying to simply replace storeToRefs with toRefs? Cause that still gives me an undefined.

@posva
Copy link
Member

posva commented Feb 21, 2022

For the moment, I think the best is to add a runtime dev-only warning if the user is using Vue 2 that says this feature is not supported, recommend them to use toRefs() instead and link to this issue. This should be a good first issue if anybody wants to pick it up!

@wujekbogdan
Copy link

That's a good idea.

@a573367014
Copy link

Can we use other ways to avoid it first?

function storeToRefs(store) {
    store = toRaw(store);
    const refs = {};
    for (const key in store) {
        const value = store[key];
        if (isRef(value) || isReactive(value)) {
            // @ts-expect-error: the key is state or getter
            refs[key] =
                // ---
                toRef(store, key);
        }
        else if(key in store.$state) {
            refs[key] = computed(() => store[key])
        }
    }
    return refs;
}

@zhanglolo
Copy link

Can we use other ways to avoid it first?

function storeToRefs(store) {
    store = toRaw(store);
    const refs = {};
    for (const key in store) {
        const value = store[key];
        if (isRef(value) || isReactive(value)) {
            // @ts-expect-error: the key is state or getter
            refs[key] =
                // ---
                toRef(store, key);
        }
        else if(key in store.$state) {
            refs[key] = computed(() => store[key])
        }
    }
    return refs;
}

work well with store which defined by use function(setup style)?

posva added a commit that referenced this issue Mar 14, 2022
@posva posva closed this as completed in 0f24ad2 Mar 31, 2022
2.1 automation moved this from To do to Done Mar 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution welcome good first issue Good for newcomers upstream The fix depends on a dependency vue 2.x Specific to Vue 2 usage
Projects
No open projects
2.1
Done
Development

No branches or pull requests

7 participants