Skip to content

Discussion on removing mapXXX helpers in Vuex 4 #1417

Closed
@chrisvfritz

Description

@chrisvfritz

@yyx990803 @ktsn Regarding this note in the current roadmap for Vuex 4:

Getting rid of mapXXX helpers via scoped-slot based store consumer component

I've experimented with this scoped-slot pattern in some apps, but it has a couple serious limitations that have led to me never using it anymore:

  • Vuex state/getters/actions are only conveniently available in templates and render functions. That means the convenience is unavailable for computed properties that rely on Vuex state and methods that dispatch Vuex actions, which is a big limitation.

  • It requires tight coupling of components to Vuex modules, which I've found doesn't scale well to large apps. Instead, I've found it very useful to never use the mapXXX helpers directly in my components, but in a state helpers file, which describes the public interfaces for all Vuex modules. From this file, I'll import the concerns I care about into individual components. For example:

    import {
      authComputed,
      notificationsComputed,
      notificationsMethods,
      searchMethods,
    } from '@state/helpers'
    
    // ...
    
    computed: {
      ...authComputed,
      ...notificationsComputed,
    },
    methods: {
      ...notificationsMethods,
      ...searchMethods,
    },

    This prevents verbose and duplicated mapXXX code in components, replaced by a nice list of the global concerns this component cares about. But the benefit really comes in when adding new state/getters/actions or when refactoring a Vuex module. For example, if I add a new userLoggedIn getter to authComputed, it will automatically be available in any component using authComputed. Or, if I rename some state or split it out into additional modules, the state helpers file allows the refactor to be done in two steps: first on the Vuex side, while updating the state helpers for compatibility, then in my components (if I actually want the public interface to change as well).

For these reasons, I feel like the mapXXX helpers should stay and the scoped slot pattern should actually be avoided. What are others' thoughts? Is there something I've missed?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions