You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: Conditionally rendering ionModal in react - Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node. #25590
I have searched for existing issues that already report this problem, without success.
Ionic Framework Version
v4.x
v5.x
v6.x
Nightly
Current Behavior
Conditionally rendering an IonModal, breaks when dismissing when using ionic v6.
Observed behavior: white/blank screen.
Error in console:
react-dom.development.js:10301 Uncaught DOMException: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
Expected Behavior
In an ideal scenario - the ionic modal could be conditionally rendered (without using the isOpen prop) this is a common pattern in react applications.
I tried to recreate a similar example in Angular to see what the behavior was there. While it doesn't error out, it does highlight a few problematic side effects of conditionally rendering modals in this way.
There is no dismiss animation for the modal.
None of the dismiss behavior fires (no dismiss events are emitted).
Since the framework is performing the remove operation, a lot of internal behavior is skipped. Modal is not currently architected to perform expected dismiss behavior when the node is removed outside of calling dismiss() or performing specific actions.
I'll need to compare with React and another modal library implementation to see how they handle this. I'll also need to explore useIonModal more and see if that's a more applicable pattern for achieving this behavior.
I am having the same issue. I have a list of items with ids and when I click on an item I open a modal passing this id. If the id is null I unmount the modal.
I get the same error when onDismiss is being fired. MESSAGE Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.
@sean-perkins I understand that useIonModal is the way around the issue, however - this is a very common and preferred pattern for react applications. When using the hook useIonModal - in a full scale application you end up having to wrap every modal hook with providers to ensure it works properly. When using a conditionally rendered modal - all the providers are there.
Also, take a look at the linked repo - the dismiss animation still works because it's called in the useLayoutEffect callback that is called before unmounting 😁
FYI we do consider this a bug, but leaving in triage status just to keep it top-of-queue to try and get a better grasp of where the root problem exists.
Ok I have been able to diagnose the root issue and have a better understanding of the work effort to resolve it.
The IonModal implementation in React does not have a framework delegate configured. This results in us manually removing the element from the DOM, instead of relying on React to re-render and remove both the React component and the element from the DOM.
Fortunately, my recent work with implementing the framework delegate into React for IonNav overlaps with this.
I'll classify this now to get it into our backlog. I'll create a dev build if my quick exploration is successful.
@JohnGeorgiadis appreciate your testing here! I'll need to do a little more investigation here. The current approach to solving conditionally rendering overlays is leading to a lot of branched behavior where it is only supported for a narrow configuration of overlays. I need to brainstorm with the team how we may alternatively support this.
Much of our overlay implementation is dependent on the overlay being rendered in the DOM. The usage of triggers requires the overlay to be in the DOM, to be able to emit a custom event to trigger the overlay to be presented. ion-datetime-button queries the closest modal or popover and there are other niche implementation patterns within our codebase. All of these do not play nicely with using React portals, which is the recommended architecture for solving the root problem.
This build has an opinionation that was required to make all the different ways we present overlays work correctly.
In this dev-build, when using triggers to present overlays, the overlay contents will always mount in the DOM. This means that it will internally set keepContentsMounted to true. You cannot use triggers without the contents being mounted. If you are using isOpen or the hooks to present overlays, you can defer mounting the React component until it is actually presented.
When presenting any overlay, we are now re-assigning the z-index value to make it the top overlay. Previously we didn't do this because inline overlays were being reconstructed when presented. We need to vet the impact of this change (appreciate any extra testing that overlays work correctly in your apps!), but if this change lands as-is, should be a minor performance improvement to inline overlays, since it reuses the DOM node and only does a style re-calc for the z-index change.
Could I ask how long until this issue is resolved? Im waiting on this to address the other related issue (#25775) before I upgrade from v5 to v6. It's quite common we have a dialog rendered and then a component is unmounted.
The pull request to resolve this issue is open for review. Once the team has an opportunity to review the changes and any required changes are addressed, this will be available in an upcoming patch release.
In the interim, I greatly appreciate any additional testing and verification with the dev-build posted above.
Hello everyone 👋 the team identified a few issues with the proposed dev-build that would be considering a breaking change to accessing refs and querying overlay elements.
We have collaborated on an alternative approach that could ship in v6.x.x. I have independently verified the test cases of this thread, but would appreciate additional testing/verification on the new dev-build:
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.
Activity
sean-perkins commentedon Jul 7, 2022
@corysmc thanks for opening this issue!
I tried to recreate a similar example in Angular to see what the behavior was there. While it doesn't error out, it does highlight a few problematic side effects of conditionally rendering modals in this way.
Since the framework is performing the remove operation, a lot of internal behavior is skipped. Modal is not currently architected to perform expected dismiss behavior when the node is removed outside of calling
dismiss()or performing specific actions.I'll need to compare with React and another modal library implementation to see how they handle this. I'll also need to explore
useIonModalmore and see if that's a more applicable pattern for achieving this behavior.JohnGeorgiadis commentedon Jul 8, 2022
I am having the same issue. I have a list of items with ids and when I click on an item I open a modal passing this id. If the id is null I unmount the modal.
I get the same error when onDismiss is being fired.
MESSAGE Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.this code was working on v5.
corysmc commentedon Jul 12, 2022
@sean-perkins I understand that useIonModal is the way around the issue, however - this is a very common and preferred pattern for react applications. When using the hook useIonModal - in a full scale application you end up having to wrap every modal hook with providers to ensure it works properly. When using a conditionally rendered modal - all the providers are there.
Also, take a look at the linked repo - the dismiss animation still works because it's called in the useLayoutEffect callback that is called before unmounting 😁
https://github.com/corysmc/react-overlay-bug/blob/development/src/components/CustomModal.tsx
sean-perkins commentedon Jul 14, 2022
Another issue popped up that we believe to be related to the problematic behavior here: https://github.com/jameson-w-taylor/modal-state-error
Rendering new child nodes before an inline modal, during the modal dismiss, causes the same exception as originally reported.
Over simplified code:
FYI we do consider this a bug, but leaving in triage status just to keep it top-of-queue to try and get a better grasp of where the root problem exists.
Fooweb commentedon Jul 20, 2022
Hello! Just wondering when an update will be made to correct this. Thanks!
sean-perkins commentedon Jul 20, 2022
Ok I have been able to diagnose the root issue and have a better understanding of the work effort to resolve it.
The
IonModalimplementation in React does not have a framework delegate configured. This results in us manually removing the element from the DOM, instead of relying on React to re-render and remove both the React component and the element from the DOM.Fortunately, my recent work with implementing the framework delegate into React for
IonNavoverlaps with this.I'll classify this now to get it into our backlog. I'll create a dev build if my quick exploration is successful.
sean-perkins commentedon Jul 20, 2022
JohnGeorgiadis commentedon Jul 21, 2022
@sean-perkins it works now as in v5. Overlay shadow is a bit weird for me, but I need to check that I did everything from the migration guide. Thanks!
sean-perkins commentedon Jul 21, 2022
@JohnGeorgiadis can you provide an image or screencast of the before & after?
Fooweb commentedon Jul 21, 2022
Hi Sean,
Thank you for the update. I tried to install the dev build but it's not working, I'm sure I'm doing something wrong.
Can you or someone help me with the command to update/install the dev build?
Thank you!
sean-perkins commentedon Jul 21, 2022
26 remaining items
sean-perkins commentedon Aug 30, 2022
@JohnGeorgiadis appreciate your testing here! I'll need to do a little more investigation here. The current approach to solving conditionally rendering overlays is leading to a lot of branched behavior where it is only supported for a narrow configuration of overlays. I need to brainstorm with the team how we may alternatively support this.
Much of our overlay implementation is dependent on the overlay being rendered in the DOM. The usage of triggers requires the overlay to be in the DOM, to be able to emit a custom event to trigger the overlay to be presented.
ion-datetime-buttonqueries the closest modal or popover and there are other niche implementation patterns within our codebase. All of these do not play nicely with using React portals, which is the recommended architecture for solving the root problem.sean-perkins commentedon Sep 9, 2022
Hello everyone 👋 unsure what version we are up to now, but I have an updated dev-build!
This build has an opinionation that was required to make all the different ways we present overlays work correctly.
In this dev-build, when using triggers to present overlays, the overlay contents will always mount in the DOM. This means that it will internally set
keepContentsMountedtotrue. You cannot use triggers without the contents being mounted. If you are usingisOpenor the hooks to present overlays, you can defer mounting the React component until it is actually presented.JohnGeorgiadis commentedon Sep 12, 2022
Hello @sean-perkins ,
I tried your build but unfortunately I still have the issue with date-pickers inside modals described before.
Is this build addressing this issue?
Thanks :)
sean-perkins commentedon Sep 13, 2022
@JohnGeorgiadis can you possibly isolate a small reproduction in a new repo?
I tried to replicate a scenario based on your code snippet above:
& I see the modal correctly displayed:
JohnGeorgiadis commentedon Sep 14, 2022
Hello @sean-perkins
https://github.com/JohnGeorgiadis/ion-datetime-button-issue here it is. I can still reproduce it.
I am loading a modal on tab1 with state isOpen.

Modal is opened with a datetimebutton

I cannot see the picker when I click on the modal

Maybe I am missing something obvious here.
sean-perkins commentedon Sep 14, 2022
@JohnGeorgiadis perfect, thank you for the reproduction!
Here is an updated dev-build that I tested against your reproduction:
When presenting any overlay, we are now re-assigning the
z-indexvalue to make it the top overlay. Previously we didn't do this because inline overlays were being reconstructed when presented. We need to vet the impact of this change (appreciate any extra testing that overlays work correctly in your apps!), but if this change lands as-is, should be a minor performance improvement to inline overlays, since it reuses the DOM node and only does a style re-calc for thez-indexchange.JohnGeorgiadis commentedon Sep 19, 2022
@sean-perkins it works! I can see that the modals are loading in the right order now.
Thanks!
puopg commentedon Sep 21, 2022
awesome!
puopg commentedon Oct 4, 2022
Could I ask how long until this issue is resolved? Im waiting on this to address the other related issue (#25775) before I upgrade from v5 to v6. It's quite common we have a dialog rendered and then a component is unmounted.
BAYiTUPAi commentedon Oct 5, 2022
Hoping for an update on this as well.
sean-perkins commentedon Oct 5, 2022
The pull request to resolve this issue is open for review. Once the team has an opportunity to review the changes and any required changes are addressed, this will be available in an upcoming patch release.
In the interim, I greatly appreciate any additional testing and verification with the dev-build posted above.
sean-perkins commentedon Oct 14, 2022
Hello everyone 👋 the team identified a few issues with the proposed dev-build that would be considering a breaking change to accessing refs and querying overlay elements.
We have collaborated on an alternative approach that could ship in v6.x.x. I have independently verified the test cases of this thread, but would appreciate additional testing/verification on the new dev-build:
Pending a team review, we could ship this fix in an upcoming patch release.
Thanks!
ionitron-bot commentedon Nov 16, 2022
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.