Skip to content
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

Improve warning for old element objects #5003

Merged
merged 1 commit into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/addons/__tests__/ReactFragment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ describe('ReactFragment', function() {
);
});

it('should throw if a plain object looks like an old element', function() {
var oldEl = {_isReactElement: true, type: 'span', props: {}};
var container = document.createElement('div');
expect(() => ReactDOM.render(<div>{oldEl}</div>, container)).toThrow(
'Invariant Violation: Objects are not valid as a React child (found: ' +
'object with keys {_isReactElement, type, props}). It looks like ' +
'you\'re using an element created by a different version of React. ' +
'Make sure to use only one copy of React.'
);
});

it('warns for numeric keys on objects as children', function() {
spyOn(console, 'error');

Expand Down
15 changes: 11 additions & 4 deletions src/shared/utils/traverseAllChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,26 @@ function traverseAllChildrenImpl(
} else if (type === 'object') {
var addendum = '';
if (__DEV__) {
addendum =
' If you meant to render a collection of children, use an array ' +
'instead or wrap the object using createFragment(object) from the ' +
'React add-ons.';
if (children._isReactElement) {
addendum =
' It looks like you\'re using an element created by a different ' +
'version of React. Make sure to use only one copy of React.';
}
if (ReactCurrentOwner.current) {
var name = ReactCurrentOwner.current.getName();
if (name) {
addendum = ' Check the render method of `' + name + '`.';
addendum += ' Check the render method of `' + name + '`.';
}
}
}
var childrenString = String(children);
invariant(
false,
'Objects are not valid as a React child (found: %s). If you meant to ' +
'render a collection of children, use an array instead or wrap the ' +
'object using createFragment(object) from the React add-ons.%s',
'Objects are not valid as a React child (found: %s).%s',
childrenString === '[object Object]' ?
'object with keys {' + Object.keys(children).join(', ') + '}' :
childrenString,
Expand Down