Skip to content

Commit

Permalink
docs: note about useStores within actions
Browse files Browse the repository at this point in the history
See #2004
  • Loading branch information
posva authored Feb 16, 2023
1 parent 1ea9eae commit dab114b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/docs/cookbook/composing-stores.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,29 @@ export const useCartStore = defineStore('cart', {
},
})
```

Since actions can be asynchronous, make sure **all of your `useStore()` calls appear before any `await`**. Otherwise, this could lead to using the wrong pinia instance _in SSR apps_:

```js{7-8,11-13}
import { defineStore } from 'pinia'
import { useUserStore } from './user'
export const useCartStore = defineStore('cart', {
actions: {
async orderCart() {
// ✅ call at the top of the action before any `await`
const user = useUserStore()
try {
await apiOrderCart(user.token, this.items)
// ❌ called after an `await` statement
const otherStore = useOtherStore()
// another action
this.emptyCart()
} catch (err) {
displayError(err)
}
},
},
})
```

0 comments on commit dab114b

Please sign in to comment.