From da0db98cdd4e4360bacd58e5cd26e98dd9716ae5 Mon Sep 17 00:00:00 2001 From: Bashu Naimi-Roy Date: Sun, 24 Jul 2022 15:23:06 -0400 Subject: [PATCH] docs: clarify composing-stores.md, remove duplicate example (#1467) [skip ci] --- packages/docs/cookbook/composing-stores.md | 31 +++------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/packages/docs/cookbook/composing-stores.md b/packages/docs/cookbook/composing-stores.md index 68d0a97e..8de80560 100644 --- a/packages/docs/cookbook/composing-stores.md +++ b/packages/docs/cookbook/composing-stores.md @@ -1,6 +1,6 @@ # Composing Stores -Composing stores is about having stores that use each other and there is one rule to follow: +Composing stores is about having stores that use each other, and this is supported in Pinia. There is one rule to follow: If **two or more stores use each other**, they cannot create an infinite loop through _getters_ or _actions_. They cannot **both** directly read each other state in their setup function: @@ -40,34 +40,9 @@ const useY = defineStore('y', () => { }) ``` -## Nested stores +## Nested Stores -Note that if one store uses another store, **there is no need to create a new store in a separate file**, you can directly import it. Think of it as nesting. - -You can call `useOtherStore()` at the top of any getter or action: - -```js -import { useUserStore } from './user' - -export const cartStore = defineStore('cart', { - getters: { - // ... other getters - summary(state) { - const user = useUserStore() - - return `Hi ${user.name}, you have ${state.list.length} items in your cart. It costs ${state.price}.` - }, - }, - - actions: { - purchase() { - const user = useUserStore() - - return apiPurchase(user.id, this.list) - }, - }, -}) -``` +Note that if one store uses another store, you can directly import and call the `useStore()` function within _actions_ and _getters_. Then you can interact with the store just like you would from within a Vue component. See [Shared Getters](#shared-getters) and [Shared Actions](#shared-actions). ## Shared Getters -- 2.47.2