Skip to content

Commit 6c8bfa1

Browse files
authored
fix(runtime-core): fix parent el update on nested HOC self-update (#1360)
fix #1357
1 parent be69bee commit 6c8bfa1

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
ref,
3+
h,
4+
render,
5+
nodeOps,
6+
serializeInner,
7+
nextTick,
8+
VNode
9+
} from '@vue/runtime-test'
10+
11+
describe('renderer: component', () => {
12+
test('should update parent(hoc) component host el when child component self update', async () => {
13+
const value = ref(true)
14+
let parentVnode: VNode
15+
let childVnode1: VNode
16+
let childVnode2: VNode
17+
18+
const Parent = {
19+
render: () => {
20+
// let Parent first rerender
21+
console.log(value.value)
22+
return (parentVnode = h(Child))
23+
}
24+
}
25+
26+
const Child = {
27+
render: () => {
28+
return value.value
29+
? (childVnode1 = h('div'))
30+
: (childVnode2 = h('span'))
31+
}
32+
}
33+
34+
const root = nodeOps.createElement('div')
35+
render(h(Parent), root)
36+
expect(serializeInner(root)).toBe(`<div></div>`)
37+
expect(parentVnode!.el).toBe(childVnode1!.el)
38+
39+
value.value = false
40+
await nextTick()
41+
expect(serializeInner(root)).toBe(`<span></span>`)
42+
expect(parentVnode!.el).toBe(childVnode2!.el)
43+
})
44+
})

packages/runtime-core/src/renderer.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,7 @@ function baseCreateRenderer(
12161216
// no update needed. just copy over properties
12171217
n2.component = n1.component
12181218
n2.el = n1.el
1219+
instance.vnode = n2
12191220
}
12201221
}
12211222

@@ -1304,6 +1305,7 @@ function baseCreateRenderer(
13041305
// This is triggered by mutation of component's own state (next: null)
13051306
// OR parent calling processComponent (next: VNode)
13061307
let { next, bu, u, parent, vnode } = instance
1308+
let originNext = next
13071309
let vnodeHook: VNodeHook | null | undefined
13081310
if (__DEV__) {
13091311
pushWarningContext(next || instance.vnode)
@@ -1355,7 +1357,7 @@ function baseCreateRenderer(
13551357
endMeasure(instance, `patch`)
13561358
}
13571359
next.el = nextTree.el
1358-
if (next === null) {
1360+
if (originNext === null) {
13591361
// self-triggered update. In case of HOC, update parent component
13601362
// vnode el. HOC is indicated by parent instance's subTree pointing
13611363
// to child component's vnode

0 commit comments

Comments
 (0)