Bug: React hijacks console functions that may cause unpredictable behavior.
See original GitHub issueReact version: 17.0.0
Steps To Reproduce
- Write the following code below:
import React from "react"; import ReactDOM from "react-dom"; let i = 0; const origLog = console.log.bind(console); const App = () => { const [a, setA] = React.useState(0); origLog(i); console.log(i + ': hijacked log'); origLog("test"); i++; return ( <button onClick={() => { setA(a + 1); }} > click me </button> ); }; const rootElement = document.getElementById("root"); ReactDOM.render( <React.StrictMode> <App /> </React.StrictMode>, rootElement );
- While the
origLog()
function is called in every call toApp()
, the “hijacked” calls toconsole.log()
would only call the original method every other time, and therefore only even numbers are logged by this function.
Link to code example:
https://codesandbox.io/s/jolly-bush-4ql7z?file=/src/index.js
The current behavior
React automatically “hijacks” the console’s methods to prevent duplicated logs, which might cause unpredictable behaviors.
The expected behavior
The original console methods are called by default even if in the component code.
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Be careful with console.log when using React.StrictMode
It turns out that React is hijacking console. log and is replacing it with a function that does nothing on the second render...
Read more >Console.log using react - it returns Syntax error
In React there is a pattern when you can destructure an object's properties, and pass them as props to a Component ...
Read more >Security alerts - a reference guide
This article lists the security alerts you might get from Microsoft Defender ... This could be legitimate activity, or an indication of a...
Read more >Asynchronous Operations in React-Redux
Setting up asynchronous operations in React can be quite a challenge. This tutorial explains how to use Redux-Thunk to do much of this...
Read more >Testing Guide
Test Number of Times a Function Can be Used Limits (OTG-BUSLOGIC-005). Testing for the Circumvention of ... date the application for unexpected behavior....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
This is intended behavior (introduced in https://github.com/facebook/react/pull/18547) that will be opt-out once https://github.com/facebook/react/pull/19710 lands in React devtools. Until then you can use a local workaround explained in https://github.com/facebook/react/issues/20090#issuecomment-715927125.
To follow up on this, we’ve changed the logging behavior in 18 to be more intuitive. https://github.com/facebook/react/issues/21783#issuecomment-1083412766