]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: updates
authorEduardo San Martin Morote <posva13@gmail.com>
Mon, 22 Mar 2021 22:47:02 +0000 (23:47 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Mon, 22 Mar 2021 22:48:21 +0000 (23:48 +0100)
docs/.vitepress/config.js
docs/core-concepts/actions.md
docs/core-concepts/getters.md
docs/core-concepts/index.md
docs/core-concepts/state.md
docs/getting-started.md
docs/introduction.md
docs/server-side-rendering.md [moved from docs/ssr.md with 98% similarity]

index 3b094bdb07e142de0a9014536bad2c1049a017f2..f0b2cacd8d67f55809f813f16e32060dc102eabe 100644 (file)
@@ -162,7 +162,7 @@ module.exports = {
         },
         {
           text: 'Server-Side Rendering (SSR)',
-          link: '/ssr.html',
+          link: '/server-side-rendering.html',
         },
         {
           text: 'Cookbook',
index c8e7f3be2506d59f7fe9b4e32881cb8184f8a581..651a4ee987249267d8ad85f3476abe64954a0c04 100644 (file)
@@ -1,6 +1,6 @@
 # Actions
 
-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 it's a perfect place to define business logic:
+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({
@@ -16,7 +16,7 @@ export const useStore = defineStore({
 })
 ```
 
-Like [getters](./getters.md), actions get access to the _whole store instance_ through `this` with full typing (and autocompletion ✨) support.
+Like [getters](./getters.md), actions get access to the _whole store instance_ through `this` with **full typing (and autocompletion ✨) support**.
 
 Actions are invoked like methods:
 
index 46e14af7bf27a8ecea64151f7e3335d3b4edecb6..e11cef2ef0f01b2a9283abdda49583f2b868f7da 100644 (file)
@@ -16,7 +16,7 @@ export const useStore = defineStore({
 })
 ```
 
-Like [actions](./actions.md), getters get access to the _whole store instance_ through `this` with full typing (and autocompletion ✨) support.
+Like [actions](./actions.md), getters get access to the _whole store instance_ through `this` with **full typing (and autocompletion ✨) support**.
 
 Then you can access the getter directly on the store instance:
 
@@ -60,7 +60,11 @@ export const useStore = defineStore({
 
 ## Accessing other stores
 
+To access a different store, you can directly _use_ the other store inside of a `getter`
+
 ```js
+import { useOtherStore } from './other-store'
+
 export const useStore = defineStore({
   id: 'main',
   state: () => ({
index 1121add776507958d1384c3a952b5473ac30753d..4f07514980b6210deef7f5e19bda73c36e499912 100644 (file)
@@ -12,13 +12,13 @@ export useStore = defineStore({
 })
 ```
 
-The `id` is necessary and is used by `pinia` to connect the store to the devtools.
+The `id` is necessary and is used by `pinia` to connect the store to the devtools. Naming the returned function _use..._ is a convention across composables to make its usage idiomatic.
 
 ## Using the store
 
-We are _defining_ a store because the store won't be created until `useStore()` is called inside of `setup()`. You can define as many stores as you want and you should define each store in a different file to automatically allow your bundle to code split.
+We are _defining_ a store because the store won't be created until `useStore()` is called inside of `setup()`. 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).
 
-Avoid calling `useStore()` outside of a `setup()` function:
+It's not possible to call `useStore()` outside of a `setup()` function. Here is an example using a store inside of a navigation guard with Vue Router:
 
 ```js
 import { createRouter } from 'vue-router'
@@ -35,7 +35,7 @@ router.beforeEach((to, from, next) => {
 })
 ```
 
-Instead, call `useStore()` inside functions that are called after `app.use(pinia)`:
+Instead, make sure to call `useStore()` inside functions that are called after your application is mounted (`app.mount()` or `new Vue()`):
 
 ```js
 router.beforeEach((to) => {
@@ -47,36 +47,32 @@ router.beforeEach((to) => {
 ```
 
 :::tip
-When dealing with Server Side Rendering, you will have to pass the `pinia` instance to `useStore()`. Read more about this in the [SSR guide](./server-side-rendering.md).
+When dealing with Server Side Rendering, you will have to pass the `pinia` instance to `useStore()`. Read more about this in the [SSR guide](/server-side-rendering.md).
 :::
 
-Once the store is instantiated, you can access any property defined in `state` and `getters` directly on the store, similar to `data` and `computed` properties in a vue component.
-
-```js
-// inside a component
-export default defineComponent({
-  setup() {
-    const store = useStore()
-    const text = store.name // "eduardo"
-    store.doubleCount // 2
-
-    return {
-      text, // will always be "eduardo"
-      doubleValue: computed(() => store.doubleValue), // reactive value
-    }
-  },
-})
-```
+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.
 
 `store` in an object wrapped with `reactive`, meaning there is no need to write `.value` after getters but, like `props` in `setup`, we cannot destructure it:
 
 ```ts
 export default defineComponent({
   setup() {
+    const store = useStore()
     // ❌ This won't work because it breaks reactivity
     // it's the same as destructuring from `props`
-    const { name, doubleCount } = useStore()
-    return { name, doubleCount }
+    const { name, doubleCount } = store
+
+    name // "eduardo"
+    doubleCount // 2
+
+    return {
+      // will always be "eduardo"
+      name,
+      // will always be 2
+      doubleCount
+      // this one will be reactive
+      doubleValue: computed(() => store.doubleCount),
+      }
   },
 })
 ```
index 0977082a21e2accf748cd811257a9d43d653bcfc..af650421489234897017ffcd4cf9f0effd3cb7fc 100644 (file)
@@ -1,12 +1,13 @@
 # State
 
-The state is, most of the time, the central part of your store. People often start by defining the state that represents their app. In Pinia the state is defined as a function that returns the initial state. \_This allows Pinia to work in both Server and Client Side.
+The state is, most of the time, the central part of your store. People often start by defining the state that represents their app. In Pinia the state is defined as a function that returns the initial state. This allows Pinia to work in both Server and Client Side.
 
 ```js
 import { defineStore } from 'pinia'
 
 const useStore = defineStore({
   id: 'storeName',
+  // can also be defined with an arrow function if you prefer that syntax
   state() {
     return {
       // all these properties will have their type inferred automatically
@@ -20,26 +21,32 @@ const useStore = defineStore({
 
 ## Accessing the `state`
 
-You can directly read and write to the state by accessing it through the `store` instance:
+By default, you can directly read and write to the state by accessing it through the `store` instance:
 
 ```js
+const store = useStore()
+
 store.counter++
 ```
 
+<!-- TODO: disable this with `strictMode` -->
+
 or call the method `$patch` that allows you apply multiple changes at the same time with a partial `state` object:
 
 ```ts
 store.$patch({
-  counter: -1,
+  counter: store.counter + 1,
   name: 'Abalam',
 })
 ```
 
-The main difference here is that `$patch` allows you to group multiple changes into one single entry in the devtools.
+<!-- TODO: disable this with `strictMode`, `{ noDirectPatch: true }` -->
+
+The main difference here is that `$patch()` allows you to group multiple changes into one single entry in the devtools. Note **both, direct changes to `state` and `$patch()` appear in the devtools**.
 
 ## Replacing the `state`
 
-Simply set your store `$stet` property to a new object:
+You can replace the whole state of a store by setting its `$state` property to a new object:
 
 ```ts
 store.$state = { counter: 666, name: 'Paimon' }
index ac8b664de8efcb8b7bfcf541d817112bdbd9bfbb..5043df268a2f7415bd97a846fead666acaafba1d 100644 (file)
@@ -9,7 +9,7 @@ npm install pinia@next
 ```
 
 :::tip
-Install Pinia v2 for Vue 3 `pinia@next` and Pinia v1 `pinia@latest` for Vue 2.
+`pinia@next` install Pinia v2 for Vue 3. If your app is using Vue 2, you need to install Pinia v1: `pinia@latest`.
 :::
 
 Create a pinia (the root store) and pass it to app:
@@ -20,7 +20,7 @@ import { createPinia } from 'pinia'
 app.use(createPinia())
 ```
 
-If you are using Vue 2, do:
+If you are using Vue 2, you also need to install a plugin and inject the created `pinia` at the root of the app:
 
 ```js
 import { createPinia, PiniaPlugin } from 'pinia'
@@ -31,6 +31,9 @@ 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,
 })
 ```
@@ -39,7 +42,7 @@ This will also add devtools support. In Vue 3, some features like time traveling
 
 ## What is a Store?
 
-A Store (like Pinia) is an entity holding state and business logic that isn't bound to your Component tree. In other words, **it hosts global state**. It's a bit like a component that is always there and that everybody can read off and write to. It has **three concepts**, the [state](./state.md), [getters](./getters.md) and [actions](./actions.md).
+A Store (like Pinia) is an entity holding state and business logic that isn't bound to your Component tree. In other words, **it hosts global state**. It's a bit like a component that is always there and that everybody can read off and write to. It has **three concepts**, the [state](./core-concepts/state.md), [getters](./core-concepts/getters.md) and [actions](./core-concepts/actions.md) and it's safe to assume these concepts are the equivalent of `data`, `computed` and `methods` in components.
 
 ## When should I use a Store
 
index 1d80051f386aaa8393c6789c50aff9bfba067d85..f8d16a43d71c12fdf77e2ce8ad2fa7d662c46693 100644 (file)
@@ -11,7 +11,11 @@ import { defineStore } from 'pinia'
 
 export const useCounter = defineStore({
   id: 'counter',
-  state: () => ({ count: 0 }),
+  state() {
+    return { count: 0 }
+  },
+  // could also be defined as
+  // state: () => ({ count: 0 })
 })
 ```
 
@@ -29,13 +33,15 @@ export default {
 }
 ```
 
+<!-- TODO: or if you prefer using the options api, use `mapStores` -->
+
 ## Why _Pinia_
 
 Pinia (pronounced like `/peenya/` in English) is is the closest word to _piña_ (_pineapple_ in Spanish) that is a valid package name. A pineapple is in reality a group of individual flowers that join together to create a multiple fruit. Similar to stores, each one is born individually, but they are all connected at the end. It's also a delicious tropical fruit indigenous to South America.
 
 ## A more realistic example
 
-Here is a more complete example of the API you will be using with Pinia **with types** even in JavaScript:
+Here is a more complete example of the API you will be using with Pinia **with types even in JavaScript**. For some people this might be enough to get started without reading further but we still recommend checking the rest of the documentation or even skipping this example and coming back once you have read about all of the _Core Concepts_.
 
 ```js
 import { defineStore } from 'pinia'
similarity index 98%
rename from docs/ssr.md
rename to docs/server-side-rendering.md
index 36c2c5d949be5498f16686397b557139aec1f63a..ca2c3c4c3ded732327d4b3f3cc58fdd0ab08610b 100644 (file)
@@ -1,4 +1,4 @@
-# Server Side Rendering
+# Server Side Rendering (SSR)
 
 Creating stores with Pinia should work out of the box for SSR as long as you call your `useStore()` functions at the top of `setup` functions, `getters` and `actions`: