Skip to content

Commit 5b61973

Browse files
committed
docs(ssr): use hasOwnProperty
1 parent 11b92fd commit 5b61973

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

packages/docs/core-concepts/plugins.md

+22-17
Original file line numberDiff line numberDiff line change
@@ -109,20 +109,25 @@ If you want to add new state properties to a store or properties that are meant
109109
- On the `store` so you can access it with `store.myState`
110110
- On `store.$state` so it can be used in devtools and, **be serialized during SSR**.
111111

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:
113113

114114
```js
115-
const globalSecret = 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'
122116

123-
const hasError = 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+
const hasError = 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
126131
store.hasError = toRef(store.$state, 'hasError')
127132

128133
// 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
137142
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`:
138143

139144
```js
140-
import { set } from '@vue/composition-api'
145+
import { set, toRef } from '@vue/composition-api'
141146
pinia.use(({ store }) => {
142-
if (!store.$state.hasOwnProperty('hello')) {
147+
if (!Object.prototype.hasOwnProperty(store.$state, 'hello')) {
143148
const secretRef = ref('secret')
144149
// If the data is meant to be used during SSR, you should
145150
// set it on the `$state` property so it is serialized and
146151
// picked up during hydration
147152
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'
152153
}
154+
// set it directly on the store too so you can access it
155+
// both ways: `store.$state.secret` / `store.secret`
156+
set(store, 'secret', toRef(store.$state, 'secret'))
157+
store.secret // 'secret'
153158
})
154159
```
155160

0 commit comments

Comments
 (0)