Warn if a non-object value is used in ReactFragment.create
I made this mistake while upgrading a few callers. I'm leaving this as warning so that it is always safe to wrap any existing child in React.addons.createFragment without breaking prod. This makes upgrading easier.
Sebastian Markbage committed
Feb 10, 2015 at 09:44 UTC
093ab000850f92c157a772e24c5ee5659bf2f649
2 files changed
+17
src/addons/ReactFragment.js
+8
@@ -96,6 +96,14 @@ var ReactFragment = {
96
// of its properties.
97
create: function(object) {
98
if (__DEV__) {
99
+ if (typeof object !== 'object' || !object) {
100
+ warning(
101
+ false,
102
+ 'React.addons.createFragment only accepts a single object. Not %s',
103
+ object
104
+ );
105
+ return object;
106
+ }
107
if (canWarnForReactFragment) {
108
var proxy = {};
109
Object.defineProperty(proxy, fragmentKey, {
src/addons/__tests__/ReactFragment-test.js
+9
@@ -73,4 +73,13 @@ describe('ReactFragment', function() {
73
);
74
});
75
76
+ it('should warn if accessing any property on a fragment', function() {
77
+ spyOn(console, 'warn');
78
+ ReactFragment.create(null);
79
+ expect(console.warn.calls.length).toBe(1);
80
+ expect(console.warn.calls[0].args[0]).toContain(
81
+ 'React.addons.createFragment only accepts a single object. Not null'
82
+ );
83
+ });
84
+
85
});