Merge pull request #1513 from danielschonfeld/shouldcomponentupdate-boolean

Warn if shouldComponentUpdate returns anything other than true/false
This commit is contained in:
Paul O’Shannessy 2014-06-18 11:29:31 -07:00
commit 92e3384cd2
2 changed files with 48 additions and 3 deletions

View File

@ -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(

View File

@ -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();