Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,23 +555,25 @@ function mixStaticSpecIntoComponent(Constructor, statics) {
continue;
}

var isReserved = name in RESERVED_SPEC_KEYS;
invariant(
!isReserved,
'ReactCompositeComponent: You are attempting to define a reserved ' +
'property, `%s`, that shouldn\'t be on the "statics" key. Define it ' +
'as an instance property instead; it will still be accessible on the ' +
'constructor.',
name
);

var isInherited = name in Constructor;
var result = property;
if (isInherited) {
var existingProperty = Constructor[name];
var existingType = typeof existingProperty;
var propertyType = typeof property;
invariant(
existingType === 'function' && propertyType === 'function',
'ReactCompositeComponent: You are attempting to define ' +
'`%s` on your component more than once, but that is only supported ' +
'for functions, which are chained together. This conflict may be ' +
'due to a mixin.',
name
);
result = createChainedFunction(existingProperty, property);
}
Constructor[name] = result;
invariant(
!isInherited,
'ReactCompositeComponent: You are attempting to define ' +
'`%s` on your component more than once. This conflict may be ' +
'due to a mixin.',
name
);
Constructor[name] = property;
}
}

Expand Down
53 changes: 50 additions & 3 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1272,6 +1272,29 @@ describe('ReactCompositeComponent', function() {
expect(Component.pqr()).toBe(Component.type);
});

it('should throw if a reserved property is in statics', function() {
expect(function() {
React.createClass({
statics: {
getDefaultProps: function() {
return {
foo: 0
};
}
},

render: function() {
return <span />;
}
});
}).toThrow(
'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
'define a reserved property, `getDefaultProps`, that shouldn\'t be on ' +
'the "statics" key. Define it as an instance property instead; it ' +
'will still be accessible on the constructor.'
);
});

it('should support statics in mixins', function() {
var Mixin = {
statics: {
Expand Down Expand Up @@ -1317,9 +1340,33 @@ describe('ReactCompositeComponent', function() {
});
}).toThrow(
'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
'define `abc` on your component more than once, but that is only ' +
'supported for functions, which are chained together. This conflict ' +
'may be due to a mixin.'
'define `abc` on your component more than once. This conflict may be ' +
'due to a mixin.'
);
});

it("should throw if mixins override functions in statics", function() {
expect(function() {
var Mixin = {
statics: {
abc: function() { console.log('foo'); }
}
};
React.createClass({
mixins: [Mixin],

statics: {
abc: function() { console.log('bar'); }
},

render: function() {
return <span />;
}
});
}).toThrow(
'Invariant Violation: ReactCompositeComponent: You are attempting to ' +
'define `abc` on your component more than once. This conflict may be ' +
'due to a mixin.'
);
});

Expand Down