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

+20
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,24 @@ describe('ReactEmptyComponent', () => {
316316
constnoscript2=container.firstChild;
317317
expect(noscript2).toBe(null);
318318
});
319+
320+
it('should warn about React.forwardRef that returns undefined',()=>{
321+
constEmpty=()=>{};
322+
constEmptyForwardRef=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+
constEmpty=()=>{};
333+
constEmptyMemo=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

+8-3
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
importinvariantfrom'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-
caseFunctionComponent: {
1397-
constComponent=returnFiber.type;
1399+
caseFunctionComponent:
1400+
caseMemoComponent:
1401+
caseSimpleMemoComponent:
1402+
caseForwardRef: {
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

+8-3
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
importinvariantfrom'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-
caseFunctionComponent: {
1389-
constComponent=returnFiber.type;
1391+
caseFunctionComponent:
1392+
caseMemoComponent:
1393+
caseSimpleMemoComponent:
1394+
caseForwardRef: {
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)
close