You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: packages/docs/core-concepts/plugins.md
+22-17
Original file line number
Diff line number
Diff line change
@@ -109,20 +109,25 @@ If you want to add new state properties to a store or properties that are meant
109
109
- On the `store` so you can access it with `store.myState`
110
110
- On `store.$state` so it can be used in devtools and, **be serialized during SSR**.
111
111
112
-
Note that this allows you to share a `ref`or `computed` property:
112
+
On top of that, you will certainly have to use a `ref()` (or other reactive API) in order to share the value across different accesses:
113
113
114
114
```js
115
-
constglobalSecret=ref('secret')
116
-
pinia.use(({ store }) => {
117
-
// `secret` is shared among all stores
118
-
store.$state.secret= globalSecret
119
-
store.secret= globalSecret
120
-
// it gets automatically unwrapped
121
-
store.secret// 'secret'
115
+
import { toRef, ref } from'vue'
122
116
123
-
consthasError=ref(false)
124
-
store.$state.hasError= hasError
125
-
// this one must always be set
117
+
pinia.use(({ store }) => {
118
+
// to correctly handle SSR, we need to make sure we are not overriding an
119
+
// existing value
120
+
if (!Object.prototype.hasOwnProperty(store.$state, 'hasError')) {
121
+
// hasError is defined within the plugin, so each store has their individual
122
+
// state property
123
+
consthasError=ref(false)
124
+
// setting the variable on `$state`, allows it be serialized during SSR
125
+
store.$state.hasError= hasError
126
+
}
127
+
// we need to transfer the ref from the state to the store, this way
128
+
// both accesses: store.hasError and store.$state.hasError will work
129
+
// and share the same variable
130
+
// See https://vuejs.org/api/reactivity-utilities.html#toref
126
131
store.hasError=toRef(store.$state, 'hasError')
127
132
128
133
// in this case it's better not to return `hasError` since it
@@ -137,19 +142,19 @@ Note that state changes or additions that occur within a plugin (that includes c
137
142
If you are using **Vue 2**, Pinia is subject to the [same reactivity caveats](https://vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats) as Vue. You will need to use `set` from `@vue/composition-api` when creating new state properties like `secret` and `hasError`:
138
143
139
144
```js
140
-
import { set } from'@vue/composition-api'
145
+
import { set, toRef } from'@vue/composition-api'
141
146
pinia.use(({ store }) => {
142
-
if (!store.$state.hasOwnProperty('hello')) {
147
+
if (!Object.prototype.hasOwnProperty(store.$state, 'hello')) {
143
148
constsecretRef=ref('secret')
144
149
// If the data is meant to be used during SSR, you should
145
150
// set it on the `$state` property so it is serialized and
146
151
// picked up during hydration
147
152
set(store.$state, 'secret', secretRef)
148
-
// set it directly on the store too so you can access it
149
-
// both ways: `store.$state.secret` / `store.secret`
150
-
set(store, 'secret', secretRef)
151
-
store.secret// 'secret'
152
153
}
154
+
// set it directly on the store too so you can access it
155
+
// both ways: `store.$state.secret` / `store.secret`
0 commit comments