Skip to content

Commit

Permalink
Throw when calling transferPropsTo() on a component you don't own
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
petehunt authored and zpao committed Oct 7, 2013
1 parent 27669c0 commit 3253228
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/core/ReactPropTransferer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"use strict";

var emptyFunction = require('emptyFunction');
var invariant = require('invariant');
var joinClasses = require('joinClasses');
var merge = require('merge');

Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var reactComponentExpect;

var TestComponent;

describe('ReactCompositeComponent-transferProps', function() {
describe('ReactPropTransferer', function() {

beforeEach(function() {
React = require('React');
Expand Down Expand Up @@ -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.'
);
});
});

0 comments on commit 3253228

Please sign in to comment.