]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
chore: notes about patch
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 4 Aug 2021 13:34:15 +0000 (15:34 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 4 Aug 2021 13:34:15 +0000 (15:34 +0200)
__tests__/store.patch.spec.ts
playground/src/stores/jokes.ts

index e4492e24a68858c63d4b860c3020c91c931d4089..707d28babc44f716528b150a14eca11ef322a556 100644 (file)
@@ -54,9 +54,13 @@ describe('store.$patch', () => {
 
   it('can patch an item that has been copied to an array', () => {
     const store = useArrayStore()
-    store.$patch({ currentItem: { id: 2 } })
+    store.$state.currentItem = { id: 2 }
+    // NOTE: a patch of an object is always recursive, writing in the object, in
+    // place.
+    //store.$patch({ currentItem: { id: 2 } })
     store.items.push(store.currentItem)
-    store.$patch({ currentItem: { id: 3 } })
+    // store.$patch({ currentItem: { id: 3 } })
+    store.$state.currentItem = { id: 3 }
 
     expect(store.$state.items).toEqual([{ id: 0 }, { id: 2 }])
     expect(store.items).toEqual([{ id: 0 }, { id: 2 }])
index f249a5f7be42155fe82c29ae47215b39ffcde494..340d7f50ef99dd435edfba50ceb289be4bae3e9f 100644 (file)
@@ -18,8 +18,9 @@ export const useJokes = defineStore('jokes', {
         this.jokes.push(this.current)
       }
 
-      this.$patch({ current: await getRandomJoke() })
-      // this.current = await getRandomJoke()
+      // NOTE: Avoid patching an object because it's recursive
+      // this.$patch({ current: await getRandomJoke() })
+      this.current = await getRandomJoke()
     },
   },
 })