Merge pull request #1513 from danielschonfeld/shouldcomponentupdate-boolean
Warn if shouldComponentUpdate returns anything other than true/false
This commit is contained in:
commit
92e3384cd2
|
@ -1005,9 +1005,22 @@ var ReactCompositeComponentMixin = {
|
|||
this._pendingState = null;
|
||||
|
||||
try {
|
||||
if (this._pendingForceUpdate ||
|
||||
!this.shouldComponentUpdate ||
|
||||
this.shouldComponentUpdate(nextProps, nextState, nextContext)) {
|
||||
var shouldUpdate =
|
||||
this._pendingForceUpdate ||
|
||||
!this.shouldComponentUpdate ||
|
||||
this.shouldComponentUpdate(nextProps, nextState, nextContext);
|
||||
|
||||
if (__DEV__) {
|
||||
if (typeof shouldUpdate === "undefined") {
|
||||
console.warn(
|
||||
(this.constructor.displayName || 'ReactCompositeComponent') +
|
||||
'.shouldComponentUpdate(): Returned undefined instead of a ' +
|
||||
'boolean value. Make sure to return true or false.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldUpdate) {
|
||||
this._pendingForceUpdate = false;
|
||||
// Will set `this.props`, `this.state` and `this.context`.
|
||||
this._performComponentUpdate(
|
||||
|
|
|
@ -894,6 +894,38 @@ describe('ReactCompositeComponent', function() {
|
|||
expect(React.isValidClass(TrickFnComponent)).toBe(false);
|
||||
});
|
||||
|
||||
it('should warn when shouldComponentUpdate() returns undefined', function() {
|
||||
var warn = console.warn;
|
||||
console.warn = mocks.getMockFunction();
|
||||
|
||||
try {
|
||||
var Component = React.createClass({
|
||||
getInitialState: function () {
|
||||
return {bogus: false};
|
||||
},
|
||||
|
||||
shouldComponentUpdate: function() {
|
||||
return undefined;
|
||||
},
|
||||
|
||||
render: function() {
|
||||
return <div />;
|
||||
}
|
||||
});
|
||||
|
||||
var instance = ReactTestUtils.renderIntoDocument(<Component />);
|
||||
instance.setState({bogus: true});
|
||||
|
||||
expect(console.warn.mock.calls.length).toBe(1);
|
||||
expect(console.warn.mock.calls[0][0]).toBe(
|
||||
'Component.shouldComponentUpdate(): Returned undefined instead of a ' +
|
||||
'boolean value. Make sure to return true or false.'
|
||||
);
|
||||
} finally {
|
||||
console.warn = warn;
|
||||
}
|
||||
});
|
||||
|
||||
it('should warn when mispelling shouldComponentUpdate', function() {
|
||||
var warn = console.warn;
|
||||
console.warn = mocks.getMockFunction();
|
||||
|
|
Loading…
Reference in New Issue