Skip to content

Commit 7d91277

Browse files
warn when shouldComponentUpdate() returns undefined
1 parent b66202e commit 7d91277

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/core/ReactCompositeComponent.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,9 +1000,22 @@ var ReactCompositeComponentMixin = {
10001000
this._pendingState = null;
10011001

10021002
try {
1003-
if (this._pendingForceUpdate ||
1004-
!this.shouldComponentUpdate ||
1005-
this.shouldComponentUpdate(nextProps, nextState, nextContext)) {
1003+
var shouldUpdate =
1004+
this._pendingForceUpdate ||
1005+
!this.shouldComponentUpdate ||
1006+
this.shouldComponentUpdate(nextProps, nextState, nextContext);
1007+
1008+
if (__DEV__) {
1009+
if (typeof shouldUpdate === "undefined") {
1010+
console.warn(
1011+
(this.constructor.displayName || 'ReactCompositeComponent') +
1012+
'.shouldComponentUpdate(): Returned undefined instead of a ' +
1013+
'boolean value. Make sure to return true or false.'
1014+
);
1015+
}
1016+
}
1017+
1018+
if (shouldUpdate) {
10061019
this._pendingForceUpdate = false;
10071020
// Will set `this.props`, `this.state` and `this.context`.
10081021
this._performComponentUpdate(

src/core/__tests__/ReactCompositeComponent-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -871,6 +871,38 @@ describe('ReactCompositeComponent', function() {
871871
expect(React.isValidClass(TrickFnComponent)).toBe(false);
872872
});
873873

874+
it('should warn when shouldComponentUpdate() returns undefined', function() {
875+
var warn = console.warn;
876+
console.warn = mocks.getMockFunction();
877+
878+
try {
879+
var Component = React.createClass({
880+
getInitialState: function () {
881+
return {bogus: false};
882+
},
883+
884+
shouldComponentUpdate: function() {
885+
return undefined;
886+
},
887+
888+
render: function() {
889+
return <div />;
890+
}
891+
});
892+
893+
var instance = ReactTestUtils.renderIntoDocument(<Component />);
894+
instance.setState({bogus: true});
895+
896+
expect(console.warn.mock.calls.length).toBe(1);
897+
expect(console.warn.mock.calls[0][0]).toBe(
898+
'Component.shouldComponentUpdate(): Returned undefined instead of a ' +
899+
'boolean value. Make sure to return true or false.'
900+
);
901+
} finally {
902+
console.warn = warn;
903+
}
904+
});
905+
874906
it('should warn when mispelling shouldComponentUpdate', function() {
875907
var warn = console.warn;
876908
console.warn = mocks.getMockFunction();

0 commit comments

Comments
 (0)