]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: use 2 arguments syntax
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 22 Jul 2021 13:15:30 +0000 (15:15 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 22 Jul 2021 13:16:35 +0000 (15:16 +0200)
README.md
docs/cookbook/composing-stores.md
docs/cookbook/options-api.md
docs/core-concepts/actions.md
docs/core-concepts/getters.md
docs/core-concepts/index.md
docs/core-concepts/plugins.md
docs/core-concepts/state.md
docs/introduction.md
docs/ssr/nuxt.md

index 8e0dd4e74dd4dfae084f3704e56f0225a5410ba3..f52c37bf105634dfeeb21cba8ba044c6f3a6fa22 100644 (file)
--- a/README.md
+++ b/README.md
@@ -130,10 +130,9 @@ You can create as many stores as you want, and they should each exist in differe
 ```ts
 import { defineStore } from 'pinia'
 
-export const useMainStore = defineStore({
-  // name of the store
-  // it is used in devtools and allows restoring state
-  id: 'main',
+// main is the name of the store. It is unique across your application
+// and will appear in devtools
+export const useMainStore = defineStore('main', {
   // a function that returns a fresh state
   state: () => ({
     counter: 0,
index 9aba33cb94627d4bbe3735be6b8bbc08f97b1108..892d49a484685ba34af5f31e8639405ae164a040 100644 (file)
@@ -11,8 +11,7 @@ You can call `useOtherStore()` at the top of any getter an action:
 ```js
 import { useUserStore } from './user'
 
-export const cartStore = defineStore({
-  id: 'cart',
+export const cartStore = defineStore('cart', {
   getters: {
     // ... other getters
     summary(state) {
@@ -42,8 +41,7 @@ import { defineStore } from 'pinia'
 import { useUserStore } from './user'
 import { useCartStore } from './cart'
 
-export const useSharedStore = defineStore({
-  id: 'shared',
+export const useSharedStore = defineStore('shared', {
   getters: {
     summary() {
       const user = useUserStore()
@@ -64,8 +62,7 @@ import { defineStore } from 'pinia'
 import { useUserStore } from './user'
 import { useCartStore } from './cart'
 
-export const useSharedStore = defineStore({
-  id: 'shared',
+export const useSharedStore = defineStore('shared', {
   actions: {
     async orderCart() {
       const user = useUserStore()
index 9929b8ce74237444522c78f7e78d0b7fd5556a36..28905d672ce805aeabf845df6ec354cdf4825027 100644 (file)
@@ -16,8 +16,12 @@ If you need to access pretty much everything from the store, it might be too muc
 import { mapStores } from 'pinia'
 
 // given two stores with the following ids
-const useUserStore = defineStore({ id: 'user' })
-const useCartStore = defineStore({ id: 'cart' })
+const useUserStore = defineStore('user', {
+  // ...
+})
+const useCartStore = defineStore('cart', {
+  // ...
+})
 
 export default {
   computed: {
index 116e07c5346ce74f3b519b810a286604668cc43e..ce59170fc606be874e7316683c2202fd5af98237 100644 (file)
@@ -3,8 +3,7 @@
 Actions are the equivalent of [methods](https://v3.vuejs.org/guide/data-methods.html#methods) in components. They can be defined with the `actions` property in `defineStore()` and **they are perfect to define business logic**:
 
 ```js
-export const useStore = defineStore({
-  id: 'main',
+export const useStore = defineStore('main', {
   state: () => ({
     counter: 0,
   }),
@@ -26,8 +25,7 @@ import { mande } from 'mande'
 
 const api = mande('/api/users')
 
-export const useUsers = defineStore({
-  id: 'users',
+export const useUsers = defineStore('users', {
   state: () => ({
     data: userData,
     // ...
@@ -71,8 +69,7 @@ To use another store, you can directly _use it_ inside of the _action_:
 ```js
 import { useAuthStore } from './auth-store'
 
-export const useSettingsStore = defineStore({
-  id: 'settings',
+export const useSettingsStore = defineStore('settings', {
   state: () => ({
     // ...
   }),
index 411d70f778d71b3536f71fb367a8a223c4a51528..2f3b46f4bfa042c2c4e94fed767e9e49bedd91a0 100644 (file)
@@ -3,8 +3,7 @@
 Getters are exactly the equivalent of [computed values](https://v3.vuejs.org/guide/reactivity-computed-watchers.html#computed-values) for the state of a Store. They can be defined with the `getters` property in `defineStore()`. They receive the `state` as the first parameter **to encourage** the usage of arrow function:
 
 ```js
-export const useStore = defineStore({
-  id: 'main',
+export const useStore = defineStore('main', {
   state: () => ({
     counter: 0,
   }),
@@ -17,8 +16,7 @@ export const useStore = defineStore({
 Most of the time, getters will only rely on the state, however, they might need to use other getters. Because of this, we can get access to the _whole store instance_ through `this` when defining a regular function **but it is necessary to define the type of the return type (in TypeScript)**. This is due to a known limitation in TypeScript and **doesn't affect getters defined with an arrow function nor getters not using `this`**:
 
 ```ts
-export const useStore = defineStore({
-  id: 'main',
+export const useStore = defineStore('main', {
   state: () => ({
     counter: 0,
   }),
@@ -59,8 +57,7 @@ export default {
 As with computed properties, you can combine multiple getters. Access any other getter via `this`. Even if you are not using TypeScript, you can hint your IDE for types with the [JSDoc](https://jsdoc.app/tags-returns.html):
 
 ```js
-export const useStore = defineStore({
-  id: 'main',
+export const useStore = defineStore('main', {
   state: () => ({
     counter: 0,
   }),
@@ -89,8 +86,7 @@ To use another store getters, you can directly _use it_ inside of the _action_:
 ```js
 import { useOtherStore } from './other-store'
 
-export const useStore = defineStore({
-  id: 'main',
+export const useStore = defineStore('main', {
   state: () => ({
     // ...
   }),
index 96e5855a4ea949029455bc3abff812a574012ff7..44b088059df193c947eb9e065164806f7082ea84 100644 (file)
@@ -6,9 +6,9 @@ Before diving into core concepts, we need to know that a store is defined using
 import { defineStore } from 'pinia'
 
 // useStore could be anything like useUser, useCart
-export const useStore = defineStore({
-  // unique id of the store across your application
-  id: 'storeId',
+// the first argument is a unique id of the store across your application
+export const useStore = defineStore('main', {
+  // other options...
 })
 ```
 
index 76ac628cc270360bbec5e19bc87d25175052c62b..58ad6121a38f8b3fb37b39949a7e2ebe4e952756 100644 (file)
@@ -192,9 +192,7 @@ The same is true for `store.$onAction()`.
 It is possible to create new options when defining stores to later on consume them from plugins. For example, you could create a `debounce` option that allows you to debounce any action:
 
 ```js
-defineStore({
-  id: 'search',
-  // ...
+defineStore('search', {
   actions: {
     searchContacts() {
       // ...
index a1af14c5bd0cd8f96ba4ad2bf356336b88caea9e..67bd7a0ddbf5df6ba823db7b082de36406e2454e 100644 (file)
@@ -5,8 +5,7 @@ The state is, most of the time, the central part of your store. People often sta
 ```js
 import { defineStore } from 'pinia'
 
-const useStore = defineStore({
-  id: 'storeId',
+const useStore = defineStore('storeId', {
   // can also be defined with an arrow function if you prefer that syntax
   state() {
     return {
@@ -167,7 +166,7 @@ Note that depending on when you create the watcher, `pinia.state.value.cart` mig
 ```ts
 import { defineStore } from 'pinia'
 
-const useCartStore = defineStore({
+const useCartStore = defineStore('cart', {
   // ...
 })
 
index 8cad4e8724ac07e9f8245d42360c25f0cdd5c09b..fbf22f98cffbe8f5f643e32138baaac34d4d0b5a 100644 (file)
@@ -10,8 +10,7 @@ You start by creating a store:
 // stores/counter.js
 import { defineStore } from 'pinia'
 
-export const useCounterStore = defineStore({
-  id: 'counter',
+export const useCounterStore = defineStore('counter', {
   state() {
     return { count: 0 }
   },
@@ -39,8 +38,7 @@ export default {
 If you are still not into `setup()` and Composition API, don't worry, Pinia also support a similar set of [_map helpers_ like Vuex](https://vuex.vuejs.org/guide/state.html#the-mapstate-helper). You define stores the same way but then use `mapStores()`, `mapState()`, or `mapActions()`:
 
 ```js
-const useCounterStore = defineStore({
-  id: 'counter',
+const useCounterStore = defineStore('counter', {
   state: () => ({ count: 0 }),
   getters: {
     double: (state) => state.count * 2,
@@ -52,7 +50,9 @@ const useCounterStore = defineStore({
   }
 })
 
-const useUserStore = defineStore({ id: 'user' })
+const useUserStore = defineStore('user', {
+  // ...
+})
 
 export default {
   computed: {
@@ -83,8 +83,7 @@ Here is a more complete example of the API you will be using with Pinia **with t
 ```js
 import { defineStore } from 'pinia'
 
-export const todos = defineStore({
-  id: 'todos',
+export const todos = defineStore('todos', {
   state: () => ({
     /** @type {{ text: string, id: number, isFinished: boolean }[]} */
     todos: [],
index 580ed7534122ac41a9d45e046c3cc2ed42305942..0e0772017dcdb29f24416b05bac1e05208c69aad 100644 (file)
@@ -45,9 +45,7 @@ export default {
 You can also use [the context](https://nuxtjs.org/docs/2.x/internals-glossary/context) in any store by using the injected property `$nuxt`:
 
 ```js
-defineStore({
-  id: 'cart',
-
+defineStore('cart', {
   actions: {
     purchase() {
       const user = useUserStore()