Skip to content

Make undefined return value invariant a warning for memo and forwardRef #19575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/react-dom/src/__tests__/ReactEmptyComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,9 +323,7 @@ describe('ReactEmptyComponent', () => {

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

it('should warn about React.memo that returns undefined', () => {
Expand All @@ -334,6 +332,6 @@ describe('ReactEmptyComponent', () => {

expect(() => {
ReactTestUtils.renderIntoDocument(<EmptyMemo />);
}).toThrowError('Empty(...): Nothing was returned from render.');
}).toWarnDev('Empty(...): Nothing was returned from render.');
});
});
17 changes: 13 additions & 4 deletions packages/react-reconciler/src/ReactChildFiber.new.js
Original file line number Diff line number Diff line change
Expand Up @@ -1383,6 +1383,18 @@ function ChildReconciler(shouldTrackSideEffects) {
// component, throw an error. If Fiber return types are disabled,
// we already threw above.
switch (returnFiber.tag) {
case Block:
case ForwardRef:
case SimpleMemoComponent:
if (__DEV__) {
console.warn(
'%s(...): Nothing was returned from render. This usually means a ' +
'return statement is missing. Or, to render nothing, ' +
'return null.',
getComponentName(returnFiber.type) || 'Component',
);
}
break;
case ClassComponent: {
if (__DEV__) {
const instance = returnFiber.stateNode;
Expand All @@ -1395,10 +1407,7 @@ function ChildReconciler(shouldTrackSideEffects) {
// Intentionally fall through to the next case, which handles both
// functions and classes
// eslint-disable-next-lined no-fallthrough
case Block:
case FunctionComponent:
case ForwardRef:
case SimpleMemoComponent: {
case FunctionComponent: {
invariant(
false,
'%s(...): Nothing was returned from render. This usually means a ' +
Expand Down
17 changes: 13 additions & 4 deletions packages/react-reconciler/src/ReactChildFiber.old.js
Original file line number Diff line number Diff line change
Expand Up @@ -1375,6 +1375,18 @@ function ChildReconciler(shouldTrackSideEffects) {
// component, throw an error. If Fiber return types are disabled,
// we already threw above.
switch (returnFiber.tag) {
case Block:
case ForwardRef:
case SimpleMemoComponent:
if (__DEV__) {
console.warn(
'%s(...): Nothing was returned from render. This usually means a ' +
'return statement is missing. Or, to render nothing, ' +
'return null.',
getComponentName(returnFiber.type) || 'Component',
);
}
break;
case ClassComponent: {
if (__DEV__) {
const instance = returnFiber.stateNode;
Expand All @@ -1387,10 +1399,7 @@ function ChildReconciler(shouldTrackSideEffects) {
// Intentionally fall through to the next case, which handles both
// functions and classes
// eslint-disable-next-lined no-fallthrough
case Block:
case FunctionComponent:
case ForwardRef:
case SimpleMemoComponent: {
case FunctionComponent: {
invariant(
false,
'%s(...): Nothing was returned from render. This usually means a ' +
Expand Down