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

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

‎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
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‎

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
importinvariantfrom'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-
caseFunctionComponent: {
1397-
constComponent=returnFiber.type;
1398+
caseBlock:
1399+
caseFunctionComponent:
1400+
caseForwardRef:
1401+
caseSimpleMemoComponent: {
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
importinvariantfrom'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-
caseFunctionComponent: {
1389-
constComponent=returnFiber.type;
1390+
caseBlock:
1391+
caseFunctionComponent:
1392+
caseForwardRef:
1393+
caseSimpleMemoComponent: {
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)