Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(keep-alive): call activated hook of nested components.(#1742) #1743

Merged
merged 2 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,36 @@ describe('KeepAlive', () => {
assertHookCalls(two, [1, 1, 2, 2, 0])
})

// #1742
test('should call lifecycle hooks on nested components when root component no hooks', async () => {
const two = {
name: 'two',
data: () => ({ msg: 'two' }),
render(this: any) {
return h('div', this.msg)
},
activated: jest.fn()
}
const one = {
name: 'one',
data: () => ({ msg: 'one' }),
render(this: any) {
return h(two)
}
}

const toggle = ref(true)
const App = {
render() {
return h(KeepAlive, () => (toggle.value ? h(one) : null))
}
}
render(h(App), root)

expect(serializeInner(root)).toBe(`<div>two</div>`)
expect(two.activated).toHaveBeenCalledTimes(1)
})

test('should call correct hooks for nested keep-alive', async () => {
const toggle2 = ref(true)
one.render = () => h(KeepAlive, () => (toggle2.value ? h(two) : null))
Expand Down
5 changes: 4 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1264,7 +1264,7 @@ function baseCreateRenderer(
if (!instance.isMounted) {
let vnodeHook: VNodeHook | null | undefined
const { el, props } = initialVNode
const { bm, m, a, parent } = instance
const { bm, m, parent } = instance
if (__DEV__) {
startMeasure(instance, `render`)
}
Expand Down Expand Up @@ -1323,6 +1323,9 @@ function baseCreateRenderer(
}, parentSuspense)
}
// activated hook for keep-alive roots.
// #1742 activated hook must be accessed after first render
// since the hook may be injected by a child keep-alive
const { a } = instance
if (
a &&
initialVNode.shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
Expand Down