Skip to content

Commit c1f564e

Browse files
committed
fix(runtime-core): fix child component double update on props change
fix vuejs#4365
1 parent 57f1081 commit c1f564e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,34 @@ describe('renderer: component', () => {
324324
expect(serializeInner(root)).toBe(``)
325325
expect(ids).toEqual([ids[0], ids[0] + 1, ids[0] + 2])
326326
})
327+
328+
test('child component props update should not lead to double update', async () => {
329+
const text = ref(0)
330+
const spy = jest.fn()
331+
332+
const App = {
333+
render() {
334+
return h(Comp, { text: text.value })
335+
}
336+
}
337+
338+
const Comp = {
339+
props: ['text'],
340+
render(this: any) {
341+
spy()
342+
return h('h1', this.text)
343+
}
344+
}
345+
346+
const root = nodeOps.createElement('div')
347+
render(h(App), root)
348+
349+
expect(serializeInner(root)).toBe(`<h1>0</h1>`)
350+
expect(spy).toHaveBeenCalledTimes(1)
351+
352+
text.value++
353+
await nextTick()
354+
expect(serializeInner(root)).toBe(`<h1>1</h1>`)
355+
expect(spy).toHaveBeenCalledTimes(2)
356+
})
327357
})

packages/runtime-core/src/renderer.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,15 +1458,16 @@ function baseCreateRenderer(
14581458
pushWarningContext(next || instance.vnode)
14591459
}
14601460

1461+
// Disallow component effect recursion during pre-lifecycle hooks.
1462+
effect.allowRecurse = false
1463+
14611464
if (next) {
14621465
next.el = vnode.el
14631466
updateComponentPreRender(instance, next, optimized)
14641467
} else {
14651468
next = vnode
14661469
}
14671470

1468-
// Disallow component effect recursion during pre-lifecycle hooks.
1469-
effect.allowRecurse = false
14701471
// beforeUpdate hook
14711472
if (bu) {
14721473
invokeArrayFns(bu)
@@ -1481,6 +1482,7 @@ function baseCreateRenderer(
14811482
) {
14821483
instance.emit('hook:beforeUpdate')
14831484
}
1485+
14841486
effect.allowRecurse = true
14851487

14861488
// render

0 commit comments

Comments
 (0)