react/packages/shared/ConsolePatchingDev.js
Sebastian Markbåge 5474a83e25
Disable console.logs in the second render pass of DEV mode double render (#18547)
* Disable console log during the second rerender

* Use the disabled log to avoid double yielding values in scheduler mock

* Reenable debugRenderPhaseSideEffectsForStrictMode in tests that can
2020-04-08 16:43:51 -07:00

49 lines
1.5 KiB
JavaScript

/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
// Helpers to patch console.logs to avoid logging during side-effect free
// replaying on render function. This currently only patches the object
// lazily which won't cover if the log function was extracted eagerly.
// We could also eagerly patch the method.
let prevLog;
let prevInfo;
let prevWarn;
let prevError;
function disabledLog() {}
export function disableLogs(): void {
if (__DEV__) {
/* eslint-disable react-internal/no-production-logging */
prevLog = console.log;
prevInfo = console.info;
prevWarn = console.warn;
prevError = console.error;
// $FlowFixMe Flow thinks console is immutable.
console.log = console.info = console.warn = console.error = disabledLog;
/* eslint-enable react-internal/no-production-logging */
}
}
export function reenableLogs(): void {
if (__DEV__) {
/* eslint-disable react-internal/no-production-logging */
// $FlowFixMe Flow thinks console is immutable.
console.log = prevLog;
// $FlowFixMe Flow thinks console is immutable.
console.info = prevInfo;
// $FlowFixMe Flow thinks console is immutable.
console.warn = prevWarn;
// $FlowFixMe Flow thinks console is immutable.
console.error = prevError;
/* eslint-enable react-internal/no-production-logging */
}
}