question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mapState doesn't work with non namespaced module

See original GitHub issue

Version

3.1.1

Reproduction link

https://codesandbox.io/s/vuex-issue-with-mapstate-5zb3x

Steps to reproduce

In the reproduction link, there is a non namespaced module with has a count in it’s state and a doubleCount in it’s getters.

In the HelloWorld component I am trying to use vuex helpers to access the state and the getters like:

...mapState(['count']),
...mapGetters(['doubleCount]')

What is expected?

Since the module it’s not namespaced I should be able to access the state and getters of the module with vuex helpers.

Both mapState and mapGetters should work fine.

What is actually happening?

However, only the mapGetters helper it’s working.


In order to get the mapState to work I should do:

    ...mapState({
      count: state => state.myModule.count
    })

However IMHO, the mapState should work with just a string since the module in the end it’s merged with the global store.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:16
  • Comments:6

github_iconTop GitHub Comments

48reactions
chromicecommented, Oct 22, 2019

@maeldur While technically you are correct, from user perspective it’s the source of confusion.

I can map state of my namespaced modules like this:

 ...mapState('myNamespacedModule', ['pieceOfState'])

…while for not-namespaced modules I have to use object/arrow functions:

...mapState({
  pieceOfState: state => state.myNotNamespacedModule.pieceOfState
})

At the very least mapState() should throw an error, if an array of state variables is passed to it without a namespace. Right now, it just creates computed variables with undefined values and the user (me in my case) stares blankly at the screen for 15 minutes trying to figure out what they are doing wrong.

19reactions
StephenHogstencommented, Jan 5, 2020

Since state is not registered to the global scope, IMO mapState should accept the module name for non-namespaced state properties. It seems needlessly verbose when you’re pulling multiple properties from the state from the same module.

...mapState({
    firstProp: state => state.myNotNamespacedModule.firstProp,
    secondProp: state => state.myNotNamespacedModule.secondProp,
    thirdProp: state => state.myNotNamespacedModule.thirdProp,
    fourthProp: state => state.myNotNamespacedModule.fourthProp
})

versus

...mapState('myNamespacedModule', ['firstProp', 'secondProp', 'thirdProp', 'fourthProp'])

when the namespace property of the module doesn’t otherwise effect the access to state.

I also agree that an error or warning should be thrown when trying to use the map helpers for nonexistent properties, instead of secretly creating empty ones.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mapState on non-namespaced module - vue.js - Stack Overflow
The most efficient way with non-namespaced modules is to define a getter. The advantage is that you don't have to redefine it in...
Read more >
[Solved]-mapState on non-namespaced module-Vue.js
The most efficient way with non-namespaced modules is to define a getter. The advantage is that you don't have to redefine it in...
Read more >
Modules | Vuex
module state is already nested and not affected by namespace option ... When binding a namespaced module to components with the mapState ......
Read more >
[vuex] module namespace not found in mapActions() - Laracasts
[vuex] module namespace not found in mapActions() ... Additionally my old getters do no longer work: ... mapState({ checkoutStatus: state => state.cart.
Read more >
Refactoring Vuex with Map Helpers and Modules - Vue Mastery
There's no need to change how we're mapping with mapState in this component because we are already mapping to event . Now the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found