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.

unmount an empty component is breaking with ReactDOM portals

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? When unmounting a component that has a child being rendered under a different parent (with portals), react is throwing an error

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem:

https://codesandbox.io/s/73n31lwpjx

What is the expected behavior?

Component should unmount normally

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? 16.8.1 Issue also happens with 16.7.0 (https://codesandbox.io/s/oxmpxmllvy)

The issue is only happening under very strict conditions:

  • The component being rendered with ReactDOM Portals (Modal) should not render any HTML
  • The parent component (Panel) should render Modal as the first component under <React.Fragment>

Avoiding this is as simple as moving Modal under some other HTML. I’m not entirely sure this is an issue or I’m just doing something wrong with Fragment and portals.

The actual error being thrown is: react-dom.development.js:9254 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

15reactions
278kunalcommented, Jul 31, 2019

Still facing this in 16.8.6

7reactions
dcporter44commented, Aug 5, 2019

@278kunal I was also receiving this same error in 16.8.6. However, I just discovered that I was receiving this error for a different reason than mentioned in this thread. I was using createPortal to append my component to a sibling container (Bad practice, but I had to do this as a workaround with a 3rd party library I’m using). My code looked something like this:

render() { return ( <div> <div id="container"></div> {ReactDOM.createPortal(<div>Append Me</div>, document.getElementById("container")) </div> ) }

When the page unmounted, React removed the container before it removed the actual portal. I fixed this by simply reordering the createPortal before the sibling container I was appending to. That way React destroys the portal prior to the container. So it looks like this:

render() { return ( <div> {ReactDOM.createPortal(<div>Append Me</div>, document.getElementById("container")) <div id="container"></div> </div> ) }

Just thought this might help someone else 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Portals - React
Portals. Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component....
Read more >
How to unmount, unrender or remove a component, from itself ...
I know this question has been asked a couple of times already but most of the time, the solution is to handle this...
Read more >
Using React Portals to Render Children Outside the DOM ...
When the component unmounts we want to remove the child to avoid any memory leakage. We will import this Portal component into another...
Read more >
[Solved]-Testing React portals with enzyme-Reactjs
Coding example for the question Testing React portals with enzyme-Reactjs. ... we just can't forget to unmount the component after each test. Modal.test.js...
Read more >
focus-trap-react - npm
A React component that traps focus.. Latest version: 10.0.2, last published: 24 days ago. Start using focus-trap-react in your project by ...
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