@samitouri / QOS-React-2 / commits / 3ffbb4d096
Fork 0

The library for web and native user interfaces.

Re-add invariant

Bring back the invariant() that disallows setProps() and replaceProps() on owned components.

CommitSyncScript committed Jun 4, 2013 at 12:52 UTC 3ffbb4d096791e0b8f868adfd598a83e3296a671
2 files changed +36
src/core/ReactComponent.js
+8
@@ -235,6 +235,14 @@ var ReactComponent = {
235 * @public
236 */
237 replaceProps: function(props) {
238 + invariant(
239 + !this.props[OWNER],
240 + 'replaceProps(...): You called `setProps` or `replaceProps` on a ' +
241 + 'component with an owner. This is an anti-pattern since props will ' +
242 + 'get reactively updated when rendered. Instead, change the owner\'s ' +
243 + '`render` method to pass the correct value as props to the component ' +
244 + 'where it is created.'
245 + );
246 var transaction = ReactComponent.ReactReconcileTransaction.getPooled();
247 transaction.perform(this.receiveProps, this, props, transaction);
248 ReactComponent.ReactReconcileTransaction.release(transaction);
src/core/__tests__/ReactComponentLifeCycle-test.js
+28
@@ -375,6 +375,34 @@ describe('ReactComponentLifeCycle', function() {
375 expect(instance.state).toEqual(POST_WILL_UNMOUNT_STATE);
376 });
377
378 + it('should throw when calling setProps() on an owned component', function() {
379 + /**
380 + * calls setProps in an componentDidMount.
381 + */
382 + var PropsUpdaterInOnDOMReady = React.createClass({
383 + componentDidMount: function() {
384 + this.refs.theSimpleComponent.setProps({
385 + value: this.props.valueToUseInOnDOMReady
386 + });
387 + },
388 + render: function() {
389 + return (
390 + <input
391 + value={this.props.valueToUseInitially}
392 + ref="theSimpleComponent">
393 + </input>
394 + );
395 + }
396 + });
397 + var instance =
398 + <PropsUpdaterInOnDOMReady
399 + valueToUseInitially="hello"
400 + valueToUseInOnDOMReady="goodbye"
401 + />;
402 + expect(ReactTestUtils.renderIntoDocument.bind(ReactTestUtils, instance))
403 + .toThrow();
404 + });
405 +
406 it('should allow state updates in componentDidMount', function() {
407 /**
408 * calls setState in an componentDidMount.