Throw an error when functions on `statics` clash due to duplicate keys
Charles Marsh committed
Aug 13, 2014 at 14:44 UTC
71e49adf35a19d57b4937f429e0902253edd7dfc
2 files changed
+35
-19
src/core/ReactCompositeComponent.js
+8
-16
@@ -556,22 +556,14 @@ function mixStaticSpecIntoComponent(Constructor, statics) {
556
}
557
558
var isInherited = name in Constructor;
559
- var result = property;
560
- if (isInherited) {
561
- var existingProperty = Constructor[name];
562
- var existingType = typeof existingProperty;
563
- var propertyType = typeof property;
564
- invariant(
565
- existingType === 'function' && propertyType === 'function',
566
- 'ReactCompositeComponent: You are attempting to define ' +
567
- '`%s` on your component more than once, but that is only supported ' +
568
- 'for functions, which are chained together. This conflict may be ' +
569
- 'due to a mixin.',
570
- name
571
- );
572
- result = createChainedFunction(existingProperty, property);
573
- }
574
- Constructor[name] = result;
559
+ invariant(
560
+ !isInherited,
561
+ 'ReactCompositeComponent: You are attempting to define ' +
562
+ '`%s` on your component more than once. This conflict may be ' +
563
+ 'due to a mixin.',
564
+ name
565
+ );
566
+ Constructor[name] = property;
567
}
568
}
569
src/core/__tests__/ReactCompositeComponent-test.js
+27
-3
@@ -1317,9 +1317,33 @@ describe('ReactCompositeComponent', function() {
1317
});
1318
}).toThrow(
1319
'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
1320
- 'define `abc` on your component more than once, but that is only ' +
1321
- 'supported for functions, which are chained together. This conflict ' +
1322
- 'may be due to a mixin.'
1320
+ 'define `abc` on your component more than once. This conflict may be ' +
1321
+ 'due to a mixin.'
1322
+ );
1323
+ });
1324
+
1325
+ it("should throw if mixins override functions in statics", function() {
1326
+ expect(function() {
1327
+ var Mixin = {
1328
+ statics: {
1329
+ abc: function() { console.log('foo'); }
1330
+ }
1331
+ };
1332
+ React.createClass({
1333
+ mixins: [Mixin],
1334
+
1335
+ statics: {
1336
+ abc: function() { console.log('bar'); }
1337
+ },
1338
+
1339
+ render: function() {
1340
+ return <span />;
1341
+ }
1342
+ });
1343
+ }).toThrow(
1344
+ 'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
1345
+ 'define `abc` on your component more than once. This conflict may be ' +
1346
+ 'due to a mixin.'
1347
);
1348
});
1349