Skip to content

Commit 4e78ff9

Browse files
Don’t error when returning an empty Fragment
Fixes #12964 When a fragment is reconciled, we directly move onto it’s children. Since an empty `<React.Fragment/>` will have children of `undefined`, this would always throw. To fix this, we bail out in those cases. This case now behaves like returning `null` directly.
1 parent 36546b5 commit 4e78ff9

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

packages/react-dom/src/__tests__/ReactDOMFiber-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ describe('ReactDOMFiber', () => {
171171
expect(firstNode.tagName).toBe('DIV');
172172
});
173173

174+
it('renders an empty fragment', () => {
175+
const EmptyFragment = () => <React.Fragment />;
176+
177+
let instance = null;
178+
ReactDOM.render(<EmptyFragment />, container);
179+
180+
expect(container.childNodes.length).toBe(0);
181+
182+
const firstNode = ReactDOM.findDOMNode(instance);
183+
expect(firstNode).toBe(null);
184+
});
185+
174186
let svgEls, htmlEls, mathEls;
175187
const expectSVG = {ref: el => svgEls.push(el)};
176188
const expectHTML = {ref: el => htmlEls.push(el)};

packages/react-reconciler/src/ReactChildFiber.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,12 @@ function ChildReconciler(shouldTrackSideEffects) {
12151215
newChild.key === null
12161216
) {
12171217
newChild = newChild.props.children;
1218+
1219+
// In case of an empty fragment, this is undefined. Since we crash when
1220+
// undefined is reconciled, we replace this case with null.
1221+
if (typeof newChild === 'undefined') {
1222+
return deleteRemainingChildren(returnFiber, currentFirstChild);
1223+
}
12181224
}
12191225

12201226
// Handle object types

0 commit comments

Comments
 (0)