Skip to content

Commit 65a5435

Browse files
author
Brian Vaughn
committed
Warn about undefined return value for memo and forwardRef
1 parent 32ff428 commit 65a5435

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-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: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import {
2929
ClassComponent,
3030
HostText,
3131
HostPortal,
32+
ForwardRef,
3233
Fragment,
34+
MemoComponent,
35+
SimpleMemoComponent,
3336
Block,
3437
} from './ReactWorkTags';
3538
import invariant from 'shared/invariant';
@@ -1393,14 +1396,16 @@ function ChildReconciler(shouldTrackSideEffects) {
13931396
// Intentionally fall through to the next case, which handles both
13941397
// functions and classes
13951398
// eslint-disable-next-lined no-fallthrough
1396-
case FunctionComponent: {
1397-
const Component = returnFiber.type;
1399+
case FunctionComponent:
1400+
case MemoComponent:
1401+
case SimpleMemoComponent:
1402+
case ForwardRef: {
13981403
invariant(
13991404
false,
14001405
'%s(...): Nothing was returned from render. This usually means a ' +
14011406
'return statement is missing. Or, to render nothing, ' +
14021407
'return null.',
1403-
Component.displayName || Component.name || 'Component',
1408+
getComponentName(returnFiber.type) || 'Component',
14041409
);
14051410
}
14061411
}

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ import {
2929
ClassComponent,
3030
HostText,
3131
HostPortal,
32+
ForwardRef,
3233
Fragment,
34+
MemoComponent,
35+
SimpleMemoComponent,
3336
Block,
3437
} from './ReactWorkTags';
3538
import invariant from 'shared/invariant';
@@ -1385,14 +1388,16 @@ function ChildReconciler(shouldTrackSideEffects) {
13851388
// Intentionally fall through to the next case, which handles both
13861389
// functions and classes
13871390
// eslint-disable-next-lined no-fallthrough
1388-
case FunctionComponent: {
1389-
const Component = returnFiber.type;
1391+
case FunctionComponent:
1392+
case MemoComponent:
1393+
case SimpleMemoComponent:
1394+
case ForwardRef: {
13901395
invariant(
13911396
false,
13921397
'%s(...): Nothing was returned from render. This usually means a ' +
13931398
'return statement is missing. Or, to render nothing, ' +
13941399
'return null.',
1395-
Component.displayName || Component.name || 'Component',
1400+
getComponentName(returnFiber.type) || 'Component',
13961401
);
13971402
}
13981403
}

0 commit comments

Comments
 (0)