Re-add invariant

Bring back the invariant() that disallows setProps() and replaceProps()
on owned components.
This commit is contained in:
CommitSyncScript
2013-06-06 14:29:44 -07:00
committed by Paul O’Shannessy
parent ca5d7bc683
commit 3ffbb4d096
2 changed files with 36 additions and 0 deletions
+8
View File
@@ -235,6 +235,14 @@ var ReactComponent = {
* @public
*/
replaceProps: function(props) {
invariant(
!this.props[OWNER],
'replaceProps(...): You called `setProps` or `replaceProps` on a ' +
'component with an owner. This is an anti-pattern since props will ' +
'get reactively updated when rendered. Instead, change the owner\'s ' +
'`render` method to pass the correct value as props to the component ' +
'where it is created.'
);
var transaction = ReactComponent.ReactReconcileTransaction.getPooled();
transaction.perform(this.receiveProps, this, props, transaction);
ReactComponent.ReactReconcileTransaction.release(transaction);
@@ -375,6 +375,34 @@ describe('ReactComponentLifeCycle', function() {
expect(instance.state).toEqual(POST_WILL_UNMOUNT_STATE);
});
it('should throw when calling setProps() on an owned component', function() {
/**
* calls setProps in an componentDidMount.
*/
var PropsUpdaterInOnDOMReady = React.createClass({
componentDidMount: function() {
this.refs.theSimpleComponent.setProps({
value: this.props.valueToUseInOnDOMReady
});
},
render: function() {
return (
<input
value={this.props.valueToUseInitially}
ref="theSimpleComponent">
</input>
);
}
});
var instance =
<PropsUpdaterInOnDOMReady
valueToUseInitially="hello"
valueToUseInOnDOMReady="goodbye"
/>;
expect(ReactTestUtils.renderIntoDocument.bind(ReactTestUtils, instance))
.toThrow();
});
it('should allow state updates in componentDidMount', function() {
/**
* calls setState in an componentDidMount.