Skip to content

Commit dab629c

Browse files
committed
fix(runtime-core): handle ref unmounting in conditional updates
1 parent 0c8dd94 commit dab629c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

packages/runtime-core/__tests__/rendererTemplateRef.spec.ts

+31
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,37 @@ describe('api: template refs', () => {
179179
expect(el.value).toBe(null)
180180
})
181181

182+
it('render function ref unmount with conditional update', async () => {
183+
const root1 = nodeOps.createElement('div')
184+
const root2 = nodeOps.createElement('div')
185+
const el1 = ref(null)
186+
const el2 = ref(null)
187+
const toggle = ref(true)
188+
189+
const Comp1 = {
190+
setup() {
191+
return () => (toggle.value ? h('div', { ref: el1 }) : h('div'))
192+
},
193+
}
194+
195+
const Comp2 = {
196+
setup() {
197+
return () => h('div', { ref: toggle.value ? el2 : undefined })
198+
},
199+
}
200+
201+
render(h(Comp1), root1)
202+
render(h(Comp2), root2)
203+
204+
expect(el1.value).toBe(root1.children[0])
205+
expect(el2.value).toBe(root2.children[0])
206+
207+
toggle.value = false
208+
await nextTick()
209+
expect(el1.value).toBe(null)
210+
expect(el2.value).toBe(null)
211+
})
212+
182213
test('string ref inside slots', async () => {
183214
const root = nodeOps.createElement('div')
184215
const spy = vi.fn()

packages/runtime-core/src/renderer.ts

+2
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,8 @@ function baseCreateRenderer(
484484
// set ref
485485
if (ref != null && parentComponent) {
486486
setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
487+
} else if (ref == null && n1 && n1.ref != null) {
488+
setRef(n1.ref, n1.ref, parentSuspense, n2 || n1, true)
487489
}
488490
}
489491

0 commit comments

Comments
 (0)