Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t error when returning an empty Fragment #12966

Merged
merged 4 commits into from Jun 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFiber-test.js
Expand Up @@ -171,6 +171,31 @@ describe('ReactDOMFiber', () => {
expect(firstNode.tagName).toBe('DIV');
});

it('renders an empty fragment', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No idea if that is the right place for the test :)

const Div = () => <div />;
const EmptyFragment = () => <React.Fragment />;
const NonEmptyFragment = () => (
<React.Fragment>
<Div />
</React.Fragment>
);

ReactDOM.render(<EmptyFragment />, container);
expect(container.firstChild).toBe(null);

ReactDOM.render(<NonEmptyFragment />, container);
expect(container.firstChild.tagName).toBe('DIV');

ReactDOM.render(<EmptyFragment />, container);
expect(container.firstChild).toBe(null);

ReactDOM.render(<Div />, container);
expect(container.firstChild.tagName).toBe('DIV');

ReactDOM.render(<EmptyFragment />, container);
expect(container.firstChild).toBe(null);
});

let svgEls, htmlEls, mathEls;
const expectSVG = {ref: el => svgEls.push(el)};
const expectHTML = {ref: el => htmlEls.push(el)};
Expand Down
8 changes: 4 additions & 4 deletions packages/react-reconciler/src/ReactChildFiber.js
Expand Up @@ -1208,12 +1208,12 @@ function ChildReconciler(shouldTrackSideEffects) {
// Handle top level unkeyed fragments as if they were arrays.
// This leads to an ambiguity between <>{[...]}</> and <>...</>.
// We treat the ambiguous cases above the same.
if (
const isUnkeyedTopLevelFragment =
typeof newChild === 'object' &&
newChild !== null &&
newChild.type === REACT_FRAGMENT_TYPE &&
newChild.key === null
) {
newChild.key === null;
if (isUnkeyedTopLevelFragment) {
newChild = newChild.props.children;
}

Expand Down Expand Up @@ -1281,7 +1281,7 @@ function ChildReconciler(shouldTrackSideEffects) {
warnOnFunctionType();
}
}
if (typeof newChild === 'undefined') {
if (typeof newChild === 'undefined' && !isUnkeyedTopLevelFragment) {
// If the new child is undefined, and the return fiber is a composite
// component, throw an error. If Fiber return types are disabled,
// we already threw above.
Expand Down