-
Notifications
You must be signed in to change notification settings - Fork 47.5k
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
unmount an empty component is breaking with ReactDOM portals #14811
Comments
Seems like the same as #14434. Want to look into it? |
Sure, I might need guidance into where exactly to start looking though |
I'm guessing it's somewhere here react/packages/react-reconciler/src/ReactFiberCommitWork.js Lines 741 to 775 in c11015f
Or here: react/packages/react-reconciler/src/ReactFiberCommitWork.js Lines 989 to 1077 in c11015f
|
Okay then I’ll take a look |
You might also want to start by making a failing test. Here's some existing tests for portals. react/packages/react-dom/src/__tests__/ReactDOMFiber-test.js Lines 418 to 462 in c11015f
|
Would this work? the test creates a component with the same criteria as the demo above and removes the child component via a state update. It currently breaks Also is ReactDOMFiber-test the correct place for the test? |
Ok I was debugging the code and I believe I can pinpoint where is the issue coming from: react/packages/react-reconciler/src/ReactFiberCommitWork.js Lines 1040 to 1073 in c11015f
When the node is a HostPortal, and it is rendering nothing, and it is the first child under a Fragment, the currentParentIsValid is not being reset to false correctly in line 1071 Adding I just feel that it's a too general solution? Am I on the right path at least? |
Thanks, this was very helpful. I sent a fix in #14820. |
* Adds failing test for #14811 * Fix removeChild() crash when removing an empty Portal
Canary |
Can confirm it works in demo and my real life project |
Fixed in 16.8.2 |
Still facing this in 16.8.6 |
@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
When the page unmounted, React removed the container before it removed the actual portal. I fixed this by simply reordering the
Just thought this might help someone else :) |
@dcporter44 You're an absolute life-saver. Had exactly the same issue (injecting into thirdy-party sibling) - switching the rendering order fixed it for me too. Thanks! |
I still see this in 16.12. Repro: https://codesandbox.io/s/portal-remove-child-error-sbnj2 |
Some of our users are getting this error sporadically in production ( |
Same here |
I did a little validation to avoid the crash.
In this way, my app is not crashing. |
I also started getting this, but in the strangest place. At first I thought it was in react-sortablejs, since it does DOM manipulation, but in React 16.12.0 it worked, and gave this error in 16.13.1. Turns out I was doing something wrong with useSelector in Redux. I basically returned a copy of my redux state, so the selector subscription was changed every time a rerender was triggered. Wasn't an obvious fix, I really had to backtrack and go through a lot of things and fixed my useSelectors with proper selector function. Think in my case it was zombie children: |
fix: unmountContainer() {
// macro task
setTimeout(() => {
// refs
if (containerEle) {
// receive removed node
// eslint-disable-next-line no-unused-vars
let removedChild = document
.querySelector('.page__wrapper')
.removeChild(containerEle);
}
});
} |
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:
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.
The text was updated successfully, but these errors were encountered: