mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
committed by
Paul O’Shannessy
parent
27669c09ca
commit
325322898c
@@ -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)) {
|
||||
|
||||
+24
-1
@@ -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.'
|
||||
);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user