]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): Lifecycle hooks should support callbacks shared by reference ...
authorThorsten Lünborg <t.luenborg@googlemail.com>
Tue, 27 Sep 2022 02:18:22 +0000 (04:18 +0200)
committerGitHub <noreply@github.com>
Tue, 27 Sep 2022 02:18:22 +0000 (22:18 -0400)
fix #6686

packages/runtime-core/__tests__/apiLifecycle.spec.ts
packages/runtime-core/src/apiLifecycle.ts

index 4c27ada55c408e7099005b7c00bf06009e7f92d4..b7e8de30d55a3b2c5d2d48014e934245faa98c9d 100644 (file)
@@ -378,4 +378,27 @@ describe('api: lifecycle hooks', () => {
       newValue: 3
     })
   })
+
+  it('runs shared hook fn for each instance', async () => {
+    const fn = jest.fn()
+    const toggle = ref(true)
+    const Comp = {
+      setup() {
+        return () => (toggle.value ? [h(Child), h(Child)] : null)
+      }
+    }
+    const Child = {
+      setup() {
+        onMounted(fn)
+        onBeforeUnmount(fn)
+        return () => h('div')
+      }
+    }
+
+    render(h(Comp), nodeOps.createElement('div'))
+    expect(fn).toHaveBeenCalledTimes(2)
+    toggle.value = false
+    await nextTick()
+    expect(fn).toHaveBeenCalledTimes(4)
+  })
 })
index ec39308eea513886b726d6cfc8de775c7f4f6f68..f7d6d7007c1871574f5fa6ed8da1d6295c7697d9 100644 (file)
@@ -68,7 +68,7 @@ export const createHook =
   (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
     // post-create lifecycle registrations are noops during SSR (except for serverPrefetch)
     (!isInSSRComponentSetup || lifecycle === LifecycleHooks.SERVER_PREFETCH) &&
-    injectHook(lifecycle, hook, target)
+    injectHook(lifecycle, (...args: unknown[]) => hook(...args), target)
 
 export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
 export const onMounted = createHook(LifecycleHooks.MOUNTED)