Skip to content

Commit 754cc2c

Browse files
committed
Deduplicate warning on invalid callback (#11833)
1 parent 48833f6 commit 754cc2c

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

packages/react-dom/src/__tests__/ReactUpdates-test.js

+10
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,11 @@ describe('ReactUpdates', () => {
886886
'Invalid argument passed as callback. Expected a function. Instead ' +
887887
'received: [object Object]',
888888
);
889+
component = ReactTestUtils.renderIntoDocument(<A />);
890+
expect(() => component.setState({}, {a: 1, b: 2})).toThrowError(
891+
'Invalid argument passed as callback. Expected a function. Instead ' +
892+
'received: [object Object]',
893+
);
889894
});
890895

891896
it('throws in forceUpdate if the update callback is not a function', () => {
@@ -933,6 +938,11 @@ describe('ReactUpdates', () => {
933938
'Invalid argument passed as callback. Expected a function. Instead ' +
934939
'received: [object Object]',
935940
);
941+
component = ReactTestUtils.renderIntoDocument(<A />);
942+
expect(() => component.forceUpdate({a: 1, b: 2})).toThrowError(
943+
'Invalid argument passed as callback. Expected a function. Instead ' +
944+
'received: [object Object]',
945+
);
936946
});
937947

938948
it('does not update one component twice in a batch (#2410)', () => {

packages/react-reconciler/src/ReactFiberClassComponent.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,24 @@ let didWarnAboutStateAssignmentForComponent;
4444
let warnOnInvalidCallback;
4545

4646
if (__DEV__) {
47+
const didWarnOnInvalidCallback = {};
4748
didWarnAboutStateAssignmentForComponent = {};
4849

4950
warnOnInvalidCallback = function(callback: mixed, callerName: string) {
50-
warning(
51-
callback === null || typeof callback === 'function',
52-
'%s(...): Expected the last optional `callback` argument to be a ' +
53-
'function. Instead received: %s.',
54-
callerName,
55-
callback,
56-
);
51+
if (callback === null || typeof callback === 'function') {
52+
return;
53+
}
54+
const key = `${callerName}_${JSON.stringify(callback)}`;
55+
if (!didWarnOnInvalidCallback[key]) {
56+
warning(
57+
false,
58+
'%s(...): Expected the last optional `callback` argument to be a ' +
59+
'function. Instead received: %s.',
60+
callerName,
61+
callback,
62+
);
63+
didWarnOnInvalidCallback[key] = true;
64+
}
5765
};
5866

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

0 commit comments

Comments
 (0)