Skip to content

What's the advantages of using this library ? #84

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

Closed
asafyish opened this issue Feb 19, 2020 · 1 comment
Closed

What's the advantages of using this library ? #84

asafyish opened this issue Feb 19, 2020 · 1 comment

Comments

@asafyish
Copy link

Not trying to offend, trying to understand do we really need a state management library for Vue 3?
I mean, the example code in the first page:

import { createStore } from 'pinia'

export const useMainStore = createStore({
  // name of the store
  // it is used in devtools and allows restoring state
  id: 'main',
  // a function that returns a fresh state
  state: () => ({
    counter: 0,
    name: 'Eduardo',
  }),
  // optional getters
  getters: {
    doubleCount: (state, getters) => state.counter * 2,
    // use getters in other getters
    doubleCountPlusOne: (state, { doubleCount }) => doubleCount.value * 2,
  },
  // optional actions
  actions: {
    reset() {
      // `this` is the store instance
      this.state.counter = 0
    },
  },
})

Can be replaced with:

import { computed, reactive } from '@vue/composition-api';

const state = reactive({
    counter: 0,
    name: 'Eduardo',
});

// Getters / Computed
const doubleCount = computed(() => state.counter * 2);
const doubleCountPlusOne = computed(() => doubleCount * 2 + 1);

// Actions
const reset = () => state.counter = 0;

export { state, doubleCount, doubleCountPlusOne, reset };

and it will work mostly the same.
Are there any advantages for pinia over the example I attached ?

@posva
Copy link
Member

posva commented Feb 19, 2020

It's fine. Most are highlighted in the Readme but not compared with a vanilla approach, so I understand why it may not be clear

  • SSR support (your example doesn't work with concurrent requests)
  • devtools support
  • grouped information, structure, convention

Some things can still get even better

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

No branches or pull requests

2 participants