# Composing Stores
-Composing stores may look hard at first glance but there is only one rule to follow really:
+Composing stores is about having stores that use each other and there is one rule to follow:
-If **multiple stores use each other** or you need to use **multiple stores** at the same time, you must create a **separate file** where you import all of them.
+If **two or more stores use each other**, you must create a new store in a **separate file** where you import and use all of them.
-If one store uses an other store, there is no need to create a new file, you can directly import it. Think of it as nesting.
+Note that if one store uses an other 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 an action:
export const useSharedStore = defineStore({
id: 'shared',
- state: () => ({}),
actions: {
async orderCart() {
const user = useUserStore()
})
```
+## Nuxt.js
+
+Make sure to install [`@nuxtjs/composition-api`](https://composition-api.nuxtjs.org/):
+
+```bash
+yarn add pinia @nuxtjs/composition-api
+# or with npm
+npm install pinia @nuxtjs/composition-api
+```
+
+If you are using Nuxt, we supply a _module_ to handle everything for you, you only need to add it to `buildModules` in your `nuxt.config.js` file:
+
+```js
+// nuxt.config.js
+export default {
+ // ... other options
+ buildModules: ['@nuxtjs/composition-api', 'pinia/nuxt'],
+}
+```
+
+If you are using TypeScript or have a `jsconfig.json`, you should also add the types for `context.pinia`:
+
+```js
+{
+ "include": [
+ // ...
+ "pinia/nuxt/types"
+ ]
+}
+```
+
+## State hydration
+
To hydrate the initial state, you need to make sure the rootState is included somewhere in the HTML for Pinia to pick it up later on:
```js
escapeHTML(JSON.stringify(pinia.state.value))
```
-On client side, you must hydrate pinia's state before calling any `useStore()` function. For example, if we serialize the state into a `<script>` tag to make it accessible globally on client side through `window.__pinia`, we can write this:
+Depending on what you are using for SSR, you will set an _initial state_ variable that will be serialized in the HTML. Example with [vite-ssr](https://github.com/frandiox/vite-ssr):
+
+```js
+export default viteSSR(App, { routes }, ({ initialState }) => {
+ // ...
+ if (import.meta.env.SSR) {
+ initialState.pinia = JSON.stringify(pinia.state.value)
+ }
+})
+```
+
+And add a _module_ to load the state on the client. It should be picked up automatically by `vite-ssr`:
+
+```ts
+// modules/pinia.ts
+import { createPinia } from 'pinia'
+import { UserModule } from '~/types'
+
+export const install: UserModule = ({ isClient, router, app }) => {
+ const pinia = createPinia()
+ app.use(pinia)
+ router.pinia = pinia
+
+ if (isClient) {
+ console.log('setting')
+ // @ts-ignore
+ if (window.__INITIAL_STATE__?.pinia) {
+ // @ts-ignore
+ pinia.state.value = JSON.parse(window.__INITIAL_STATE__.pinia)
+ }
+ }
+
+ return pinia
+}
+```
+
+Adapt this strategy to your environment. Make sure to hydrate pinia's state before calling any `useStore()` function on client side. For example, if we serialize the state into a `<script>` tag to make it accessible globally on client side through `window.__pinia`, we can write this:
```js
const pinia = createPinia()