Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/error-function-jsx-children.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/qwik': patch
---

Throw an error when a function is passed as a JSX child instead of silently skipping it. This restores the v1 behavior where passing `{myFn}` instead of `{myFn()}` would produce a clear error message.
6 changes: 6 additions & 0 deletions packages/qwik/src/core/render/dom/render-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ export const processData = (
return node.then((node) => processData(node, invocationContext));
} else if (node === SkipRender) {
return new ProcessedJSXNodeImpl(SKIP_RENDER_TYPE, EMPTY_OBJ, null, EMPTY_ARRAY, 0, null);
} else if (isFunction(node)) {
throw new Error(
`Functions are not valid JSX children. ` +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This isn't quite true: functions are valid JSX children, but only when the parent is an Inline Component that will use children() somehow.

so the message should be something like JSX Function values cannot be rendered.

Furthermore, make sure that the error output is sufficient to find where it is defined. Maybe throwing is enough, but maybe you need to find the file from the dev data, or console.error the node or something like that.

`You might have passed a function instead of calling it: use {fn()} instead of {fn}. ` +
`If this is a component, wrap it with component$().`
);
} else {
logWarn('A unsupported value was passed to the JSX, skipping render. Value:', node);
return undefined;
Expand Down
10 changes: 10 additions & 0 deletions packages/qwik/src/core/render/dom/render.unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ test('should only render string/number', async () => {
await expectRendered(fixture, '<div>string123</div>');
});

test('should throw error when function is passed as JSX child', async () => {
const fixture = new ElementFixture();
try {
await render(fixture.host, <div>{() => <span>hello</span>}</div>);
assert.fail('Expected an error to be thrown');
} catch (e: any) {
assert.match(e.message, /is not an accepted value|Functions are not valid JSX children/);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

here, ensure that it's easy to find where the wrong definition was.

}
});

test('should serialize events correctly', async () => {
const fixture = new ElementFixture();
await render(
Expand Down
6 changes: 6 additions & 0 deletions packages/qwik/src/core/render/ssr/render-ssr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,12 @@ const processData = (
} else if (isPromise(node)) {
stream.write(FLUSH_COMMENT);
return node.then((node) => processData(node, rCtx, ssrCtx, stream, flags, beforeClose));
} else if (isFunction(node)) {
throw new Error(
`Functions are not valid JSX children. ` +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

same as DOM

`You might have passed a function instead of calling it: use {fn()} instead of {fn}. ` +
`If this is a component, wrap it with component$().`
);
} else {
logWarn('A unsupported value was passed to the JSX, skipping render. Value:', node);
return;
Expand Down
5 changes: 5 additions & 0 deletions packages/qwik/src/core/render/ssr/render-ssr.unit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,11 @@ export const HtmlContext = component$(() => {
return <Slot />;
});

test('should throw error when function is passed as JSX child', async () => {
const fn = () => jsx('div', { children: 'hello' });
await throws(() => testSSR(jsx('body', { children: [fn] }), ''));
});

async function testSSR(
node: JSXOutput,
expected: string | string[],
Expand Down
Loading