You can call `useOtherStore()` at the top of any getter an action:
-```ts
+```js
import { useUserStore } from './user'
export const cartStore = defineStore({
If you need to compute a value based on the `state` and/or `getters` of multiple stores, you may be able to import all the stores but one into the remaining store, but depending on how your stores are used across your application, **this would hurt your code splitting** because importing the store that imports all others stores, would result in **one single big chunk** with all of your stores.
To prevent this, **we follow the rule above** and we create a new file with a new store:
-```ts
+```js
import { defineStore } from 'pinia'
import { useUserStore } from './user'
import { useCartStore } from './cart'
When an actions needs to use multiple stores, we do the same, we create a new file with a new store:
-```ts
+```js
import { defineStore } from 'pinia'
import { useUserStore } from './user'
import { useCartStore } from './cart'
Actions are invoked like methods:
-```ts
+```js
export default defineComponent({
setup() {
const main = useMainStore()
// useStore could be anything like useUser, useCart
export const useStore = defineStore({
// unique id of the store across your application
- id: 'storeName'
+ id: 'storeName',
})
```
`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
+```js
export default defineComponent({
setup() {
const store = useStore()
or call the method `$patch` that allows you apply multiple changes at the same time with a partial `state` object:
-```ts
+```js
store.$patch({
counter: store.counter + 1,
name: 'Abalam',
You can replace the whole state of a store by setting its `$state` property to a new object:
-```ts
+```js
store.$state = { counter: 666, name: 'Paimon' }
```
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`:
-```ts
+```js
export default defineComponent({
setup() {
// this works because pinia knows what application is running inside of
If you need to use the store somewhere else, you need to pass the `pinia` instance [that was passed to the app](#install-the-plugin) to the `useStore()` function call:
-```ts
+```js
const pinia = createPinia()
const app = createApp(App)
If you are using TypeScript or have a `jsconfig.json`, you should also add the types for `context.pinia`:
-```js
+```json
{
"types": [
// ...