Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Special case errror messages for children of host components
These are likely meant to be text content if they're not a supported object.
  • Loading branch information
sebmarkbage committed Oct 17, 2022
commit a94c21bcacf303d3d8d16b476875144d08c42417
17 changes: 8 additions & 9 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ describe('ReactFlight', () => {
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Date are not supported.',
'Date objects are not supported.',
{withoutStack: true},
);
});
Expand All @@ -536,8 +536,7 @@ describe('ReactFlight', () => {
);
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Date are not supported.\n' +
'Date objects cannot be rendered as text children. Try formatting it using toString().\n' +
' <div>Current date: {Date}</div>\n' +
' ^^^^^^',
{withoutStack: true},
Expand All @@ -550,7 +549,7 @@ describe('ReactFlight', () => {
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Math are not supported.\n' +
'Math objects are not supported.\n' +
' <input value={Math}>\n' +
' ^^^^^^',
{withoutStack: true},
Expand Down Expand Up @@ -582,7 +581,7 @@ describe('ReactFlight', () => {
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Date are not supported.',
'Date objects are not supported.',
{withoutStack: true},
);
});
Expand All @@ -599,7 +598,7 @@ describe('ReactFlight', () => {
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Date are not supported.\n' +
'Date objects are not supported.\n' +
' <>Current date: {Date}</>\n' +
' ^^^^^^',
{withoutStack: true},
Expand All @@ -616,7 +615,7 @@ describe('ReactFlight', () => {
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Math are not supported.\n' +
'Math objects are not supported.\n' +
' <... value={Math}>\n' +
' ^^^^^^',
{withoutStack: true},
Expand Down Expand Up @@ -652,7 +651,7 @@ describe('ReactFlight', () => {
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Math are not supported.\n' +
'Math objects are not supported.\n' +
' {hello: Math, title: <h1/>}\n' +
' ^^^^',
{withoutStack: true},
Expand All @@ -673,7 +672,7 @@ describe('ReactFlight', () => {
ReactNoopFlightClient.read(transport);
}).toErrorDev(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like Math are not supported.\n' +
'Math objects are not supported.\n' +
' [..., Math, <h1/>]\n' +
' ^^^^',
{withoutStack: true},
Expand Down
23 changes: 16 additions & 7 deletions packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,12 +712,21 @@ export function resolveModelToJSON(
const originalValue = parent[key];
if (typeof originalValue === 'object' && originalValue !== value) {
if (objectName(originalValue) !== 'Object') {
console.error(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like %s are not supported.%s',
objectName(originalValue),
describeObjectForErrorMessage(parent, key),
);
const jsxParentType = jsxChildrenParents.get(parent);
if (typeof jsxParentType === 'string') {
console.error(
'%s objects cannot be rendered as text children. Try formatting it using toString().%s',
objectName(originalValue),
describeObjectForErrorMessage(parent, key),
);
} else {
console.error(
'Only plain objects can be passed to Client Components from Server Components. ' +
'%s objects are not supported.%s',
objectName(originalValue),
describeObjectForErrorMessage(parent, key),
);
}
} else {
console.error(
'Only plain objects can be passed to Client Components from Server Components. ' +
Expand Down Expand Up @@ -855,7 +864,7 @@ export function resolveModelToJSON(
if (objectName(value) !== 'Object') {
console.error(
'Only plain objects can be passed to Client Components from Server Components. ' +
'Built-ins like %s are not supported.%s',
'%s objects are not supported.%s',
objectName(value),
describeObjectForErrorMessage(parent, key),
);
Expand Down