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

Feature Request: Private State #1520

Open
jcupps opened this Issue Mar 8, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@jcupps
Copy link

jcupps commented Mar 8, 2019

What problem does this feature solve?

I would like the ability to make certain state private to the store, i.e. accessible within store getters, actions, and mutations, but not directly accessible by consumers (e.g. components). This would prevent unintended reading or manipulation of state which should be done through getters or actions/mutations respectively. As far as I know, there is no good way to achieve this today in Vuex.

What does the proposed API look like?

The API could look something like this:

A store:

{
  state() {
    return {
      somethingPublic: ''
    };
  },

  privateState() {
    return {
      somethingPrivate: []
    };
  },

  getters: {
    filtered: (state, getters, privateState) => privateState.somethingPrivate.filter(...)
  }
}

In a component:

// works:
this.$store.state.somethingPublic
this.$store.getters.filtered
// doesn't work:
this.$store.state.somethingPrivate
this.$store.privateState.somethingPrivate

Thanks.

@Jinjiang

This comment has been minimized.

Copy link
Member

Jinjiang commented Mar 10, 2019

Don't think it's necessary. And it also increases the complexity of the lib which would probably also make users more confused. Maybe ES private field would help in the future but I am not sure about this.
Thanks.

@jcupps

This comment has been minimized.

Copy link
Author

jcupps commented Mar 11, 2019

I agree that it would increase the complexity, but I haven't been able to come up with a good workaround. Are there any you could recommend?

@Jinjiang

This comment has been minimized.

Copy link
Member

Jinjiang commented Mar 11, 2019

How about strict mode https://vuex.vuejs.org/guide/strict.html? Or put the data you would expose into a module. Or just by giving some conventions with all your colleagues. Btw this question could also be asked in forum and StackOverflow.

@jcupps

This comment has been minimized.

Copy link
Author

jcupps commented Mar 11, 2019

Thanks for the suggestions; I had considered those, although unfortunately none would solve the problem. I asked the question here because I assumed that the assertion that it isn't necessary implied that good alternatives already exist. I don't think I would agree that private state in general isn't necessary, but I can understand not prioritizing it over other enhancements. I'll continue to look for a workaround. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.