]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: corrections and improvements (#1579)
authorJerald Vinfrank <46400789+JeraldVin@users.noreply.github.com>
Mon, 22 Aug 2022 09:48:56 +0000 (15:18 +0530)
committerGitHub <noreply@github.com>
Mon, 22 Aug 2022 09:48:56 +0000 (11:48 +0200)
Co-authored-by: Jerald Vinfrank <jerald.vinfrank@gdn-commerce.com>
Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
README.md
packages/docs/cookbook/composables.md
packages/docs/core-concepts/actions.md
packages/docs/core-concepts/getters.md
packages/docs/core-concepts/index.md
packages/docs/core-concepts/state.md

index a09581c5488341aeb6d6572c679b7176ed92a028..1a0c91cdbf9cb91f3d44273abe52e035e7207ee7 100644 (file)
--- a/README.md
+++ b/README.md
@@ -135,9 +135,33 @@ npm install pinia @vue/composition-api
 Create a pinia (the root store) and pass it to app:
 
 ```js
+// Vue 3
+import { createApp } from 'vue'
 import { createPinia } from 'pinia'
+import App from './App.vue'
 
-app.use(createPinia())
+const pinia = createPinia()
+const app = createApp(App)
+
+app.use(pinia)
+app.mount('#app')
+```
+
+```js
+// Vue 2
+import { createPinia, PiniaVuePlugin } from 'pinia'
+
+Vue.use(PiniaVuePlugin)
+const pinia = createPinia()
+
+new Vue({
+  el: '#app',
+  // other options...
+  // ...
+  // note the same `pinia` instance can be used across multiple Vue apps on
+  // the same page
+  pinia,
+})
 ```
 
 ### Create a Store
index 293b17d9253e82bad30a569e742a03a5301c2106..bca392dfa23ef2baef0664847a2706997aee9724 100644 (file)
@@ -86,7 +86,7 @@ In [Setup Stores](#setup-stores), you need to use a helper named `skipHydrate()`
 import { defineStore, skipHydrate } from 'pinia'
 import { useEyeDropper, useLocalStorage } from '@vueuse/core'
 
-const useColorStore = defineStore('colors', () => {
+export const useColorStore = defineStore('colors', () => {
   const { isSupported, open, sRGBHex } = useEyeDropper()
   const lastColor = useLocalStorage('lastColor', sRGBHex)
   // ...
index 5d92d5c004f0f356e1ab7efd701b84b8729b084c..a68ee33820ab4685cb508dfa9d1174cb4cfa9784 100644 (file)
@@ -8,11 +8,12 @@
 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('main', {
+export const useCounterStore = defineStore('counter', {
   state: () => ({
     count: 0,
   }),
   actions: {
+    // since we rely on `this`, we cannot use an arrow function
     increment() {
       this.count++
     },
@@ -58,9 +59,9 @@ Actions are invoked like methods:
 ```js
 export default defineComponent({
   setup() {
-    const main = useMainStore()
+    const store = useCounterStore()
     // call the action as a method of the store
-    main.randomizeCounter()
+    store.randomizeCounter()
 
     return {}
   },
@@ -99,7 +100,7 @@ You can directly call any action as a method of the store:
 ```js
 export default {
   setup() {
-    const store = useStore()
+    const store = useCounterStore()
 
     store.randomizeCounter()
   },
@@ -121,7 +122,7 @@ For the following examples, you can assume the following store was created:
 
 import { defineStore } from 'pinia',
 
-const useCounterStore = defineStore('counter', {
+export const useCounterStore = defineStore('counter', {
   state: () => ({
     count: 0
   }),
@@ -135,7 +136,7 @@ const useCounterStore = defineStore('counter', {
 
 ### With `setup()`
 
-While Composition API is not for everyone, the `setup()` hook can make using Pinia easier to work with in the Options API. No extra map helper functions needed!
+While Composition API is not for everyone, the `setup()` hook can make using Pinia easier to work within the Options API. No extra map helper functions needed!
 
 ```js
 import { useCounterStore } from '../stores/counter'
@@ -169,7 +170,7 @@ export default {
     // same as calling from store.increment()
     ...mapActions(useCounterStore, ['increment'])
     // same as above but registers it as this.myOwnName()
-    ...mapActions(useCounterStore, { myOwnName: 'doubleCount' }),
+    ...mapActions(useCounterStore, { myOwnName: 'increment' }),
   },
 }
 ```
@@ -217,14 +218,14 @@ const unsubscribe = someStore.$onAction(
 unsubscribe()
 ```
 
-By default, _action subscriptions_ are bound to the component where they are added (if the store is inside a component's `setup()`). Meaning, they will be automatically removed when the component is unmounted. If you want to keep them after the component is unmounted, pass `true` as the second argument to _detach_ the _action subscription_ from the current component:
+By default, _action subscriptions_ are bound to the component where they are added (if the store is inside a component's `setup()`). Meaning, they will be automatically removed when the component is unmounted. If you also want to keep them after the component is unmounted, pass `true` as the second argument to _detach_ the _action subscription_ from the current component:
 
 ```js
 export default {
   setup() {
     const someStore = useSomeStore()
 
-    // this subscription will be kept after the component is unmounted
+    // this subscription will be kept even after the component is unmounted
     someStore.$onAction(callback, true)
 
     // ...
index 6077f0f0ded18215a1a37daab2a628bc53f06f0b..6ecb9f3a9c8892c1b65808cb3acbf2a3cbd81580 100644 (file)
@@ -8,7 +8,7 @@
 Getters are exactly the equivalent of [computed values](https://vuejs.org/guide/essentials/computed.html) 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('main', {
+export const useCounterStore = defineStore('counter', {
   state: () => ({
     count: 0,
   }),
@@ -21,7 +21,7 @@ export const useStore = defineStore('main', {
 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('main', {
+export const useCounterStore = defineStore('counter', {
   state: () => ({
     count: 0,
   }),
@@ -49,7 +49,7 @@ Then you can access the getter directly on the store instance:
 <script>
 export default {
   setup() {
-    const store = useStore()
+    const store = useCounterStore()
 
     return { store }
   },
@@ -62,7 +62,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('main', {
+export const useCounterStore = defineStore('counter', {
   state: () => ({
     count: 0,
   }),
@@ -156,7 +156,7 @@ You can directly access any getter as a property of the store (exactly like stat
 ```js
 export default {
   setup() {
-    const store = useStore()
+    const store = useCounterStore()
 
     store.count = 3
     store.doubleCount // 6
index c8790f90fff08cc1ae42a78274c9845b24933e05..0db512f127850a18acc464f5062efa2c9a0456b8 100644 (file)
@@ -27,9 +27,9 @@ Similar to Vue's Options API, we can also pass an Options Object with `state`, `
 
 ```js {2-10}
 export const useCounterStore = defineStore('counter', {
-  state: () => ({ count: 0 }),
+  state: () => ({ count: 0, name: 'Eduardo' }),
   getters: {
-    double: (state) => state.count * 2,
+    doubleCount: (state) => state.count * 2,
   },
   actions: {
     increment() {
@@ -50,11 +50,13 @@ There is also another possible syntax to define stores. Similar to the Vue Compo
 ```js
 export const useCounterStore = defineStore('counter', () => {
   const count = ref(0)
+  const name = ref('Eduardo')
+  const doubleCount = computed(() => count.value * 2)
   function increment() {
     count.value++
   }
 
-  return { count, increment }
+  return { count, name, doubleCount, increment }
 })
 ```
 
@@ -72,7 +74,7 @@ As with [Vue's Composition API and Option API](https://vuejs.org/guide/introduct
 
 ## Using the store
 
-We are _defining_ a store because the store won't be created until `useStore()` is called inside of `setup()`:
+We are _defining_ a store because the store won't be created until `use...Store()` is called inside of `setup()`:
 
 ```js
 import { useCounterStore } from '@/stores/counter'
@@ -89,9 +91,11 @@ export default {
 }
 ```
 
-You can define as many stores as you want and **you should define each store in a different file** to get the most out of pinia (like automatically allow your bundle to code split and TypeScript inference).
-
+:::tip
 If you are not using `setup` components yet, [you can still use Pinia with _map helpers_](../cookbook/options-api.md).
+:::
+
+You can define as many stores as you want and **you should define each store in a different file** to get the most out of pinia (like automatically allow your bundler to code split and TypeScript inference).
 
 Once the store is instantiated, you can access any property defined in `state`, `getters`, and `actions` directly on the store. We will see these in detail in the next pages but autocompletion will help you.
 
@@ -105,15 +109,22 @@ export default defineComponent({
     // it's the same as destructuring from `props`
     const { name, doubleCount } = store
 
-    name // "eduardo"
-    doubleCount // 2
+    name // "Eduardo"
+    doubleCount // 0
+
+    setTimeout(() => {
+      store.increment()
+    }, 1000)
 
     return {
-      // will always be "eduardo"
+      // will always be "Eduardo"
       name,
-      // will always be 2
+      // will always be 0
       doubleCount,
-      // this one will be reactive
+      // will also always be 0
+      doubleNumber: store.doubleCount,
+
+      // ✅ this one will be reactive
       doubleValue: computed(() => store.doubleCount),
     }
   },
index 0531affb739408e04fa29c3fe845d02c94fff302..aacf86a49dcade77b06211d297267ce2d546cafe 100644 (file)
@@ -10,7 +10,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('storeId', {
+export const useStore = defineStore('storeId', {
   // arrow function recommended for full type inference
   state: () => {
     return {
@@ -32,7 +32,7 @@ If you are using Vue 2, the data you create in `state` follows the same rules as
 You don't need to do much in order to make your state compatible with TS: make sure [`strict`](https://www.typescriptlang.org/tsconfig#strict), or at the very least, [`noImplicitThis`](https://www.typescriptlang.org/tsconfig#noImplicitThis), are enabled and Pinia will infer the type of your state automatically! However, there are a few cases where you should give it a hand with some casting:
 
 ```ts
-const useStore = defineStore('storeId', {
+export const useUserStore = defineStore('user', {
   state: () => {
     return {
       // for initially empty lists
@@ -57,7 +57,7 @@ interface State {
   user: UserInfo | null
 }
 
-const useStore = defineStore('storeId', {
+export const useUserStore = defineStore('user', {
   state: (): State => {
     return {
       userList: [],
@@ -107,7 +107,7 @@ For the following examples, you can assume the following store was created:
 
 import { defineStore } from 'pinia'
 
-const useCounterStore = defineStore('counter', {
+export const useCounterStore = defineStore('counter', {
   state: () => ({
     count: 0,
   }),
@@ -227,14 +227,14 @@ cartStore.$subscribe((mutation, state) => {
 })
 ```
 
-By default, _state subscriptions_ are bound to the component where they are added (if the store is inside a component's `setup()`). Meaning, they will be automatically removed when the component is unmounted. If you want to keep them after the component is unmounted, pass `{ detached: true }` as the second argument to _detach_ the _state subscription_ from the current component:
+By default, _state subscriptions_ are bound to the component where they are added (if the store is inside a component's `setup()`). Meaning, they will be automatically removed when the component is unmounted. If you also want to keep them after the component is unmounted, pass `{ detached: true }` as the second argument to _detach_ the _state subscription_ from the current component:
 
 ```js
 export default {
   setup() {
     const someStore = useSomeStore()
 
-    // this subscription will be kept after the component is unmounted
+    // this subscription will be kept even after the component is unmounted
     someStore.$subscribe(callback, { detached: true })
 
     // ...