@samitouri / QOS-React / commits / 65aeb70195

Deduplicate warning on invalid callback (#11833) (#11833)

Shi Yan committed Jan 7, 2018 at 19:52 UTC 65aeb701955521551848a8a9cc8e9f279ef2e73c
2 files changed +19 -19
packages/react-dom/src/__tests__/ReactUpdates-test.js
+4 -12
@@ -876,13 +876,9 @@ describe('ReactUpdates', () => {
876 'Invalid argument passed as callback. Expected a function. Instead ' +
877 'received: [object Object]',
878 );
879 + // Make sure the warning is deduplicated and doesn't fire again
880 component = ReactTestUtils.renderIntoDocument(<A />);
880 - expect(() => {
881 - expect(() => component.setState({}, new Foo())).toWarnDev(
882 - 'setState(...): Expected the last optional `callback` argument to be ' +
883 - 'a function. Instead received: [object Object].',
884 - );
885 - }).toThrowError(
881 + expect(() => component.setState({}, new Foo())).toThrowError(
882 'Invalid argument passed as callback. Expected a function. Instead ' +
883 'received: [object Object]',
884 );
@@ -923,13 +919,9 @@ describe('ReactUpdates', () => {
919 'Invalid argument passed as callback. Expected a function. Instead ' +
920 'received: [object Object]',
921 );
922 + // Make sure the warning is deduplicated and doesn't fire again
923 component = ReactTestUtils.renderIntoDocument(<A />);
927 - expect(() => {
928 - expect(() => component.forceUpdate(new Foo())).toWarnDev(
929 - 'forceUpdate(...): Expected the last optional `callback` argument to be ' +
930 - 'a function. Instead received: [object Object].',
931 - );
932 - }).toThrowError(
924 + expect(() => component.forceUpdate(new Foo())).toThrowError(
925 'Invalid argument passed as callback. Expected a function. Instead ' +
926 'received: [object Object]',
927 );
packages/react-reconciler/src/ReactFiberClassComponent.js
+15 -7
@@ -44,16 +44,24 @@ let didWarnAboutStateAssignmentForComponent;
44 let warnOnInvalidCallback;
45
46 if (__DEV__) {
47 + const didWarnOnInvalidCallback = {};
48 didWarnAboutStateAssignmentForComponent = {};
49
50 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}_${(callback: any)}`;
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 + }
65 };
66
67 // This is so gross but it's at least non-critical and can be removed if