Skip to content

Commit 5c68d1d

Browse files
Match error message to one in ReactFiber.js
1 parent 4eed18d commit 5c68d1d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

packages/react-dom/src/__tests__/ReactDOMServerIntegrationElements-test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,21 @@ describe('ReactDOMServerIntegration', () => {
870870
'an array instead.'
871871
: ''),
872872
);
873+
874+
itThrowsWhenRendering(
875+
'badly-typed elements',
876+
async render => {
877+
spyOnDev(console, 'error');
878+
const EmptyComponent = {};
879+
await render(<EmptyComponent />);
880+
},
881+
'Element type is invalid: expected a string (for built-in components) or a class/function ' +
882+
'(for composite components) but got: object.' +
883+
(__DEV__
884+
? " You likely forgot to export your component from the file it's defined in, " +
885+
'or you might have mixed up default and named imports.'
886+
: ''),
887+
);
873888
});
874889
});
875890
});

packages/react-dom/src/server/ReactPartialRenderer.js

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,12 +889,33 @@ class ReactDOMServerRenderer {
889889
break;
890890
}
891891
}
892+
893+
let info = '';
894+
if (__DEV__) {
895+
const owner = elementType._owner;
896+
if (
897+
elementType === undefined ||
898+
(typeof elementType === 'object' &&
899+
elementType !== null &&
900+
Object.keys(elementType).length === 0)
901+
) {
902+
info +=
903+
' You likely forgot to export your component from the file ' +
904+
"it's defined in, or you might have mixed up default and " +
905+
'named imports.';
906+
}
907+
const ownerName = owner ? getComponentName(owner) : null;
908+
if (ownerName) {
909+
info += '\n\nCheck the render method of `' + ownerName + '`.';
910+
}
911+
}
892912
invariant(
893913
false,
894914
'Element type is invalid: expected a string (for built-in ' +
895915
'components) or a class/function (for composite components) ' +
896-
'but got: %s.',
916+
'but got: %s.%s',
897917
elementType == null ? elementType : typeof elementType,
918+
info,
898919
);
899920
}
900921
}

0 commit comments

Comments
 (0)