]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: add store destructure guide (#621)
authorTomatoGuy0502 <H34066131@gs.ncku.edu.tw>
Wed, 25 Aug 2021 08:40:26 +0000 (16:40 +0800)
committerGitHub <noreply@github.com>
Wed, 25 Aug 2021 08:40:26 +0000 (10:40 +0200)
* docs: add store destructure guide

* Apply suggestions from code review

Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com>
packages/docs/core-concepts/index.md

index 5df92276facddc71864db74f1409300fcc52176a..879878410c9a9562d22da869f04fbd18cd5873f9 100644 (file)
@@ -63,3 +63,23 @@ export default defineComponent({
   },
 })
 ```
+
+In order to extract properties from the store while keeping its reactivity, you need to use `storeToRefs()`. It will create refs for any reactive property. This is useful when you are only using state from the store but not calling any action:
+```js
+import { storeToRefs } from 'pinia'
+
+export default defineComponent({
+  setup() {
+    const store = useStore()
+    // `name` and `doubleCount` are reactive refs
+    // This will also create refs for properties added by plugins
+    // but skip any action or non reactive (non ref/reactive) property
+    const { name, doubleCount } = storeToRefs(store)
+
+    return {
+      name,
+      doubleCount
+    }
+  },
+})
+```