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

`createNamespacedHelpers` return mappers that need `this` #1684

Closed
davidmeirlevy opened this issue Feb 21, 2020 · 10 comments
Closed

`createNamespacedHelpers` return mappers that need `this` #1684

davidmeirlevy opened this issue Feb 21, 2020 · 10 comments

Comments

@davidmeirlevy
Copy link

@davidmeirlevy davidmeirlevy commented Feb 21, 2020

Version

3.1.2

Reproduction link

[https://jsfiddle.net/davidmeirlevy/9hbqjmg2/34/)

Steps to reproduce

get a mapper:
const { mapActions } = createNamespacedHelpers(MODULE_NAME)

map some action:
const {action} = mapActions(['action'])

run this action directly, without adding it to a component methods:
action()

What is expected?

the action should be dispacthed

What is actually happening?

This line breaks because we didn't ran this function from a vue component context:
var dispatch = this.$store.dispatch;


when we want to use the mappers from the setup() function of the composition-api, the component's this shouldn't be relevant, so you should probably find another way to map actions without expecting being executed as a vue component's method.

davidmeirlevy added a commit to davidmeirlevy/vuex that referenced this issue Feb 23, 2020
@davidmeirlevy

This comment has been minimized.

Copy link
Author

@davidmeirlevy davidmeirlevy commented Feb 24, 2020

I've added a PR to fix this issue:
#1685

After this fix, we can use the namespaced-helpers alongside with the composition-api.

@ktsn

This comment has been minimized.

Copy link
Member

@ktsn ktsn commented Feb 25, 2020

We would not reuse mapXXX helpers for setup usage as they are totally different things and supporting both on one helper function will complicate the implementation. We are working on Vue v3 + Vuex now and considering useXXX composition function for setup.

@kiaking

This comment has been minimized.

Copy link
Member

@kiaking kiaking commented Feb 26, 2020

Also it's not only for createNamespacedHelpers. All of the mapXXX features are meant be used inside Vue Component since they all tries to fetch store instance from the this context.

Let's close this one for now since it's indeed intended behavior, and if we want to have issue for useXXX feature, we should have it as a fresh issue 🤝

@kiaking kiaking closed this Feb 26, 2020
@davidmeirlevy

This comment has been minimized.

Copy link
Author

@davidmeirlevy davidmeirlevy commented Feb 26, 2020

So basically you’re saying that “createNamespacedHelpers” is deprecated?

@kiaking

This comment has been minimized.

Copy link
Member

@kiaking kiaking commented Feb 26, 2020

No. You can use all mapXXX helpers in Vue Component, but outside of setup hook. For setup hook, we probably have other helpers such as useXXX helpers.

@davidmeirlevy

This comment has been minimized.

Copy link
Author

@davidmeirlevy davidmeirlevy commented Feb 26, 2020

There will be something such as “useNamespaced” ?

@kiaking

This comment has been minimized.

Copy link
Member

@kiaking kiaking commented Feb 26, 2020

Probably. Not 100% sure just yet, but I guess something like that 👍

@davidmeirlevy

This comment has been minimized.

Copy link
Author

@davidmeirlevy davidmeirlevy commented Feb 26, 2020

@kiaking

This comment has been minimized.

Copy link
Member

@kiaking kiaking commented Feb 27, 2020

Yeah in terms of API, it looks good 👍 I think you don't have to pass root.store since you can fetch the store instance by inject hook inside the helper functions.

@davidmeirlevy

This comment has been minimized.

Copy link
Author

@davidmeirlevy davidmeirlevy commented Feb 27, 2020

Interesting idea. Thanks.

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

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.