Skip to content

Auto-creating getters & actions #1762

@evgeniyPP

Description

@evgeniyPP

What problem does this feature solve?

A popular practice is to create getters for each value in the state, and actions for each mutation. This eliminates the confusion in the components. Now you can use only mapGetters for properties, and mapActions & dispatch for methods.

However, this makes the store very verbose. You need to create useless getters and actions that simply return a value from a state/commit a mutation.

It would be convenient if Vuex, in case it does not find a suitable getter, looked for a value in the state and returned it. Or, if it doesn’t find action with the given name, he finds a mutation with that name and commits it.

In this case, it would be possible to use only mapGetters and mapActions in the components and at the same time not to write extra code in the store.

I know that this is probably not solving a problem that cannot be easily dealt with using existing APIs. But IMHO that would be a great improvement to the user experience, and I wanted to share this idea with the development team.

What does the proposed API look like?

store.js

export default new Vuex.Store({
    state: {
        count: 0
    },
    mutations: {
        increment (state) {
            state.count;
        }
    }
});

component.vue

<template>
  <div>
    <p>{{ count }}</p>
    <button @click="increment"> </button>
  </div>
</template>

<script>
import { mapGetters, mapActions } from "vuex";

export default ({
    computed: mapGetters(['count']),
    methods: mapActions(['increment'])
});
</script>

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