Add setProps/replaceProps warning
Ben Alpert committed
Jul 3, 2015 at 10:26 UTC
41aa3496aa632634f650edbe10d617799922d265
4 files changed
+26
-5
src/isomorphic/classic/class/ReactClass.js
+18
@@ -55,6 +55,18 @@ var SpecPolicy = keyMirror({
55
56
var injectedMixins = [];
57
58
+var warnedSetProps = false;
59
+function warnSetProps() {
60
+ if (!warnedSetProps) {
61
+ warnedSetProps = true;
62
+ warning(
63
+ false,
64
+ 'setProps(...) and replaceProps(...) are deprecated. ' +
65
+ 'Instead, call React.render again at the top level.'
66
+ );
67
+ }
68
+}
69
+
70
/**
71
* Composite components are higher-level components that compose other composite
72
* or native components.
@@ -739,6 +751,9 @@ var ReactClassMixin = {
751
* @deprecated
752
*/
753
setProps: function(partialProps, callback) {
754
+ if (__DEV__) {
755
+ warnSetProps();
756
+ }
757
this.updater.enqueueSetProps(this, partialProps);
758
if (callback) {
759
this.updater.enqueueCallback(this, callback);
@@ -755,6 +770,9 @@ var ReactClassMixin = {
770
* @deprecated
771
*/
772
replaceProps: function(newProps, callback) {
773
+ if (__DEV__) {
774
+ warnSetProps();
775
+ }
776
this.updater.enqueueReplaceProps(this, newProps);
777
if (callback) {
778
this.updater.enqueueCallback(this, callback);
src/renderers/shared/reconciler/__tests__/ReactComponentLifeCycle-test.js
+2
@@ -471,6 +471,7 @@ describe('ReactComponentLifeCycle', function() {
471
valueToUseInitially="hello"
472
valueToUseInOnDOMReady="goodbye"
473
/>;
474
+ spyOn(console, 'error');
475
expect(function() {
476
instance = ReactTestUtils.renderIntoDocument(instance);
477
}).toThrow(
@@ -480,6 +481,7 @@ describe('ReactComponentLifeCycle', function() {
481
'`render` method to pass the correct value as props to the component ' +
482
'where it is created.'
483
);
484
+ expect(console.error.calls.length).toBe(1); // setProps deprecated
485
});
486
487
it('should not throw when updating an auxiliary component', function() {
src/renderers/shared/reconciler/__tests__/ReactCompositeComponent-test.js
+3
-3
@@ -416,15 +416,15 @@ describe('ReactCompositeComponent', function() {
416
expect(function() {
417
instance.setProps({value: 1});
418
}).not.toThrow();
419
- expect(console.error.calls.length).toBe(0);
419
+ expect(console.error.calls.length).toBe(1); // setProps deprecated
420
421
React.unmountComponentAtNode(container);
422
expect(function() {
423
instance.setProps({value: 2});
424
}).not.toThrow();
425
426
- expect(console.error.calls.length).toBe(1);
427
- expect(console.error.argsForCall[0][0]).toBe(
426
+ expect(console.error.calls.length).toBe(2);
427
+ expect(console.error.argsForCall[1][0]).toBe(
428
'Warning: setProps(...): Can only update a mounted or ' +
429
'mounting component. This usually means you called setProps() on an ' +
430
'unmounted component. This is a no-op. Please check the code for the ' +
src/renderers/shared/reconciler/__tests__/ReactUpdates-test.js
+3
-2
@@ -95,12 +95,13 @@ describe('ReactUpdates', function() {
95
},
96
});
97
98
- var instance = ReactTestUtils.renderIntoDocument(<Component x={0} />);
98
+ var container = document.createElement('div');
99
+ var instance = React.render(<Component x={0} />, container);
100
expect(instance.props.x).toBe(0);
101
expect(instance.state.y).toBe(0);
102
103
ReactUpdates.batchedUpdates(function() {
103
- instance.setProps({x: 1});
104
+ React.render(<Component x={1} />, container)
105
instance.setState({y: 2});
106
expect(instance.props.x).toBe(0);
107
expect(instance.state.y).toBe(0);