Skip to content

Commit ab03f34

Browse files
committed
Merge pull request facebook#4951 from spicyj/facebookgh-4840
Improve traverseAllChildren object error message
2 parents 33e44f0 + 1922db1 commit ab03f34

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

src/addons/__tests__/ReactFragment-test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ describe('ReactFragment', function() {
3232
var element = <div>{[children]}</div>;
3333
var container = document.createElement('div');
3434
expect(() => ReactDOM.render(element, container)).toThrow(
35-
'Invariant Violation: Objects are not valid as a React child (found ' +
35+
'Invariant Violation: Objects are not valid as a React child (found: ' +
3636
'object with keys {x, y, z}). If you meant to render a collection of ' +
3737
'children, use an array instead or wrap the object using ' +
38-
'React.addons.createFragment(object).'
38+
'createFragment(object) from the React add-ons.'
3939
);
4040
});
4141

@@ -52,10 +52,11 @@ describe('ReactFragment', function() {
5252
}
5353
var container = document.createElement('div');
5454
expect(() => ReactDOM.render(<Foo />, container)).toThrow(
55-
'Invariant Violation: Objects are not valid as a React child (found ' +
55+
'Invariant Violation: Objects are not valid as a React child (found: ' +
5656
'object with keys {a, b, c}). If you meant to render a collection of ' +
5757
'children, use an array instead or wrap the object using ' +
58-
'React.addons.createFragment(object). Check the render method of `Foo`.'
58+
'createFragment(object) from the React add-ons. Check the render ' +
59+
'method of `Foo`.'
5960
);
6061
});
6162

src/shared/utils/__tests__/traverseAllChildren-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,4 +476,28 @@ describe('traverseAllChildren', function() {
476476
}
477477
});
478478

479+
it('should throw on object', function() {
480+
expect(function() {
481+
traverseAllChildren({a: 1, b: 2}, function() {}, null);
482+
}).toThrow(
483+
'Invariant Violation: Objects are not valid as a React child (found: ' +
484+
'object with keys {a, b}). If you meant to render a collection of ' +
485+
'children, use an array instead or wrap the object using ' +
486+
'createFragment(object) from the React add-ons.'
487+
);
488+
});
489+
490+
it('should throw on regex', function() {
491+
// Really, we care about dates (#4840) but those have nondeterministic
492+
// serialization (timezones) so let's test a regex instead:
493+
expect(function() {
494+
traverseAllChildren(/abc/, function() {}, null);
495+
}).toThrow(
496+
'Invariant Violation: Objects are not valid as a React child (found: ' +
497+
'/abc/). If you meant to render a collection of children, use an array ' +
498+
'instead or wrap the object using createFragment(object) from the ' +
499+
'React add-ons.'
500+
);
501+
});
502+
479503
});

src/shared/utils/traverseAllChildren.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,13 +188,15 @@ function traverseAllChildrenImpl(
188188
}
189189
}
190190
}
191+
var childrenString = String(children);
191192
invariant(
192193
false,
193-
'Objects are not valid as a React child (found object with keys ' +
194-
'{%s}). If you meant to render a collection of children, use an ' +
195-
'array instead or wrap the object using ' +
196-
'React.addons.createFragment(object).%s',
197-
Object.keys(children).join(', '),
194+
'Objects are not valid as a React child (found: %s). If you meant to ' +
195+
'render a collection of children, use an array instead or wrap the ' +
196+
'object using createFragment(object) from the React add-ons.%s',
197+
childrenString === '[object Object]' ?
198+
'object with keys {' + Object.keys(children).join(', ') + '}' :
199+
childrenString,
198200
addendum
199201
);
200202
}

0 commit comments

Comments
 (0)