top | item 30977996

(no title)

Gravey | 3 years ago

Afaik this has been the behavior of StrictMode since it was introduced in React 16.

One thing that’s called out in the linked docs is that duplicate console.logs were _deliberately_ silenced in React 17. I have no idea what they were thinking when they made this decision, but it sounds like it has been walked back in 18.

discuss

order

lioeters|3 years ago

Monkey-patching console.log sounds so hacky..

> In React 17, React automatically modifies the console methods like console.log() to silence the logs in the second call to lifecycle functions. However, it may cause undesired behavior in certain cases where a workaround can be used.

> Starting from React 18, React does not suppress any logs. However, if you have React DevTools installed, the logs from the second call will appear slightly dimmed. React DevTools also offers a setting (off by default) to suppress them completely.

marcus_cemes|3 years ago

Also, these types of global modifications make such libraries fundamentally incompatible with projects such as Tauri that focuse on safety and security. In a recent video of theirs they mentioned that of the popular frameworks, the only one that didn't seem to do any dodgy modifications of the global scope was Svelte (they didn't mention which frameworks failed the test, but I imagine React was one of them).

SirHound|3 years ago

It is, I have lost tens of hours to this over the last couple years.

tomduncalf|3 years ago

Interesting and an odd choice of behaviour in 17! I tested in a codesandbox with Next.js using React 17 and 18 and observed that only 18 did the double logging I was seeing, so assumed it was new behaviour in 18. Actually I guess I was seeing 17 silencing the double log.

jitl|3 years ago

React also already ran components twice when devtools is open, even without strict mode. This happens in my React 16 codebase with silenced console.log and no StrictMode in sight.

joshribakoff|3 years ago

I think you mean it logged log messages twice?

I don’t believe having devtools opens changes the runtime semantics of the app.that would defeat the point of the devtools.

I do know it annoyingly logs things twice

joshribakoff|3 years ago

The hacker news post doesn’t even link to the article about what’s new in react 18: https://reactjs.org/blog/2022/03/29/react-v18.html#new-stric...

> To help surface these issues, React 18 introduces a new development-only check to Strict Mode. This new check will automatically unmount and remount every component, whenever a component mounts for the first time, restoring the previous state on the second mount.

SirHound|3 years ago

It used to call render twice, now it runs effects twice also.

supermatt|3 years ago

It was like this even before strict mode was introduced.