Throw when calling transferPropsTo() on a component you don't own

This is dangerous because it means that data is flowing into the component from two components, only one of which is the actual "owner". While we may be able to figure out how to
support this someday, let's be strict and prevent it for now.
This commit is contained in:
Pete Hunt
2013-10-07 15:07:04 -07:00
committed by Paul O’Shannessy
parent 27669c09ca
commit 325322898c
2 changed files with 32 additions and 1 deletions
+8
View File
@@ -19,6 +19,7 @@
"use strict";
var emptyFunction = require('emptyFunction');
var invariant = require('invariant');
var joinClasses = require('joinClasses');
var merge = require('merge');
@@ -90,6 +91,13 @@ var ReactPropTransferer = {
* @protected
*/
transferPropsTo: function(component) {
invariant(
component.props.__owner__ === this,
'You can\'t call transferPropsTo() on a component that you don\'t ' +
'own. This usually means you are calling transferPropsTo() on a ' +
'component passed in as props or children.'
);
var props = {};
for (var thatKey in component.props) {
if (component.props.hasOwnProperty(thatKey)) {
@@ -25,7 +25,7 @@ var reactComponentExpect;
var TestComponent;
describe('ReactCompositeComponent-transferProps', function() {
describe('ReactPropTransferer', function() {
beforeEach(function() {
React = require('React');
@@ -130,4 +130,27 @@ describe('ReactCompositeComponent-transferProps', function() {
ReactTestUtils.renderIntoDocument(<OuterOuterRefTestComponent />);
});
it('should not transferPropsTo() a component you don\'t own', function() {
var Parent = React.createClass({
render: function() {
return <Child><span /></Child>;
}
});
var Child = React.createClass({
render: function() {
return this.transferPropsTo(this.props.children);
}
});
expect(function() {
ReactTestUtils.renderIntoDocument(<Parent />);
}).toThrow(
'Invariant Violation: ' +
'You can\'t call transferPropsTo() on a component that you don\'t own. ' +
'This usually means you are calling transferPropsTo() on a component ' +
'passed in as props or children.'
);
});
});