Skip to content

Commit

Permalink
fix(keep-alive): should update re-activated component with latest props
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Mar 24, 2020
1 parent bfae9b2 commit 1237387
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 12 deletions.
27 changes: 27 additions & 0 deletions packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,32 @@ describe('KeepAlive', () => {
await nextTick()
expect(Foo.unmounted).not.toHaveBeenCalled()
})

test('should update re-activated component if props have changed', async () => {
const Foo = (props: { n: number }) => props.n

const toggle = ref(true)
const n = ref(0)

const App = {
setup() {
return () =>
h(KeepAlive, () => (toggle.value ? h(Foo, { n: n.value }) : null))
}
}

render(h(App), root)
expect(serializeInner(root)).toBe(`0`)

toggle.value = false
await nextTick()
expect(serializeInner(root)).toBe(`<!---->`)

n.value++
await nextTick()
toggle.value = true
await nextTick()
expect(serializeInner(root)).toBe(`1`)
})
})
})
36 changes: 25 additions & 11 deletions packages/runtime-core/src/components/KeepAlive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export interface KeepAliveSink {
activate: (
vnode: VNode,
container: RendererElement,
anchor: RendererNode | null
anchor: RendererNode | null,
isSVG: boolean,
optimized: boolean
) => void
deactivate: (vnode: VNode) => void
}
Expand Down Expand Up @@ -78,6 +80,7 @@ const KeepAliveImpl = {
const sink = instance.sink as KeepAliveSink
const {
renderer: {
p: patch,
m: move,
um: _unmount,
o: { createElement }
Expand All @@ -86,13 +89,24 @@ const KeepAliveImpl = {
} = sink
const storageContainer = createElement('div')

sink.activate = (vnode, container, anchor) => {
sink.activate = (vnode, container, anchor, isSVG, optimized) => {
const child = vnode.component!
move(vnode, container, anchor, MoveType.ENTER, parentSuspense)
// in case props have changed
patch(
child.vnode,
vnode,
container,
anchor,
instance,
parentSuspense,
isSVG,
optimized
)
queuePostRenderEffect(() => {
const component = vnode.component!
component.isDeactivated = false
if (component.a) {
invokeHooks(component.a)
child.isDeactivated = false
if (child.a) {
invokeHooks(child.a)
}
}, parentSuspense)
}
Expand Down Expand Up @@ -181,19 +195,19 @@ const KeepAliveImpl = {
}

const key = vnode.key == null ? comp : vnode.key
const cached = cache.get(key)
const cachedVNode = cache.get(key)

// clone vnode if it's reused because we are going to mutate it
if (vnode.el) {
vnode = cloneVNode(vnode)
}
cache.set(key, vnode)

if (cached) {
if (cachedVNode) {
// copy over mounted state
vnode.el = cached.el
vnode.anchor = cached.anchor
vnode.component = cached.component
vnode.el = cachedVNode.el
vnode.anchor = cachedVNode.anchor
vnode.component = cachedVNode.component
if (vnode.transition) {
// recursively update transition hooks on subTree
setTransitionHooks(vnode, vnode.transition!)
Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,9 @@ function baseCreateRenderer(
;(parentComponent!.sink as KeepAliveSink).activate(
n2,
container,
anchor
anchor,
isSVG,
optimized
)
} else {
mountComponent(
Expand Down

0 comments on commit 1237387

Please sign in to comment.