Skip to content

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

Closed

Description

@corysmc

Prerequisites

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.

Steps to Reproduce

  1. Conditionally render an IonModal
...
const [showIonModal, setShowIonModal] = useState(false);
...

return (
<div>
  <IonButton onClick={() => setShowIonModal(true)}>
    Show Modal
  </IonButton>
  {showIonModal && (
    <IonModal isOpen={true} onDidDismiss={() => setShowIonModal(false)}>
      <IonHeader>
        <IonToolbar>
          <IonTitle>Content</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent fullscreen>
        Ionic Modal Content here!
        <IonButton color="danger" onClick={() => setShowIonModal(false)}>
          Hide Modal (will break)
        </IonButton>
      </IonContent>
    </IonModal>
  )}
</div>
)
...
  1. Dismiss the modal (so that IonModal is pulled from the dom)
  2. Observe the error.

Code Reproduction URL

https://github.com/corysmc/react-overlay-bug

Ionic Info

Ionic:

Ionic CLI : 6.15.0
Ionic Framework : @ionic/react 6.1.13

Utility:

cordova-res : not installed
native-run : 1.6.0

System:

NodeJS : v14.16.0
npm : 6.14.11
OS : macOS Big Sur

Additional Information

Related:
#24887

Activity

  1. sean-perkins commented on Jul 7, 2022

    @sean-perkins
    Contributor

    @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.

    1. There is no dismiss animation for the modal.
    2. 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.

  2. JohnGeorgiadis commented on Jul 8, 2022

    @JohnGeorgiadis

    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.

     {selectedViewGroupId && (
            <GroupInfoModal
              handleDismissModal={() => setSelectedViewGroupId(null)}
              id={selectedViewGroupId}
              isOpen={!!selectedViewGroupId}
            />
          )}
    

    this code was working on v5.

  3. corysmc commented on Jul 12, 2022

    @corysmc
    ContributorAuthor

    @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

  4. sean-perkins commented on Jul 14, 2022

    @sean-perkins
    Contributor

    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:

    {items.map(item => <IonCard />)}
    <IonModal ref={modal} onWillDismiss={() => {
      setItems(...items, 'Another one');
    }}>
      <IonButton onClick={() => modal.current.dismiss()}>Close</IonButton>
    </IonModal>

    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.

  5. Fooweb commented on Jul 20, 2022

    @Fooweb

    Hello! Just wondering when an update will be made to correct this. Thanks!

  6. sean-perkins commented on Jul 20, 2022

    @sean-perkins
    Contributor

    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.

  7. added
    package: react
    type: bug
    on Jul 20, 2022
  8. sean-perkins commented on Jul 20, 2022

    @sean-perkins
  9. JohnGeorgiadis commented on Jul 21, 2022

    @JohnGeorgiadis

    @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!

  10. sean-perkins commented on Jul 21, 2022

    @sean-perkins
    Contributor

    @JohnGeorgiadis can you provide an image or screencast of the before & after?

  11. Fooweb commented on Jul 21, 2022

    @Fooweb

    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!

  12. sean-perkins commented on Jul 21, 2022

    @sean-perkins
  13. 26 remaining items

  14. sean-perkins commented on Aug 30, 2022

    @sean-perkins
    Contributor

    @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.

  15. sean-perkins commented on Sep 9, 2022

    @sean-perkins
    Contributor

    Hello everyone 👋 unsure what version we are up to now, but I have an updated dev-build!

    npm install @ionic/[email protected] @ionic/[email protected]

    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.

  16. JohnGeorgiadis commented on Sep 12, 2022

    @JohnGeorgiadis

    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 :)

  17. sean-perkins commented on Sep 13, 2022

    @sean-perkins
    Contributor

    @JohnGeorgiadis can you possibly isolate a small reproduction in a new repo?

    I tried to replicate a scenario based on your code snippet above:

    <IonDatetimeButton datetime="startDate" />
    <IonModal keepContentsMounted={true}>
      <IonDatetime
        id="startDate"
        preferWheel
        presentation="date"
        name="startDate"
        showDefaultButtons
        color="primary"
      />
    </IonModal>

    & I see the modal correctly displayed:

    Screen Shot 2022-09-13 at 4 09 43 PM

  18. JohnGeorgiadis commented on Sep 14, 2022

    @JohnGeorgiadis

    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.
    Screenshot 2022-09-14 at 09 43 14

    Modal is opened with a datetimebutton
    Screenshot 2022-09-14 at 09 43 20

    I cannot see the picker when I click on the modal
    Screenshot 2022-09-14 at 09 43 26

    Maybe I am missing something obvious here.

  19. sean-perkins commented on Sep 14, 2022

    @sean-perkins
    Contributor

    @JohnGeorgiadis perfect, thank you for the reproduction!

    Here is an updated dev-build that I tested against your reproduction:

    npm install @ionic/[email protected] @ionic/[email protected]

    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.

  20. JohnGeorgiadis commented on Sep 19, 2022

    @JohnGeorgiadis

    @sean-perkins it works! I can see that the modals are loading in the right order now.
    Thanks!

  21. puopg commented on Sep 21, 2022

    @puopg
    Contributor

    awesome!

  22. puopg commented on Oct 4, 2022

    @puopg
    Contributor

    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.

  23. BAYiTUPAi commented on Oct 5, 2022

    @BAYiTUPAi

    Hoping for an update on this as well.

  24. sean-perkins commented on Oct 5, 2022

    @sean-perkins
    Contributor

    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.

  25. sean-perkins commented on Oct 14, 2022

    @sean-perkins
    Contributor

    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:

    npm install @ionic/[email protected] npm install @ionic/[email protected]

    Pending a team review, we could ship this fix in an upcoming patch release.

    Thanks!

  26. ionitron-bot commented on Nov 16, 2022

    @ionitron-bot

    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.

  27. locked and limited conversation to collaborators on Nov 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: reacttype: bug

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions