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

Improve documents and examples for composition api store #2065

Open
tuan-lm97 opened this issue Oct 1, 2021 · 10 comments
Open

Improve documents and examples for composition api store #2065

tuan-lm97 opened this issue Oct 1, 2021 · 10 comments
Labels
4.x documentation Improvements or additions to documentation

Comments

@tuan-lm97
Copy link

What problem does this feature solve?

Currently, the document describe how to use composition api of vuex useStore is very limited.
None of the example show how to use useStore in .vue flies setup function and in js/ts files.

What does the proposed API look like?

To the very least, the examples must include useStore() examples, how to import and use them in .vue setup function and in .js/ts files

@kiaking
Copy link
Member

kiaking commented Oct 1, 2021

I think it does? Following page have the exact example on how to use it in setup function.
https://next.vuex.vuejs.org/guide/composition-api.html

And this applies same to js/ts file. What made you confusing?

@kiaking kiaking added documentation Improvements or additions to documentation 4.x labels Oct 1, 2021
@tuan-lm97
Copy link
Author

I think it does? Following page have the exact example on how to use it in setup function.
https://next.vuex.vuejs.org/guide/composition-api.html

And this applies same to js/ts file. What made you confusing?

By examples I mean the examples in this link ( quote from the docs )
https://github.com/vuejs/vuex/tree/4.0/examples/composition
There are 5 examples in this link, none of them use import useStore() from "vuex".

@cuebit
Copy link
Member

cuebit commented Oct 3, 2021

@tuan-lm97 Every example has at least one usage of useStore.

  1. Chat Example
  1. Counter Example
  1. Counter Hot Example
  1. Shopping Cart Example
  1. Todo Example

@tuan-lm97
Copy link
Author

tuan-lm97 commented Oct 3, 2021

@cuebit @kiaking
Sorry, I mean usage of useStore inside js/ts files for reuse purpose.

I know it really silly, but before write this issue, I have wasted 30m figure it out why is useStore always return undefined for me.
My code is like the follow.

// myfile.ts
import { useStore } from "vuex"
export default {
    const myFunc = () => {
        const store = useStore();
        console.log(store)  // undefined
    }
    return { myFunc }
}

Now I know that the problems is because I don't call myFunc inside my setup function, instead I just return to use it in my template.

I know this is mostly because I don't understand how the composition work but it would be nice if there are some examples for beginner don't make silly mistake like this

@kiaking
Copy link
Member

kiaking commented Oct 4, 2021

In that case I think it would make more sense to add that special note to the Docs's composition api page. That says "You must always call useStore inside setup hook". It's a bit redundant because this is not Vuex specific problem but it's about Vue Composition API in general. Though as you said I still think it makes sense to ave helpful note for new comers.

I'm not sure if we should force our example to use external ts/js file. Because it will make the code harder to read in my opinion. Examples should be as simple as possible. And it might give users impression that "you must separate file", or "separating file is a good practice". Which is not always true.

Also, even if we had that example, I think it's evenly hard to understand that the external function must be called inside setup hook just by looking into the example.

So following up the issue in the docs sounds like a good idea to me.

@kiaking kiaking changed the title Improve documents and examples for composition apo store Improve documents and examples for composition api store Oct 11, 2021
@pstaabp
Copy link

pstaabp commented Oct 21, 2021

I would also add that at least one typescript example would be nice. I often have trouble getting the typing on the getters correct.

@tuan-lm97
Copy link
Author

I would also add that at least one typescript example would be nice. I often have trouble getting the typing on the getters correct.

Yes, I also think example should have more variant, especially typescript related stuff. Vue 3 natively support typescript but on the document side, it haven't got enough love

@kinoli
Copy link

kinoli commented Dec 24, 2021

How about an example of using mapGetters within the setup function?

@cuebit
Copy link
Member

cuebit commented Dec 24, 2021

@kinoli you wouldn’t use mapGetters in setup. See #1725

@willwinberg
Copy link

willwinberg commented Jan 19, 2022

@cuebit @kiaking Sorry, I mean usage of useStore inside js/ts files for reuse purpose.

So what would be the proper way to do this? How should we handle using the store in files that aren't components?
It seems undesirable to mix import { useStore } from 'vuex in components and import store from 'vuex' in js/ts files throughout a project

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
4.x documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

6 participants