Warning added if defaultProps were defined as an instance property (#9433)
* Added warning when defaultProps was defined as an instance property * Added testcases to check warning message for defaultProps * Update fiber tests
Abhay Nikam committed
Apr 21, 2017 at 20:16 UTC
5518bd44a9bdd573ec33f9f2af9d91ea90dedc90
4 files changed
+37
scripts/fiber/tests-passing.txt
+1
@@ -461,6 +461,7 @@ src/renderers/__tests__/ReactCompositeComponent-test.js
461
* should call componentWillUnmount before unmounting
462
* should warn when shouldComponentUpdate() returns undefined
463
* should warn when componentDidUnmount method is defined
464
+* should warn when defaultProps was defined as an instance property
465
* should pass context to children when not owner
466
* should skip update when rerendering element in container
467
* should pass context when re-rendered for static child
src/renderers/__tests__/ReactCompositeComponent-test.js
+23
@@ -488,6 +488,29 @@ describe('ReactCompositeComponent', () => {
488
);
489
});
490
491
+ it('should warn when defaultProps was defined as an instance property', () => {
492
+ spyOn(console, 'error');
493
+
494
+ class Component extends React.Component {
495
+ constructor(props) {
496
+ super(props);
497
+ this.defaultProps = {name: 'Abhay'};
498
+ }
499
+
500
+ render() {
501
+ return <div />;
502
+ }
503
+ }
504
+
505
+ ReactTestUtils.renderIntoDocument(<Component />);
506
+
507
+ expectDev(console.error.calls.count()).toBe(1);
508
+ expectDev(console.error.calls.argsFor(0)[0]).toBe(
509
+ 'Warning: defaultProps was defined as an instance property ' +
510
+ 'on Component. Use a static property to define defaultProps instead.',
511
+ );
512
+ });
513
+
514
it('should pass context to children when not owner', () => {
515
class Parent extends React.Component {
516
render() {
src/renderers/shared/fiber/ReactFiberClassComponent.js
+7
@@ -228,6 +228,13 @@ module.exports = function(
228
name,
229
name,
230
);
231
+ const noInstanceDefaultProps = !instance.defaultProps;
232
+ warning(
233
+ noInstanceDefaultProps,
234
+ 'defaultProps was defined as an instance property on %s. Use a static ' +
235
+ 'property to define defaultProps instead.',
236
+ name,
237
+ );
238
}
239
240
const state = instance.state;
src/renderers/shared/stack/reconciler/ReactCompositeComponent.js
+6
@@ -299,6 +299,12 @@ var ReactCompositeComponent = {
299
'componentWillRecieveProps(). Did you mean componentWillReceiveProps()?',
300
this.getName() || 'A component',
301
);
302
+ warning(
303
+ !inst.defaultProps,
304
+ 'defaultProps was defined as an instance property on %s. Use a static ' +
305
+ 'property to define defaultProps instead.',
306
+ this.getName() || 'a component',
307
+ );
308
}
309
310
var initialState = inst.state;