Skip to content

Commit

Permalink
fix(runtime-core): fix child component double update on props change
Browse files Browse the repository at this point in the history
fix #4365
  • Loading branch information
yyx990803 committed Aug 17, 2021
1 parent 57f1081 commit c1f564e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
30 changes: 30 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Expand Up @@ -324,4 +324,34 @@ describe('renderer: component', () => {
expect(serializeInner(root)).toBe(``)
expect(ids).toEqual([ids[0], ids[0] + 1, ids[0] + 2])
})

test('child component props update should not lead to double update', async () => {
const text = ref(0)
const spy = jest.fn()

const App = {
render() {
return h(Comp, { text: text.value })
}
}

const Comp = {
props: ['text'],
render(this: any) {
spy()
return h('h1', this.text)
}
}

const root = nodeOps.createElement('div')
render(h(App), root)

expect(serializeInner(root)).toBe(`<h1>0</h1>`)
expect(spy).toHaveBeenCalledTimes(1)

text.value++
await nextTick()
expect(serializeInner(root)).toBe(`<h1>1</h1>`)
expect(spy).toHaveBeenCalledTimes(2)
})
})
6 changes: 4 additions & 2 deletions packages/runtime-core/src/renderer.ts
Expand Up @@ -1458,15 +1458,16 @@ function baseCreateRenderer(
pushWarningContext(next || instance.vnode)
}

// Disallow component effect recursion during pre-lifecycle hooks.
effect.allowRecurse = false

if (next) {
next.el = vnode.el
updateComponentPreRender(instance, next, optimized)
} else {
next = vnode
}

// Disallow component effect recursion during pre-lifecycle hooks.
effect.allowRecurse = false
// beforeUpdate hook
if (bu) {
invokeArrayFns(bu)
Expand All @@ -1481,6 +1482,7 @@ function baseCreateRenderer(
) {
instance.emit('hook:beforeUpdate')
}

effect.allowRecurse = true

// render
Expand Down

0 comments on commit c1f564e

Please sign in to comment.