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): avoid infinite loop rendering caused by setting props multiple times #3384

Closed
wants to merge 1 commit into from
Closed
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
47 changes: 47 additions & 0 deletions packages/runtime-core/__tests__/rendererComponent.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,51 @@ describe('renderer: component', () => {
expect(serializeInner(root)).toBe(`<h1>1</h1>`)
})
})

// #3371
test(`should not cause an infinite loop when the child component's props track the parent component's render fn`, async () => {
const Parent = {
setup(props: any, { slots }: SetupContext) {
const childProps = ref()
const registerChildProps = (props: any) => {
childProps.value = props
}
provide('register', registerChildProps)

return () => {
// access the child component's props
childProps.value && childProps.value.foo
return slots.default!()
}
}
}

const Child = {
props: {
foo: {
type: Boolean,
required: false
}
},
setup(props: { foo: boolean }) {
const register = inject('register') as any
// 1. change the reactivity data of the parent component
// 2. register its own props to the parent component
register(props)

return () => 'foo'
}
}

const App = {
setup() {
return () => h(Parent, () => h(Child as any, { foo: '' }, () => null))
}
}

const root = nodeOps.createElement('div')
render(h(App), root)
await nextTick()
expect(serializeInner(root)).toBe(`foo`)
})
})
11 changes: 8 additions & 3 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ function setFullProps(
) {
const [options, needCastKeys] = instance.propsOptions
let hasAttrsChanged = false
const rawCurrentProps = extend({}, toRaw(props))
if (rawProps) {
for (let key in rawProps) {
// key, ref are reserved and never passed down
Expand All @@ -337,7 +338,7 @@ function setFullProps(
// kebab -> camel conversion here we need to camelize the key.
let camelKey
if (options && hasOwn(options, (camelKey = camelize(key)))) {
props[camelKey] = value
rawCurrentProps[camelKey] = value
} else if (!isEmitListener(instance.emitsOptions, key)) {
// Any non-declared (either as a prop or an emitted event) props are put
// into a separate `attrs` object for spreading. Make sure to preserve
Expand All @@ -358,7 +359,6 @@ function setFullProps(
}

if (needCastKeys) {
const rawCurrentProps = toRaw(props)
for (let i = 0; i < needCastKeys.length; i++) {
const key = needCastKeys[i]
props[key] = resolvePropValue(
Expand All @@ -370,7 +370,12 @@ function setFullProps(
)
}
}

if (options) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds two additional object key enumerations each call which is not ideal - I managed to avoid them in 0255be2

for (const key in rawCurrentProps) {
if (!needCastKeys || !needCastKeys.includes(key))
props[key] = rawCurrentProps[key]
}
}
return hasAttrsChanged
}

Expand Down