Skip to content

Commit

Permalink
Deduplicate warning on invalid callback (#11833)
Browse files Browse the repository at this point in the history
  • Loading branch information
yenshih committed Jan 6, 2018
1 parent 48833f6 commit 754cc2c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 10 additions & 0 deletions packages/react-dom/src/__tests__/ReactUpdates-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,11 @@ describe('ReactUpdates', () => {
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
component = ReactTestUtils.renderIntoDocument(<A />);
expect(() => component.setState({}, {a: 1, b: 2})).toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
});

it('throws in forceUpdate if the update callback is not a function', () => {
Expand Down Expand Up @@ -933,6 +938,11 @@ describe('ReactUpdates', () => {
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
component = ReactTestUtils.renderIntoDocument(<A />);
expect(() => component.forceUpdate({a: 1, b: 2})).toThrowError(
'Invalid argument passed as callback. Expected a function. Instead ' +
'received: [object Object]',
);
});

it('does not update one component twice in a batch (#2410)', () => {
Expand Down
22 changes: 15 additions & 7 deletions packages/react-reconciler/src/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,24 @@ let didWarnAboutStateAssignmentForComponent;
let warnOnInvalidCallback;

if (__DEV__) {
const didWarnOnInvalidCallback = {};
didWarnAboutStateAssignmentForComponent = {};

warnOnInvalidCallback = function(callback: mixed, callerName: string) {
warning(
callback === null || typeof callback === 'function',
'%s(...): Expected the last optional `callback` argument to be a ' +
'function. Instead received: %s.',
callerName,
callback,
);
if (callback === null || typeof callback === 'function') {
return;
}
const key = `${callerName}_${JSON.stringify(callback)}`;
if (!didWarnOnInvalidCallback[key]) {
warning(
false,
'%s(...): Expected the last optional `callback` argument to be a ' +
'function. Instead received: %s.',
callerName,
callback,
);
didWarnOnInvalidCallback[key] = true;
}
};

// This is so gross but it's at least non-critical and can be removed if
Expand Down

0 comments on commit 754cc2c

Please sign in to comment.