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): handle nested reactive object in guardReactiveProps #11693

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
16 changes: 15 additions & 1 deletion packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -642,11 +642,25 @@ function _createVNode(
)
}

function generateProps(props: Data & VNodeProps) {
const target: Data & VNodeProps = {}
for (const key in props) {
if (isProxy(props[key])) {
Copy link
Member

@edison1105 edison1105 Aug 23, 2024

Choose a reason for hiding this comment

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

should check props[key] is Object first.
consider the following situation:

const state = reactive({
      title: 'hi',
      obj: {
        style: {
          color: 'red',
        },
      },
    })

In theory, only 2 layers of nesting need to be handled here, and for safety, maybe it is necessary to deal with deep recursive processing.

target[key] = extend({}, props[key])
} else {
target[key] = props[key]
}
}
return target
}

export function guardReactiveProps(
props: (Data & VNodeProps) | null,
): (Data & VNodeProps) | null {
if (!props) return null
return isProxy(props) || isInternalObject(props) ? extend({}, props) : props
return isProxy(props) || isInternalObject(props)
? extend({}, generateProps(props))
: props
}

export function cloneVNode<T, U>(
Expand Down
46 changes: 45 additions & 1 deletion packages/runtime-dom/__tests__/patchProps.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { patchProp } from '../src/patchProp'
import { h, nextTick, ref, render } from '../src'
import {
createElementBlock,
guardReactiveProps,
h,
nextTick,
normalizeProps,
reactive,
ref,
render,
toRefs,
} from '../src'

describe('runtime-dom: props patching', () => {
test('basic', () => {
Expand Down Expand Up @@ -351,4 +361,38 @@ describe('runtime-dom: props patching', () => {
expect(el.translate).toBeFalsy()
expect(el.getAttribute('translate')).toBe('no')
})

// #11691
test('should update the color style of the element from red to yellow after a timeout', async () => {
const state = reactive({
obj: {
style: {
color: 'red',
},
},
})
const App = {
setup() {
const { obj } = toRefs(state)
setTimeout(async () => {
state.obj.style.color = 'yellow'
Copy link
Member

Choose a reason for hiding this comment

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

Why not move those 3 lines to the end of this case?

await nextTick()
expect(el.style.color).toBe('yellow')
})
return () => {
// <div v-bind="obj">msg</div>
return createElementBlock(
'div',
normalizeProps(guardReactiveProps(obj.value)),
'msg',
17,
)
}
},
}
const root = document.createElement('div')
render(h(App), root)
const el = root.children[0] as HTMLSelectElement
expect(el.style.color).toBe('red')
})
})