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 1 commit
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
12 changes: 12 additions & 0 deletions packages/react-dom/src/__tests__/ReactDOMFiber-test.js
Expand Up @@ -171,6 +171,18 @@ 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 EmptyFragment = () => <React.Fragment />;

let instance = null;
ReactDOM.render(<EmptyFragment />, container);

expect(container.childNodes.length).toBe(0);

const firstNode = ReactDOM.findDOMNode(instance);
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can just check container.firstChild?

expect(firstNode).toBe(null);
});

let svgEls, htmlEls, mathEls;
const expectSVG = {ref: el => svgEls.push(el)};
const expectHTML = {ref: el => htmlEls.push(el)};
Expand Down
6 changes: 6 additions & 0 deletions packages/react-reconciler/src/ReactChildFiber.js
Expand Up @@ -1215,6 +1215,12 @@ function ChildReconciler(shouldTrackSideEffects) {
newChild.key === null
) {
newChild = newChild.props.children;

// In case of an empty fragment, this is undefined. Since we crash when
// undefined is reconciled, we bail out early.
if (typeof newChild === 'undefined') {
return deleteRemainingChildren(returnFiber, currentFirstChild);
}
}

// Handle object types
Expand Down