question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: React hijacks console functions that may cause unpredictable behavior.

See original GitHub issue

React version: 17.0.0

Steps To Reproduce

  1. 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
    );
    
  2. While the origLog() function is called in every call to App(), the “hijacked” calls to console.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:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
eps1loncommented, Dec 2, 2020

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.

0reactions
gaearoncommented, Mar 30, 2022

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found