Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/core/ReactCompositeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -1000,9 +1000,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(
Expand Down
32 changes: 32 additions & 0 deletions src/core/__tests__/ReactCompositeComponent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,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();
Expand Down