Skip to content

fix(keepAlive): do not invoke onVnodeBeforeUnmount if is KeepAlive component #1079

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

Merged
merged 3 commits into from
Apr 30, 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
96 changes: 95 additions & 1 deletion packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ import {
KeepAlive,
serializeInner,
nextTick,
ComponentOptions
ComponentOptions,
markRaw,
inject,
defineComponent,
ComponentPublicInstance,
Ref,
cloneVNode,
provide
} from '@vue/runtime-test'
import { KeepAliveProps } from '../../src/components/KeepAlive'

Expand Down Expand Up @@ -559,4 +566,91 @@ describe('KeepAlive', () => {
expect(serializeInner(root)).toBe(`1`)
})
})

it('should not call onVnodeUnmounted', async () => {
const Foo = markRaw({
name: 'Foo',
render() {
return h('Foo')
}
})
const Bar = markRaw({
name: 'Bar',
render() {
return h('Bar')
}
})

const spyMounted = jest.fn()
const spyUnmounted = jest.fn()

const RouterView = defineComponent({
setup(_, { slots }) {
const Component = inject<Ref<ComponentPublicInstance>>('component')
const refView = ref()

let componentProps = {
ref: refView,
onVnodeMounted() {
spyMounted()
},
onVnodeUnmounted() {
spyUnmounted()
}
}

return () => {
const child: any = slots.default!({
Component: Component!.value
})[0]

const innerChild = child.children[0]
child.children[0] = cloneVNode(innerChild, componentProps)
return child
}
}
})

let toggle: () => void = () => {}

const App = defineComponent({
setup() {
const component = ref(Foo)

provide('component', component)

toggle = () => {
component.value = component.value === Foo ? Bar : Foo
}
return {
component,
toggle
}
},
render() {
return h(RouterView, null, {
default: ({ Component }: any) => h(KeepAlive, null, [h(Component)])
})
}
})

render(h(App), root)
await nextTick()
expect(spyMounted).toHaveBeenCalledTimes(1)
expect(spyUnmounted).toHaveBeenCalledTimes(0)

toggle()
await nextTick()

expect(spyMounted).toHaveBeenCalledTimes(2)
expect(spyUnmounted).toHaveBeenCalledTimes(0)

toggle()
await nextTick()
render(null, root)
await nextTick()

expect(spyMounted).toHaveBeenCalledTimes(2)
expect(spyUnmounted).toHaveBeenCalledTimes(2)
})
})
10 changes: 7 additions & 3 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1705,19 +1705,20 @@ function baseCreateRenderer(
) => {
const { props, ref, children, dynamicChildren, shapeFlag, dirs } = vnode
const shouldInvokeDirs = shapeFlag & ShapeFlags.ELEMENT && dirs
const shouldKeepAlive = shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE
let vnodeHook: VNodeHook | undefined | null

// unset ref
if (ref != null && parentComponent) {
setRef(ref, null, parentComponent, null)
}

if ((vnodeHook = props && props.onVnodeBeforeUnmount)) {
if ((vnodeHook = props && props.onVnodeBeforeUnmount) && !shouldKeepAlive) {
invokeVNodeHook(vnodeHook, parentComponent, vnode)
}

if (shapeFlag & ShapeFlags.COMPONENT) {
if (shapeFlag & ShapeFlags.COMPONENT_SHOULD_KEEP_ALIVE) {
if (shouldKeepAlive) {
;(parentComponent!.ctx as KeepAliveContext).deactivate(vnode)
} else {
unmountComponent(vnode.component!, parentSuspense, doRemove)
Expand Down Expand Up @@ -1749,7 +1750,10 @@ function baseCreateRenderer(
}
}

if ((vnodeHook = props && props.onVnodeUnmounted) || shouldInvokeDirs) {
if (
((vnodeHook = props && props.onVnodeUnmounted) || shouldInvokeDirs) &&
!shouldKeepAlive
) {
queuePostRenderEffect(() => {
vnodeHook && invokeVNodeHook(vnodeHook, parentComponent, vnode)
shouldInvokeDirs &&
Expand Down