Skip to content
Merged
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
28 changes: 28 additions & 0 deletions packages/runtime-core/__tests__/componentSlots.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,4 +324,32 @@ describe('component: slots', () => {
'Slot "default" invoked outside of the render function',
).not.toHaveBeenWarned()
})

test('should not warn when render in setup', () => {
const container = {
setup(_: any, { slots }: any) {
return function () {
return slots.default && slots.default()
}
},
}

const comp = h(container, null, {
default() {
return () => h('div')
},
})

const App = {
setup() {
render(h(comp), nodeOps.createElement('div'))
return () => null
},
}

createApp(App).mount(nodeOps.createElement('div'))
expect(
'Slot "default" invoked outside of the render function',
).not.toHaveBeenWarned()
})
})
9 changes: 5 additions & 4 deletions packages/runtime-core/src/componentSlots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ShapeFlags,
SlotFlags,
def,
hasOwn,
isArray,
isFunction,
} from '@vue/shared'
Expand Down Expand Up @@ -87,17 +88,18 @@ const normalizeSlotValue = (value: unknown): VNode[] =>
const normalizeSlot = (
key: string,
rawSlot: Function,
ctx: ComponentInternalInstance | null | undefined,
rawSlots: RawSlots,
): Slot => {
if ((rawSlot as any)._n) {
// already normalized - #5353
return rawSlot as Slot
}
const ctx = rawSlots._ctx
const normalized = withCtx((...args: any[]) => {
if (
__DEV__ &&
currentInstance &&
(!ctx || ctx.root === currentInstance.root)
(!hasOwn(rawSlots, '_ctx') || (ctx && ctx.root === currentInstance.root))
Copy link
Member

Choose a reason for hiding this comment

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

The condition will cause the following warning to never trigger. Removing the condition || (ctx && ctx.root === currentInstance.root) will also prevent the warning from triggering in

'Slot "default" invoked outside of the render function',
. I think a test case needs to be added to verify the warning will trigger.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't see anything wrong with it .
added a basic warn case

Copy link
Member

Choose a reason for hiding this comment

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

children = { default: children, _ctx: currentRenderingInstance }

if the slot value is a function the rawSlots always has the _ctx key.

so all test cases pass using the following conditions

if (
  __DEV__ &&
  currentInstance &&
  (ctx && ctx.root === currentInstance.root)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added a few test cases, are they correct?

Copy link
Member

@edison1105 edison1105 Oct 18, 2024

Choose a reason for hiding this comment

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

All test cases are correct.
the proper condition should be:

if (
  __DEV__ &&
  currentInstance &&
  !(ctx === null && currentRenderingInstance) &&
  !(ctx && ctx.root !== currentInstance.root)
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

cool!!!

) {
warn(
`Slot "${key}" invoked outside of the render function: ` +
Expand All @@ -117,12 +119,11 @@ const normalizeObjectSlots = (
slots: InternalSlots,
instance: ComponentInternalInstance,
) => {
const ctx = rawSlots._ctx
for (const key in rawSlots) {
if (isInternalKey(key)) continue
const value = rawSlots[key]
if (isFunction(value)) {
slots[key] = normalizeSlot(key, value, ctx)
slots[key] = normalizeSlot(key, value, rawSlots)
} else if (value != null) {
if (
__DEV__ &&
Expand Down