'What is the correct way of setting state on a Vue 3 Pinia store?

When initializing state on a Vue 3 (composition API) Pinia store which pattern is more "correct" or idiomatic?

Option 1:

  state: () => ({
    user: {},
  }),

Option 2:

  state: () => {
    return {
      user: {},
    };
  },

Option 3: Maybe something else?



Solution 1:[1]

They are the same. Option 1 and 2 are functions that returns an object. In arrow functions, the { stands for the content of the function (like function x () {). So if you want to return an object (like return {), you would use ({ as option 1 does.

Reference: Arrow functions advanced syntax

Solution 2:[2]

Option 2. It should be a functions that returns the state. https://pinia.vuejs.org/core-concepts/state.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bernardo Dal Corno
Solution 2 Eze Kohon