warn when shouldComponentUpdate() returns undefined
Daniel Schonfeld committed
May 10, 2014 at 23:56 UTC
7d91277a5025cd08210d2362ade4ca92676a5fa2
2 files changed
+48
-3
src/core/ReactCompositeComponent.js
+16
-3
@@ -1000,9 +1000,22 @@ var ReactCompositeComponentMixin = {
1000
this._pendingState = null;
1001
1002
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) {
1019
this._pendingForceUpdate = false;
1020
// Will set `this.props`, `this.state` and `this.context`.
1021
this._performComponentUpdate(
src/core/__tests__/ReactCompositeComponent-test.js
+32
@@ -871,6 +871,38 @@ describe('ReactCompositeComponent', function() {
871
expect(React.isValidClass(TrickFnComponent)).toBe(false);
872
});
873
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
+
906
it('should warn when mispelling shouldComponentUpdate', function() {
907
var warn = console.warn;
908
console.warn = mocks.getMockFunction();