Skip to content

Commit a89e5ed

Browse files
committed
Better error message if you pass a function as a child to a client component
1 parent c1fd2a9 commit a89e5ed

File tree

4 files changed

+49
-12
lines changed

4 files changed

+49
-12
lines changed

packages/react-client/src/__tests__/ReactFlight-test.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ describe('ReactFlight', () => {
689689
);
690690
}
691691
function FunctionProp() {
692-
return <div>{() => {}}</div>;
692+
return <div>{function fn() {}}</div>;
693693
}
694694
function SymbolProp() {
695695
return <div foo={Symbol('foo')} />;
@@ -707,8 +707,11 @@ describe('ReactFlight', () => {
707707
</Client>
708708
);
709709
}
710+
function FunctionChildrenClient() {
711+
return <Client>{function Component() {}}</Client>;
712+
}
710713
function FunctionPropClient() {
711-
return <Client>{() => {}}</Client>;
714+
return <Client foo={() => {}} />;
712715
}
713716
function SymbolPropClient() {
714717
return <Client foo={Symbol('foo')} />;
@@ -731,6 +734,10 @@ describe('ReactFlight', () => {
731734
<EventHandlerPropClient />,
732735
options,
733736
);
737+
const fnChildrenClient = ReactNoopFlightServer.render(
738+
<FunctionChildrenClient />,
739+
options,
740+
);
734741
const fnClient = ReactNoopFlightServer.render(
735742
<FunctionPropClient />,
736743
options,
@@ -754,7 +761,9 @@ describe('ReactFlight', () => {
754761
</ErrorBoundary>
755762
<ErrorBoundary
756763
expectedMessage={
757-
'Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".'
764+
__DEV__
765+
? 'Functions are not valid as a child of Client Components. This may happen if you return fn instead of <fn /> from render. Or maybe you meant to call this function rather than return it.'
766+
: 'Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".'
758767
}>
759768
<Render promise={ReactNoopFlightClient.read(fn)} />
760769
</ErrorBoundary>
@@ -767,6 +776,14 @@ describe('ReactFlight', () => {
767776
<ErrorBoundary expectedMessage="Event handlers cannot be passed to Client Component props.">
768777
<Render promise={ReactNoopFlightClient.read(eventClient)} />
769778
</ErrorBoundary>
779+
<ErrorBoundary
780+
expectedMessage={
781+
__DEV__
782+
? 'Functions are not valid as a child of Client Components. This may happen if you return Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.'
783+
: 'Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".'
784+
}>
785+
<Render promise={ReactNoopFlightClient.read(fnChildrenClient)} />
786+
</ErrorBoundary>
770787
<ErrorBoundary
771788
expectedMessage={
772789
'Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with "use server".'
@@ -945,8 +962,8 @@ describe('ReactFlight', () => {
945962
'Only plain objects can be passed to Client Components from Server Components. ' +
946963
'Objects with toJSON methods are not supported. ' +
947964
'Convert it manually to a simple value before passing it to props.\n' +
948-
' <input value={{toJSON: function}}>\n' +
949-
' ^^^^^^^^^^^^^^^^^^^^',
965+
' <input value={{toJSON: ...}}>\n' +
966+
' ^^^^^^^^^^^^^^^',
950967
{withoutStack: true},
951968
);
952969
});
@@ -1035,8 +1052,8 @@ describe('ReactFlight', () => {
10351052
'Only plain objects can be passed to Client Components from Server Components. ' +
10361053
'Objects with toJSON methods are not supported. ' +
10371054
'Convert it manually to a simple value before passing it to props.\n' +
1038-
' <>Current date: {{toJSON: function}}</>\n' +
1039-
' ^^^^^^^^^^^^^^^^^^^^',
1055+
' <>Current date: {{toJSON: ...}}</>\n' +
1056+
' ^^^^^^^^^^^^^^^',
10401057
{withoutStack: true},
10411058
);
10421059
});

packages/react-server/src/ReactFlightServer.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1554,10 +1554,27 @@ function renderModelDestructive(
15541554
describeObjectForErrorMessage(parent, parentPropertyName) +
15551555
'\nIf you need interactivity, consider converting part of this to a Client Component.',
15561556
);
1557+
} else if (
1558+
__DEV__ &&
1559+
(jsxChildrenParents.has(parent) ||
1560+
(jsxPropsParents.has(parent) && parentPropertyName === 'children'))
1561+
) {
1562+
const componentName = value.displayName || value.name || 'Component';
1563+
throw new Error(
1564+
'Functions are not valid as a child of Client Components. This may happen if ' +
1565+
'you return ' +
1566+
componentName +
1567+
' instead of <' +
1568+
componentName +
1569+
' /> from render. ' +
1570+
'Or maybe you meant to call this function rather than return it.' +
1571+
describeObjectForErrorMessage(parent, parentPropertyName),
1572+
);
15571573
} else {
15581574
throw new Error(
15591575
'Functions cannot be passed directly to Client Components ' +
1560-
'unless you explicitly expose it by marking it with "use server".' +
1576+
'unless you explicitly expose it by marking it with "use server". ' +
1577+
'Or maybe you meant to call this function rather than return it.' +
15611578
describeObjectForErrorMessage(parent, parentPropertyName),
15621579
);
15631580
}

packages/shared/ReactSerializationErrors.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,10 @@ export function describeValueForErrorMessage(value: mixed): string {
104104
}
105105
return name;
106106
}
107-
case 'function':
108-
return 'function';
107+
case 'function': {
108+
const name = (value: any).displayName || value.name;
109+
return name ? 'function ' + name : 'function';
110+
}
109111
default:
110112
// eslint-disable-next-line react-internal/safe-string-coercion
111113
return String(value);

scripts/error-codes/codes.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@
361361
"372": "Cannot call unstable_createEventHandle with \"%s\", as it is not an event known to React.",
362362
"373": "This Hook is not supported in Server Components.",
363363
"374": "Event handlers cannot be passed to Client Component props.%s\nIf you need interactivity, consider converting part of this to a Client Component.",
364-
"375": "Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with \"use server\".%s",
364+
"375": "Functions cannot be passed directly to Client Components unless you explicitly expose it by marking it with \"use server\". Or maybe you meant to call this function rather than return it.%s",
365365
"376": "Only global symbols received from Symbol.for(...) can be passed to Client Components. The symbol Symbol.for(%s) cannot be found among global symbols.%s",
366366
"377": "BigInt (%s) is not yet supported in Client Component props.%s",
367367
"378": "Type %s is not supported in Client Component props.%s",
@@ -490,5 +490,6 @@
490490
"502": "Cannot read a Client Context from a Server Component.",
491491
"503": "Cannot use() an already resolved Client Reference.",
492492
"504": "Failed to read a RSC payload created by a development version of React on the server while using a production version on the client. Always use matching versions on the server and the client.",
493-
"505": "Cannot render an Async Component, Promise or React.Lazy inside React.Children. We recommend not iterating over children and just rendering them plain."
493+
"505": "Cannot render an Async Component, Promise or React.Lazy inside React.Children. We recommend not iterating over children and just rendering them plain.",
494+
"506": "Functions are not valid as a child of Client Components. This may happen if you return %s instead of <%s /> from render. Or maybe you meant to call this function rather than return it.%s"
494495
}

0 commit comments

Comments
 (0)