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): should successfully set ref with async comp #5004

Closed
wants to merge 2 commits into from
Closed
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
39 changes: 39 additions & 0 deletions packages/runtime-core/__tests__/components/KeepAlive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -874,4 +874,43 @@ describe('KeepAlive', () => {
await nextTick()
expect(serializeInner(root)).toBe('<p>1</p>')
})

test('should correctly set ref with async component', async () => {
let resolve: (comp: Component) => void
const AsyncComp = defineAsyncComponent(
() =>
new Promise(r => {
resolve = r as any
})
)

const toggle = ref(true)
const instanceRef = ref<any>(null)
const App = {
render: () => {
return h(KeepAlive, () =>
toggle.value ? h(AsyncComp, { ref: instanceRef }) : null
)
}
}

render(h(App), root)

resolve!({
render() {
return h('div')
}
})

await timeout()
expect(instanceRef.value).not.toBe(null)

toggle.value = false
await nextTick()

toggle.value = true
await nextTick()

expect(instanceRef.value).not.toBe(null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not enough to simply check that instanceRef.value is not null. We need to ensure that the methods exposed by AsyncComp can be called correctly to fully match the use case of #4999. I tested the reproduction of #4999 with this PR, and it did not pass.

})
})
15 changes: 12 additions & 3 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ function baseCreateRenderer(

// set ref
if (ref != null && parentComponent) {
setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2, parentComponent)
}
}

Expand Down Expand Up @@ -2355,7 +2355,8 @@ export function setRef(
oldRawRef: VNodeNormalizedRef | null,
parentSuspense: SuspenseBoundary | null,
vnode: VNode,
isUnmount = false
isUnmount = false,
parentComponent: ComponentInternalInstance | null = null
) {
if (isArray(rawRef)) {
rawRef.forEach((r, i) =>
Expand All @@ -2370,7 +2371,15 @@ export function setRef(
return
}

if (isAsyncWrapper(vnode) && !isUnmount) {
if (
isAsyncWrapper(vnode) &&
!isUnmount &&
// #4999
// when async-component in keep-alive, it should set ref to async-component
// so actual vnode in async-component can access right ref, because the
// keep-alive cache the wrapper vnode
!(parentComponent && isKeepAlive(parentComponent.vnode))
) {
// when mounting async components, nothing needs to be done,
// because the template ref is forwarded to inner component
return
Expand Down