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.

a component is mounted twice

See original GitHub issue

bug

What is the current behavior?

For the same component, componentDidMount is called multiple times, without calling componentWillUnmount , when using ReactDOM.unmountComponentAtNode .

You can use the link below to reproduce it:

https://codesandbox.io/s/x76mlw16rp

What is the expected behavior?

componentWillUnmount should be called before calling componentDidMount again.

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React?

16.x

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:28 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
michael8090commented, Sep 12, 2018

Yes, I checked yesterday and saw the legacyRenderSubtreeIntoContainer scheduled an unmout task and the callback () => container._reactRootContainer = null was set, but the following forceUpdate(triggered by ReactDOM.unstable_batchedUpdates(() => this.refs.a.forceUpdate()) somehow cancelled the real unmount task without removing the callback (that’s the root cause). The component did a regular update due to the forceUpdate, and the callback was called, resulting in the following updating logic thinking it was a fresh initial mount.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React Component Mounting Twice - Stack Overflow
I use NextJS and I had reactStrictMode: true, which causes each component to mount twice (on purpose, in dev, for debugging help.. not...
Read more >
React App mounting twice ? : r/react - Reddit
This is caused by rendering the component inside ReactStrict Mode, to fix this remove the `<React.StrictMode>` tag fromthe root of your app.
Read more >
App mounting twice in dev mode · Issue #12363 - GitHub
This is caused by rendering the component inside React Strict Mode, to fix this remove the <React.StrictMode> tag from the root of your...
Read more >
React Components rendered twice — any way to fix this?
Let's find out if there is a way to avoid this problem by trying different implementations. A) Functional Component with useState. function App()...
Read more >
How to avoid React componentDidMount being called ...
Are you seeing your React component calling the componentDidMount lifecycle multiple times? The answer to this problem is to avoid ...
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