Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
infinnie opened this issue Dec 2, 2020 · 6 comments
Closed

Comments

@infinnie
Copy link

infinnie commented Dec 2, 2020

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.

@infinnie infinnie added the Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug label Dec 2, 2020
@eps1lon
Copy link
Collaborator

eps1lon commented Dec 2, 2020

This is intended behavior (introduced in #18547) that will be opt-out once #19710 lands in React devtools. Until then you can use a local workaround explained in #20090 (comment).

@eps1lon eps1lon added Resolution: Expected Behavior and removed Status: Unconfirmed A potential issue that we haven't yet confirmed as a bug labels Dec 2, 2020
@eps1lon eps1lon closed this as completed Dec 2, 2020
@infinnie
Copy link
Author

infinnie commented Dec 3, 2020

This is intended behavior (introduced in #18547) that will be opt-out once #19710 lands in React devtools. Until then you can use a local workaround explained in #20090 (comment).

But that is bad—It’s been tripping users up like what has been posted in comments on the original pull request:

At least, were the console suppression to be kept, it should be explicitly stated in the documentation.

@gaearon
Copy link
Collaborator

gaearon commented Dec 3, 2020

Yeah we should definitely add it to StrictMode documentation. Would you be interested in adding a note there?

It’s been tripping users up like what have been posted in comments on the original pull request:

Yes, we understand it would trip users. Both behaviors trip users in different ways. From our experience so far, it is still net positive to silence the noise from the second log, but it does come with the tradeoff of confusing other people in other situations once in a while.

@infinnie
Copy link
Author

infinnie commented Dec 3, 2020

Yeah we should definitely add it to StrictMode documentation. Would you be interested in adding a note there?

It’s been tripping users up like what have been posted in comments on the original pull request:

Yes, we understand it would trip users. Both behaviors trip users in different ways. From our experience so far, it is still net positive to silence the noise from the second log, but it does come with the tradeoff of confusing other people in other situations once in a while.

A pull request is opened in reactjs/react.dev#3426.

@eps1lon
Copy link
Collaborator

eps1lon commented Dec 3, 2020

But that is bad—It’s been tripping users up like what has been posted in comments on the original pull request:

The previous behavior has as well. Neither state is perfect which is why the devtools option is on the table.

At least, were the console suppression to be kept, it should be explicitly stated in the documentation.

Sounds great. Though we had the previous behavior documented as well and people were confused by it as well. This will just take some time until this knowledge proliferated enough through the community.

@gaearon
Copy link
Collaborator

gaearon commented Mar 30, 2022

To follow up on this, we've changed the logging behavior in 18 to be more intuitive.
#21783 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants