Skip to content

Commit

Permalink
fix(runtime-core): handle ref unmounting in conditional updates
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Feb 18, 2025
1 parent 0c8dd94 commit dab629c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/runtime-core/__tests__/rendererTemplateRef.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,37 @@ describe('api: template refs', () => {
expect(el.value).toBe(null)
})

it('render function ref unmount with conditional update', async () => {
const root1 = nodeOps.createElement('div')
const root2 = nodeOps.createElement('div')
const el1 = ref(null)
const el2 = ref(null)
const toggle = ref(true)

const Comp1 = {
setup() {
return () => (toggle.value ? h('div', { ref: el1 }) : h('div'))
},
}

const Comp2 = {
setup() {
return () => h('div', { ref: toggle.value ? el2 : undefined })
},
}

render(h(Comp1), root1)
render(h(Comp2), root2)

expect(el1.value).toBe(root1.children[0])
expect(el2.value).toBe(root2.children[0])

toggle.value = false
await nextTick()
expect(el1.value).toBe(null)
expect(el2.value).toBe(null)
})

test('string ref inside slots', async () => {
const root = nodeOps.createElement('div')
const spy = vi.fn()
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,8 @@ function baseCreateRenderer(
// set ref
if (ref != null && parentComponent) {
setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
} else if (ref == null && n1 && n1.ref != null) {
setRef(n1.ref, n1.ref, parentSuspense, n2 || n1, true)
}
}

Expand Down

0 comments on commit dab629c

Please sign in to comment.