Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): prevent child component updates when style remains unchanged #12825

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
inject,
nextTick,
nodeOps,
onMounted,
provide,
ref,
render,
Expand Down Expand Up @@ -474,4 +475,55 @@ describe('renderer: component', () => {
`Property '$attrs' was accessed via 'this'. Avoid using 'this' in templates.`,
).toHaveBeenWarned()
})

test('should not update child component if style is not changed', async () => {
const text = ref(0)
const spy = vi.fn()

const ClientOnly = {
setup(_: any, { slots }: SetupContext) {
const mounted = ref(false)
onMounted(() => {
mounted.value = true
})
return () => {
if (mounted.value) {
return slots.default!()
}
}
},
}

const App = {
render() {
return h(ClientOnly, null, {
default: () => [
h('span', null, [text.value]),
h(Comp, { style: { width: '100%' } }),
],
})
},
}

const Comp = {
render(this: any) {
spy()
return null
},
}

const root = nodeOps.createElement('div')
render(h(App), root)
expect(serializeInner(root)).toBe(`<!---->`)
await nextTick()

expect(serializeInner(root)).toBe(`<span>0</span><!---->`)
expect(spy).toHaveBeenCalledTimes(1)

text.value++
await nextTick()
expect(serializeInner(root)).toBe(`<span>1</span><!---->`)
// expect Comp to not be re-rendered
expect(spy).toHaveBeenCalledTimes(1)
})
})
26 changes: 23 additions & 3 deletions packages/runtime-core/src/componentRenderUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ import {
normalizeVNode,
} from './vnode'
import { ErrorCodes, handleError } from './errorHandling'
import { PatchFlags, ShapeFlags, isModelListener, isOn } from '@vue/shared'
import {
PatchFlags,
ShapeFlags,
isModelListener,
isObject,
isOn,
looseEqual,
} from '@vue/shared'
import { warn } from './warning'
import { isHmrUpdating } from './hmr'
import type { NormalizedProps } from './componentProps'
Expand Down Expand Up @@ -399,7 +406,7 @@ export function shouldUpdateComponent(
for (let i = 0; i < dynamicProps.length; i++) {
const key = dynamicProps[i]
if (
nextProps![key] !== prevProps![key] &&
hasPropValueChanged(nextProps!, prevProps!, key) &&
!isEmitListener(emits, key)
) {
return true
Expand Down Expand Up @@ -441,7 +448,7 @@ function hasPropsChanged(
for (let i = 0; i < nextKeys.length; i++) {
const key = nextKeys[i]
if (
nextProps[key] !== prevProps[key] &&
hasPropValueChanged(nextProps, prevProps, key) &&
!isEmitListener(emitsOptions, key)
) {
return true
Expand All @@ -450,6 +457,19 @@ function hasPropsChanged(
return false
}

function hasPropValueChanged(
nextProps: Data,
prevProps: Data,
key: string,
): boolean {
const nextProp = nextProps[key]
const prevProp = prevProps[key]
if (key === 'style' && isObject(nextProp) && isObject(prevProp)) {
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
return !looseEqual(nextProp, prevProp)
}
return nextProp !== prevProp
}

export function updateHOCHostEl(
{ vnode, parent }: ComponentInternalInstance,
el: typeof vnode.el, // HostNode
Expand Down