Skip to content

Commit d9d4e7f

Browse files
chrisvfritzyyx990803
authored andcommitted
prevent nested thunk children from failing (vuejs#3367)
1 parent e1ab28c commit d9d4e7f

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/core/vdom/helpers.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,16 @@ export function normalizeChildren (
77
children: any,
88
ns: string | void
99
): Array<VNode> | void {
10-
// invoke children thunks.
11-
// components always receive their children as thunks so that they
12-
// can perform the actual render inside their own dependency collection cycle.
13-
if (typeof children === 'function') {
10+
// Invoke children thunks. Components always receive their children
11+
// as thunks so that they can perform the actual render inside their
12+
// own dependency collection cycle. Also, since JSX automatically
13+
// wraps component children in a thunk, we handle nested thunks to
14+
// prevent situations such as <MyComponent>{ children }</MyComponent>
15+
// from failing when it produces a double thunk.
16+
while (typeof children === 'function') {
1417
children = children()
1518
}
19+
1620
if (isPrimitive(children)) {
1721
return [createTextVNode(children)]
1822
}

test/unit/features/options/render.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,16 @@ describe('Options render', () => {
3636
new Vue().$mount()
3737
expect('Failed to mount component: template or render function not defined.').toHaveBeenWarned()
3838
})
39+
40+
// Since JSX automatically thunkifies children, this will
41+
// prevent <MyComponent>{ children }</MyComponent> from
42+
// failing when it produces a double thunk.
43+
it('should support nested thunk children', () => {
44+
const vm = new Vue({
45+
render: h => h('div',
46+
() => () => () => ['hello ', h('strong', 'world')]
47+
)
48+
}).$mount()
49+
expect(vm.$el.innerHTML).toBe('hello <strong>world</strong>')
50+
})
3951
})

0 commit comments

Comments
 (0)