a component is mounted twice
See original GitHub issuebug
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:
- Created 5 years ago
- Comments:28 (6 by maintainers)
Top 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 >
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

Yes, I checked yesterday and saw the
legacyRenderSubtreeIntoContainerscheduled anunmouttask and the callback() => container._reactRootContainer = nullwas set, but the followingforceUpdate(triggered byReactDOM.unstable_batchedUpdates(() => this.refs.a.forceUpdate()) somehow cancelled the realunmounttask without removing the callback (that’s the root cause). The component did a regular update due to theforceUpdate, and the callback was called, resulting in the following updating logic thinking it was a fresh initial mount.I would look for suspicious race conditions around here
https://github.com/facebook/react/blob/d3bbfe09cc47e2c5e34c3f5d0373e726981f0919/packages/react-dom/src/client/ReactDOM.js#L681-L688
https://github.com/facebook/react/blob/d3bbfe09cc47e2c5e34c3f5d0373e726981f0919/packages/react-dom/src/client/ReactDOM.js#L524-L550