]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
fix(vue2): use toRefs in storeToRefs
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 31 Mar 2022 09:03:27 +0000 (11:03 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 31 Mar 2022 09:03:27 +0000 (11:03 +0200)
Close #852

packages/pinia/src/storeToRefs.ts

index d0ba5a4fc07611dc6868f95e2fd61cb7ff887cee..fc294d31f94c3ae8e73e6241f58cae58f04a8297 100644 (file)
@@ -23,30 +23,29 @@ export function storeToRefs<SS extends StoreGeneric>(
 ): ToRefs<
   StoreState<SS> & StoreGetters<SS> & PiniaCustomStateProperties<StoreState<SS>>
 > {
-  if (__DEV__ && isVue2) {
-    console.warn(
-      '[Pinia]: "storeToRefs()" currently only works on Vue 3 until https://github.com/vuejs/pinia/issues/852 is fixed. Please, use "toRefs()" (from vue) instead. This will FAIL in production if not changed.'
-    )
+  // See https://github.com/vuejs/pinia/issues/852
+  // It's easier to just use toRefs() even if it includes more stuff
+  if (isVue2) {
     // @ts-expect-error: toRefs include methods and others
     return toRefs(store)
-  }
-
-  store = toRaw(store)
+  } else {
+    store = toRaw(store)
 
-  const refs = {} as ToRefs<
-    StoreState<SS> &
-      StoreGetters<SS> &
-      PiniaCustomStateProperties<StoreState<SS>>
-  >
-  for (const key in store) {
-    const value = store[key]
-    if (isRef(value) || isReactive(value)) {
-      // @ts-expect-error: the key is state or getter
-      refs[key] =
-        // ---
-        toRef(store, key)
+    const refs = {} as ToRefs<
+      StoreState<SS> &
+        StoreGetters<SS> &
+        PiniaCustomStateProperties<StoreState<SS>>
+    >
+    for (const key in store) {
+      const value = store[key]
+      if (isRef(value) || isReactive(value)) {
+        // @ts-expect-error: the key is state or getter
+        refs[key] =
+          // ---
+          toRef(store, key)
+      }
     }
-  }
 
-  return refs
+    return refs
+  }
 }