Skip to content

Commit 057e4a0

Browse files
committed
Disable console log during the second rerender
1 parent b225d4f commit 057e4a0

File tree

5 files changed

+144
-43
lines changed

5 files changed

+144
-43
lines changed

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ describe('ReactTestUtils.act()', () => {
121121
);
122122
}).toErrorDev([
123123
'An update to App ran an effect, but was not wrapped in act(...)',
124-
'An update to App ran an effect, but was not wrapped in act(...)',
125124
]);
126125
});
127126

@@ -132,7 +131,6 @@ describe('ReactTestUtils.act()', () => {
132131
Scheduler.unstable_flushAll();
133132
}).toErrorDev([
134133
'An update to App ran an effect, but was not wrapped in act(...)',
135-
'An update to App ran an effect, but was not wrapped in act(...)',
136134
]);
137135
});
138136

@@ -143,7 +141,6 @@ describe('ReactTestUtils.act()', () => {
143141
Scheduler.unstable_flushAll();
144142
}).toErrorDev([
145143
'An update to App ran an effect, but was not wrapped in act(...)',
146-
'An update to App ran an effect, but was not wrapped in act(...)',
147144
]);
148145
});
149146
});

packages/react-reconciler/src/ReactFiberBeginWork.js

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ import {
181181
getWorkInProgressRoot,
182182
} from './ReactFiberWorkLoop';
183183

184+
import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';
185+
184186
const ReactCurrentOwner = ReactSharedInternals.ReactCurrentOwner;
185187

186188
let didReceiveUpdate: boolean = false;
@@ -320,14 +322,19 @@ function updateForwardRef(
320322
debugRenderPhaseSideEffectsForStrictMode &&
321323
workInProgress.mode & StrictMode
322324
) {
323-
nextChildren = renderWithHooks(
324-
current,
325-
workInProgress,
326-
render,
327-
nextProps,
328-
ref,
329-
renderExpirationTime,
330-
);
325+
disableLogs();
326+
try {
327+
nextChildren = renderWithHooks(
328+
current,
329+
workInProgress,
330+
render,
331+
nextProps,
332+
ref,
333+
renderExpirationTime,
334+
);
335+
} finally {
336+
reenableLogs();
337+
}
331338
}
332339
setIsRendering(false);
333340
} else {
@@ -658,14 +665,19 @@ function updateFunctionComponent(
658665
debugRenderPhaseSideEffectsForStrictMode &&
659666
workInProgress.mode & StrictMode
660667
) {
661-
nextChildren = renderWithHooks(
662-
current,
663-
workInProgress,
664-
Component,
665-
nextProps,
666-
context,
667-
renderExpirationTime,
668-
);
668+
disableLogs();
669+
try {
670+
nextChildren = renderWithHooks(
671+
current,
672+
workInProgress,
673+
Component,
674+
nextProps,
675+
context,
676+
renderExpirationTime,
677+
);
678+
} finally {
679+
reenableLogs();
680+
}
669681
}
670682
setIsRendering(false);
671683
} else {
@@ -731,14 +743,19 @@ function updateBlock<Props, Data>(
731743
debugRenderPhaseSideEffectsForStrictMode &&
732744
workInProgress.mode & StrictMode
733745
) {
734-
nextChildren = renderWithHooks(
735-
current,
736-
workInProgress,
737-
render,
738-
nextProps,
739-
data,
740-
renderExpirationTime,
741-
);
746+
disableLogs();
747+
try {
748+
nextChildren = renderWithHooks(
749+
current,
750+
workInProgress,
751+
render,
752+
nextProps,
753+
data,
754+
renderExpirationTime,
755+
);
756+
} finally {
757+
reenableLogs();
758+
}
742759
}
743760
setIsRendering(false);
744761
} else {
@@ -923,7 +940,12 @@ function finishClassComponent(
923940
debugRenderPhaseSideEffectsForStrictMode &&
924941
workInProgress.mode & StrictMode
925942
) {
926-
instance.render();
943+
disableLogs();
944+
try {
945+
instance.render();
946+
} finally {
947+
reenableLogs();
948+
}
927949
}
928950
setIsRendering(false);
929951
} else {
@@ -1485,14 +1507,19 @@ function mountIndeterminateComponent(
14851507
debugRenderPhaseSideEffectsForStrictMode &&
14861508
workInProgress.mode & StrictMode
14871509
) {
1488-
value = renderWithHooks(
1489-
null,
1490-
workInProgress,
1491-
Component,
1492-
props,
1493-
context,
1494-
renderExpirationTime,
1495-
);
1510+
disableLogs();
1511+
try {
1512+
value = renderWithHooks(
1513+
null,
1514+
workInProgress,
1515+
Component,
1516+
props,
1517+
context,
1518+
renderExpirationTime,
1519+
);
1520+
} finally {
1521+
reenableLogs();
1522+
}
14961523
}
14971524
}
14981525
reconcileChildren(null, workInProgress, value, renderExpirationTime);

packages/react-reconciler/src/ReactFiberClassComponent.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ import {
5656
} from './ReactFiberWorkLoop';
5757
import {requestCurrentSuspenseConfig} from './ReactFiberSuspenseConfig';
5858

59+
import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';
60+
5961
const fakeInternalInstance = {};
6062
const isArray = Array.isArray;
6163

@@ -151,8 +153,13 @@ export function applyDerivedStateFromProps(
151153
debugRenderPhaseSideEffectsForStrictMode &&
152154
workInProgress.mode & StrictMode
153155
) {
154-
// Invoke the function an extra time to help detect side-effects.
155-
getDerivedStateFromProps(nextProps, prevState);
156+
disableLogs();
157+
try {
158+
// Invoke the function an extra time to help detect side-effects.
159+
getDerivedStateFromProps(nextProps, prevState);
160+
} finally {
161+
reenableLogs();
162+
}
156163
}
157164
}
158165

@@ -266,8 +273,13 @@ function checkShouldComponentUpdate(
266273
debugRenderPhaseSideEffectsForStrictMode &&
267274
workInProgress.mode & StrictMode
268275
) {
269-
// Invoke the function an extra time to help detect side-effects.
270-
instance.shouldComponentUpdate(newProps, newState, nextContext);
276+
disableLogs();
277+
try {
278+
// Invoke the function an extra time to help detect side-effects.
279+
instance.shouldComponentUpdate(newProps, newState, nextContext);
280+
} finally {
281+
reenableLogs();
282+
}
271283
}
272284
}
273285
const shouldUpdate = instance.shouldComponentUpdate(
@@ -598,7 +610,12 @@ function constructClassInstance(
598610
debugRenderPhaseSideEffectsForStrictMode &&
599611
workInProgress.mode & StrictMode
600612
) {
601-
new ctor(props, context); // eslint-disable-line no-new
613+
disableLogs();
614+
try {
615+
new ctor(props, context); // eslint-disable-line no-new
616+
} finally {
617+
reenableLogs();
618+
}
602619
}
603620
}
604621

packages/react-reconciler/src/ReactUpdateQueue.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ import {
107107
import invariant from 'shared/invariant';
108108
import {getCurrentPriorityLevel} from './SchedulerWithReactIntegration';
109109

110+
import {disableLogs, reenableLogs} from 'shared/ConsolePatchingDev';
111+
110112
export type Update<State> = {|
111113
expirationTime: ExpirationTime,
112114
suspenseConfig: null | SuspenseConfig,
@@ -336,7 +338,12 @@ function getStateFromUpdate<State>(
336338
debugRenderPhaseSideEffectsForStrictMode &&
337339
workInProgress.mode & StrictMode
338340
) {
339-
payload.call(instance, prevState, nextProps);
341+
disableLogs();
342+
try {
343+
payload.call(instance, prevState, nextProps);
344+
} finally {
345+
reenableLogs();
346+
}
340347
}
341348
}
342349
const nextState = payload.call(instance, prevState, nextProps);
@@ -364,7 +371,12 @@ function getStateFromUpdate<State>(
364371
debugRenderPhaseSideEffectsForStrictMode &&
365372
workInProgress.mode & StrictMode
366373
) {
367-
payload.call(instance, prevState, nextProps);
374+
disableLogs();
375+
try {
376+
payload.call(instance, prevState, nextProps);
377+
} finally {
378+
reenableLogs();
379+
}
368380
}
369381
}
370382
partialState = payload.call(instance, prevState, nextProps);

packages/shared/ConsolePatchingDev.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
// Helpers to patch console.logs to avoid logging during side-effect free
11+
// replaying on render function. This currently only patches the object
12+
// lazily which won't cover if the log function was extracted eagerly.
13+
// We could also eagerly patch the method.
14+
15+
let prevLog;
16+
let prevInfo;
17+
let prevWarn;
18+
let prevError;
19+
20+
function disabledLog() {}
21+
22+
export function disableLogs(): void {
23+
if (__DEV__) {
24+
/* eslint-disable react-internal/no-production-logging */
25+
prevLog = console.log;
26+
prevInfo = console.info;
27+
prevWarn = console.warn;
28+
prevError = console.error;
29+
// $FlowFixMe Flow thinks console is immutable.
30+
console.log = console.info = console.warn = console.error = disabledLog;
31+
/* eslint-enable react-internal/no-production-logging */
32+
}
33+
}
34+
35+
export function reenableLogs(): void {
36+
if (__DEV__) {
37+
/* eslint-disable react-internal/no-production-logging */
38+
// $FlowFixMe Flow thinks console is immutable.
39+
console.log = prevLog;
40+
// $FlowFixMe Flow thinks console is immutable.
41+
console.info = prevInfo;
42+
// $FlowFixMe Flow thinks console is immutable.
43+
console.warn = prevWarn;
44+
// $FlowFixMe Flow thinks console is immutable.
45+
console.error = prevError;
46+
/* eslint-enable react-internal/no-production-logging */
47+
}
48+
}

0 commit comments

Comments
 (0)