Skip to content

Commit

Permalink
Warn about undefined return value for memo and forwardRef
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Vaughn committed Aug 6, 2020
1 parent 32ff428 commit 65a5435
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
20 changes: 20 additions & 0 deletions packages/react-dom/src/__tests__/ReactEmptyComponent-test.js
Expand Up @@ -316,4 +316,24 @@ describe('ReactEmptyComponent', () => {
const noscript2 = container.firstChild;
expect(noscript2).toBe(null);
});

it('should warn about React.forwardRef that returns undefined', () => {
const Empty = () => {};
const EmptyForwardRef = React.forwardRef(Empty);

expect(() => {
ReactTestUtils.renderIntoDocument(<EmptyForwardRef />);
}).toThrowError(
'ForwardRef(Empty)(...): Nothing was returned from render.',
);
});

it('should warn about React.memo that returns undefined', () => {
const Empty = () => {};
const EmptyMemo = React.memo(Empty);

expect(() => {
ReactTestUtils.renderIntoDocument(<EmptyMemo />);
}).toThrowError('Empty(...): Nothing was returned from render.');
});
});
11 changes: 8 additions & 3 deletions packages/react-reconciler/src/ReactChildFiber.new.js
Expand Up @@ -29,7 +29,10 @@ import {
ClassComponent,
HostText,
HostPortal,
ForwardRef,
Fragment,
MemoComponent,
SimpleMemoComponent,
Block,
} from './ReactWorkTags';
import invariant from 'shared/invariant';
Expand Down Expand Up @@ -1393,14 +1396,16 @@ function ChildReconciler(shouldTrackSideEffects) {
// Intentionally fall through to the next case, which handles both
// functions and classes
// eslint-disable-next-lined no-fallthrough
case FunctionComponent: {
const Component = returnFiber.type;
case FunctionComponent:
case MemoComponent:
case SimpleMemoComponent:
case ForwardRef: {
invariant(
false,
'%s(...): Nothing was returned from render. This usually means a ' +
'return statement is missing. Or, to render nothing, ' +
'return null.',
Component.displayName || Component.name || 'Component',
getComponentName(returnFiber.type) || 'Component',
);
}
}
Expand Down
11 changes: 8 additions & 3 deletions packages/react-reconciler/src/ReactChildFiber.old.js
Expand Up @@ -29,7 +29,10 @@ import {
ClassComponent,
HostText,
HostPortal,
ForwardRef,
Fragment,
MemoComponent,
SimpleMemoComponent,
Block,
} from './ReactWorkTags';
import invariant from 'shared/invariant';
Expand Down Expand Up @@ -1385,14 +1388,16 @@ function ChildReconciler(shouldTrackSideEffects) {
// Intentionally fall through to the next case, which handles both
// functions and classes
// eslint-disable-next-lined no-fallthrough
case FunctionComponent: {
const Component = returnFiber.type;
case FunctionComponent:
case MemoComponent:
case SimpleMemoComponent:
case ForwardRef: {
invariant(
false,
'%s(...): Nothing was returned from render. This usually means a ' +
'return statement is missing. Or, to render nothing, ' +
'return null.',
Component.displayName || Component.name || 'Component',
getComponentName(returnFiber.type) || 'Component',
);
}
}
Expand Down

0 comments on commit 65a5435

Please sign in to comment.