Skip to content

Commit

Permalink
Disable key warning when element used as type
Browse files Browse the repository at this point in the history
This gets serialized as a tuple in the Flight protocol so we think it needs
a key but it doesn't. It'll error later on the client.

Fix test asserting lazy because it's not actually executing the rendering
pass on the client since the transport is not live in Noop.

Technically this doesn't error if the Shared Component renders something
that can be a type since we actually resolve this component as if it is
just some value.
  • Loading branch information
sebmarkbage committed Jun 26, 2024
1 parent eb947b5 commit fdfec40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
22 changes: 15 additions & 7 deletions packages/react-client/src/__tests__/ReactFlight-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -692,14 +692,22 @@ describe('ReactFlight', () => {

const transport = ReactNoopFlightServer.render(<ServerComponent />);

await act(async () => {
const rootModel = await ReactNoopFlightClient.read(transport);
ReactNoop.render(rootModel);
});
expect(ReactNoop).toMatchRenderedOutput('Loading...');
spyOnDevAndProd(console, 'error').mockImplementation(() => {});
await load();
expect(console.error).toHaveBeenCalledTimes(1);

await expect(async () => {
await act(async () => {
const rootModel = await ReactNoopFlightClient.read(transport);
ReactNoop.render(rootModel);
});
}).rejects.toThrow(
__DEV__
? 'Element type is invalid: expected a string (for built-in components) or a class/function ' +
'(for composite components) but got: <div />. ' +
'Did you accidentally export a JSX literal instead of a component?'
: 'Element type is invalid: expected a string (for built-in components) or a class/function ' +
'(for composite components) but got: object.',
);
expect(ReactNoop).toMatchRenderedOutput(null);
});

it('can render a lazy element', async () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/react-server/src/ReactFlightServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ import {
} from 'shared/ReactSymbols';

import {
describeValueForErrorMessage,
describeObjectForErrorMessage,
isSimpleObject,
jsxPropsParents,
Expand Down Expand Up @@ -1591,6 +1590,14 @@ function renderElement(
validated,
);
}
case REACT_ELEMENT_TYPE: {
// This is invalid but we'll let the client determine that it is.
if (__DEV__) {
// Disable the key warning that would happen otherwise because this
// element gets serialized inside an array. We'll error later anyway.
type._store.validated = 1;
}
}
}
}
// For anything else, try it on the client instead.
Expand Down

0 comments on commit fdfec40

Please sign in to comment.