Skip to content

Commit

Permalink
fix(runtime-core): Lifecycle hooks should support callbacks shared by…
Browse files Browse the repository at this point in the history
… reference (#6687)

fix #6686
  • Loading branch information
LinusBorg authored Sep 27, 2022
1 parent 96ba71d commit c71a08e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/runtime-core/__tests__/apiLifecycle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
2 changes: 1 addition & 1 deletion packages/runtime-core/src/apiLifecycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c71a08e

Please sign in to comment.