Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ed276a9

Browse files
committed
fix(runtime-vapor): update props/slots on vapor child reactivation in vdom KeepAlive
1 parent d978639 commit ed276a9

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

‎packages/runtime-vapor/__tests__/vdomInterop.spec.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,4 +1932,62 @@ describe('vdomInterop', () => {
19321932
expect(html()).toContain('<span>resolved</span>')
19331933
})
19341934
})
1935+
1936+
describe('KeepAlive', () => {
1937+
test('should update props on reactivation of vapor child in vdom KeepAlive', async () => {
1938+
const VaporChild = defineVaporComponent({
1939+
props: { msg: String },
1940+
setup(props: any) {
1941+
const n0 = template('<div> </div>')() as any
1942+
const x0 = child(n0) as any
1943+
renderEffect(() => setText(x0, props.msg))
1944+
return n0
1945+
},
1946+
})
1947+
1948+
const VdomChild = defineComponent({
1949+
setup() {
1950+
return () => h('span', 'vdom')
1951+
},
1952+
})
1953+
1954+
const current = shallowRef<any>(VaporChild)
1955+
const msg = ref('hello')
1956+
1957+
const App = defineComponent({
1958+
setup() {
1959+
return () =>
1960+
h(KeepAlive, null, {
1961+
default: () =>
1962+
h(
1963+
resolveDynamicComponent(current.value) as any,
1964+
current.value === VaporChild ? { msg: msg.value } : null,
1965+
),
1966+
})
1967+
},
1968+
})
1969+
1970+
const root = document.createElement('div')
1971+
const app = createApp(App)
1972+
app.use(vaporInteropPlugin)
1973+
app.mount(root)
1974+
1975+
expect(root.innerHTML).toBe('<div>hello</div>')
1976+
1977+
// Switch to vdom child (deactivates vapor child)
1978+
current.value = VdomChild
1979+
await nextTick()
1980+
expect(root.innerHTML).toBe('<span>vdom</span>')
1981+
1982+
// Change props while vapor child is deactivated
1983+
msg.value = 'updated'
1984+
await nextTick()
1985+
expect(root.innerHTML).toBe('<span>vdom</span>')
1986+
1987+
// Reactivate vapor child — should reflect new props
1988+
current.value = VaporChild
1989+
await nextTick()
1990+
expect(root.innerHTML).toBe('<div>updated</div>')
1991+
})
1992+
})
19351993
})

‎packages/runtime-vapor/src/vdomInterop.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,12 @@ const vaporInteropImpl: Omit<
352352
vnode.el = cached.el
353353
vnode.component = cached.component
354354
vnode.anchor = cached.anchor
355-
activate(vnode.component as any, container, anchor)
355+
const instance = vnode.component as any as VaporComponentInstance
356+
activate(instance, container, anchor)
356357
insert(vnode.anchor as any, container, anchor)
358+
// in case props have changed while deactivated
359+
instance.rawPropsRef!.value = filterReservedProps(vnode.props)
360+
instance.rawSlotsRef!.value = vnode.children
357361
},
358362

359363
deactivate(vnode, container) {

0 commit comments

Comments
 (0)