mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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
This commit is contained in:
committed by
Brandon Dail
parent
895dca587b
commit
5518bd44a9
@@ -461,6 +461,7 @@ src/renderers/__tests__/ReactCompositeComponent-test.js
|
||||
* should call componentWillUnmount before unmounting
|
||||
* should warn when shouldComponentUpdate() returns undefined
|
||||
* should warn when componentDidUnmount method is defined
|
||||
* should warn when defaultProps was defined as an instance property
|
||||
* should pass context to children when not owner
|
||||
* should skip update when rerendering element in container
|
||||
* should pass context when re-rendered for static child
|
||||
|
||||
@@ -488,6 +488,29 @@ describe('ReactCompositeComponent', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should warn when defaultProps was defined as an instance property', () => {
|
||||
spyOn(console, 'error');
|
||||
|
||||
class Component extends React.Component {
|
||||
constructor(props) {
|
||||
super(props);
|
||||
this.defaultProps = {name: 'Abhay'};
|
||||
}
|
||||
|
||||
render() {
|
||||
return <div />;
|
||||
}
|
||||
}
|
||||
|
||||
ReactTestUtils.renderIntoDocument(<Component />);
|
||||
|
||||
expectDev(console.error.calls.count()).toBe(1);
|
||||
expectDev(console.error.calls.argsFor(0)[0]).toBe(
|
||||
'Warning: defaultProps was defined as an instance property ' +
|
||||
'on Component. Use a static property to define defaultProps instead.',
|
||||
);
|
||||
});
|
||||
|
||||
it('should pass context to children when not owner', () => {
|
||||
class Parent extends React.Component {
|
||||
render() {
|
||||
|
||||
@@ -228,6 +228,13 @@ module.exports = function(
|
||||
name,
|
||||
name,
|
||||
);
|
||||
const noInstanceDefaultProps = !instance.defaultProps;
|
||||
warning(
|
||||
noInstanceDefaultProps,
|
||||
'defaultProps was defined as an instance property on %s. Use a static ' +
|
||||
'property to define defaultProps instead.',
|
||||
name,
|
||||
);
|
||||
}
|
||||
|
||||
const state = instance.state;
|
||||
|
||||
@@ -299,6 +299,12 @@ var ReactCompositeComponent = {
|
||||
'componentWillRecieveProps(). Did you mean componentWillReceiveProps()?',
|
||||
this.getName() || 'A component',
|
||||
);
|
||||
warning(
|
||||
!inst.defaultProps,
|
||||
'defaultProps was defined as an instance property on %s. Use a static ' +
|
||||
'property to define defaultProps instead.',
|
||||
this.getName() || 'a component',
|
||||
);
|
||||
}
|
||||
|
||||
var initialState = inst.state;
|
||||
|
||||
Reference in New Issue
Block a user