Skip to content

Commit 95de877

Browse files
committed
Merge pull request #2036 from crm416/static-functions
Throw an error when functions on `statics` clash due to duplicate keys
2 parents f7c7676 + a70db00 commit 95de877

File tree

2 files changed

+68
-19
lines changed

2 files changed

+68
-19
lines changed

src/core/ReactCompositeComponent.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -575,23 +575,25 @@ function mixStaticSpecIntoComponent(Constructor, statics) {
575575
continue;
576576
}
577577

578+
var isReserved = name in RESERVED_SPEC_KEYS;
579+
invariant(
580+
!isReserved,
581+
'ReactCompositeComponent: You are attempting to define a reserved ' +
582+
'property, `%s`, that shouldn\'t be on the "statics" key. Define it ' +
583+
'as an instance property instead; it will still be accessible on the ' +
584+
'constructor.',
585+
name
586+
);
587+
578588
var isInherited = name in Constructor;
579-
var result = property;
580-
if (isInherited) {
581-
var existingProperty = Constructor[name];
582-
var existingType = typeof existingProperty;
583-
var propertyType = typeof property;
584-
invariant(
585-
existingType === 'function' && propertyType === 'function',
586-
'ReactCompositeComponent: You are attempting to define ' +
587-
'`%s` on your component more than once, but that is only supported ' +
588-
'for functions, which are chained together. This conflict may be ' +
589-
'due to a mixin.',
590-
name
591-
);
592-
result = createChainedFunction(existingProperty, property);
593-
}
594-
Constructor[name] = result;
589+
invariant(
590+
!isInherited,
591+
'ReactCompositeComponent: You are attempting to define ' +
592+
'`%s` on your component more than once. This conflict may be ' +
593+
'due to a mixin.',
594+
name
595+
);
596+
Constructor[name] = property;
595597
}
596598
}
597599

src/core/__tests__/ReactCompositeComponent-test.js

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,29 @@ describe('ReactCompositeComponent', function() {
12761276
expect(Component.pqr()).toBe(Component.type);
12771277
});
12781278

1279+
it('should throw if a reserved property is in statics', function() {
1280+
expect(function() {
1281+
React.createClass({
1282+
statics: {
1283+
getDefaultProps: function() {
1284+
return {
1285+
foo: 0
1286+
};
1287+
}
1288+
},
1289+
1290+
render: function() {
1291+
return <span />;
1292+
}
1293+
});
1294+
}).toThrow(
1295+
'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
1296+
'define a reserved property, `getDefaultProps`, that shouldn\'t be on ' +
1297+
'the "statics" key. Define it as an instance property instead; it ' +
1298+
'will still be accessible on the constructor.'
1299+
);
1300+
});
1301+
12791302
it('should support statics in mixins', function() {
12801303
var Mixin = {
12811304
statics: {
@@ -1321,9 +1344,33 @@ describe('ReactCompositeComponent', function() {
13211344
});
13221345
}).toThrow(
13231346
'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
1324-
'define `abc` on your component more than once, but that is only ' +
1325-
'supported for functions, which are chained together. This conflict ' +
1326-
'may be due to a mixin.'
1347+
'define `abc` on your component more than once. This conflict may be ' +
1348+
'due to a mixin.'
1349+
);
1350+
});
1351+
1352+
it("should throw if mixins override functions in statics", function() {
1353+
expect(function() {
1354+
var Mixin = {
1355+
statics: {
1356+
abc: function() { console.log('foo'); }
1357+
}
1358+
};
1359+
React.createClass({
1360+
mixins: [Mixin],
1361+
1362+
statics: {
1363+
abc: function() { console.log('bar'); }
1364+
},
1365+
1366+
render: function() {
1367+
return <span />;
1368+
}
1369+
});
1370+
}).toThrow(
1371+
'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
1372+
'define `abc` on your component more than once. This conflict may be ' +
1373+
'due to a mixin.'
13271374
);
13281375
});
13291376

0 commit comments

Comments
 (0)