Skip to content

Commit a63893f

Browse files
author
Brian Vaughn
authored
Warn about undefined return value for memo and forwardRef (#19550)
1 parent 32ff428 commit a63893f

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,24 @@ describe('ReactEmptyComponent', () => {
316316
const noscript2 = container.firstChild;
317317
expect(noscript2).toBe(null);
318318
});
319+
320+
it('should warn about React.forwardRef that returns undefined', () => {
321+
const Empty = () => {};
322+
const EmptyForwardRef = React.forwardRef(Empty);
323+
324+
expect(() => {
325+
ReactTestUtils.renderIntoDocument(<EmptyForwardRef />);
326+
}).toThrowError(
327+
'ForwardRef(Empty)(...): Nothing was returned from render.',
328+
);
329+
});
330+
331+
it('should warn about React.memo that returns undefined', () => {
332+
const Empty = () => {};
333+
const EmptyMemo = React.memo(Empty);
334+
335+
expect(() => {
336+
ReactTestUtils.renderIntoDocument(<EmptyMemo />);
337+
}).toThrowError('Empty(...): Nothing was returned from render.');
338+
});
319339
});

packages/react-reconciler/src/ReactChildFiber.new.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import {
2929
ClassComponent,
3030
HostText,
3131
HostPortal,
32+
ForwardRef,
3233
Fragment,
34+
SimpleMemoComponent,
3335
Block,
3436
} from './ReactWorkTags';
3537
import invariant from 'shared/invariant';
@@ -1393,14 +1395,16 @@ function ChildReconciler(shouldTrackSideEffects) {
13931395
// Intentionally fall through to the next case, which handles both
13941396
// functions and classes
13951397
// eslint-disable-next-lined no-fallthrough
1396-
case FunctionComponent: {
1397-
const Component = returnFiber.type;
1398+
case Block:
1399+
case FunctionComponent:
1400+
case ForwardRef:
1401+
case SimpleMemoComponent: {
13981402
invariant(
13991403
false,
14001404
'%s(...): Nothing was returned from render. This usually means a ' +
14011405
'return statement is missing. Or, to render nothing, ' +
14021406
'return null.',
1403-
Component.displayName || Component.name || 'Component',
1407+
getComponentName(returnFiber.type) || 'Component',
14041408
);
14051409
}
14061410
}

packages/react-reconciler/src/ReactChildFiber.old.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import {
2929
ClassComponent,
3030
HostText,
3131
HostPortal,
32+
ForwardRef,
3233
Fragment,
34+
SimpleMemoComponent,
3335
Block,
3436
} from './ReactWorkTags';
3537
import invariant from 'shared/invariant';
@@ -1385,14 +1387,16 @@ function ChildReconciler(shouldTrackSideEffects) {
13851387
// Intentionally fall through to the next case, which handles both
13861388
// functions and classes
13871389
// eslint-disable-next-lined no-fallthrough
1388-
case FunctionComponent: {
1389-
const Component = returnFiber.type;
1390+
case Block:
1391+
case FunctionComponent:
1392+
case ForwardRef:
1393+
case SimpleMemoComponent: {
13901394
invariant(
13911395
false,
13921396
'%s(...): Nothing was returned from render. This usually means a ' +
13931397
'return statement is missing. Or, to render nothing, ' +
13941398
'return null.',
1395-
Component.displayName || Component.name || 'Component',
1399+
getComponentName(returnFiber.type) || 'Component',
13961400
);
13971401
}
13981402
}

0 commit comments

Comments
 (0)