mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
Re-add invariant
Bring back the invariant() that disallows setProps() and replaceProps() on owned components.
This commit is contained in:
committed by
Paul O’Shannessy
parent
ca5d7bc683
commit
3ffbb4d096
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user