Skip to content

bug: using a fragment with IonCard and IonModal results in error after upgrading to ionic 6  #24887

Closed

Description

@ldikmans

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x

Current Behavior

A Fragment with an IonCard and an IonModal. The modal contains a button that will close the modal AND hide the card.
Click on the Button
Observed behavior: white 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

Expected behavior:
modal closes and card is hidden.

It works when not using a fragment, but moving the modal to the container the card is defined in. However we want to reuse this card in several places in our app.
In previous versions this setup worked as expected. (Ionic 5)

Steps to Reproduce

Create a fragment with an IonCard and an IonModal.
Add a button on the modal that will close the modal and hides the card.
add the fragment to a container.

Run the code with developer console open

click on the card to open the modal
click on the button to close the modal and hide the card.

Code Reproduction URL

https://github.com/ldikmans/FragmentBug

Ionic Info

Ionic:

Ionic CLI : 6.18.1 (/usr/local/lib/node_modules/@ionic/cli)
Ionic Framework : @ionic/react 6.0.10

Capacitor:

Capacitor CLI : 3.4.3
@capacitor/android : 3.4.3
@capacitor/core : 3.4.3
@capacitor/ios : 3.4.3

Utility:

cordova-res : 0.15.4
native-run : 1.5.0

System:

NodeJS : v16.13.1 (/usr/local/Cellar/node@16/16.13.1/libexec/node)
npm : 8.1.2
OS : macOS Monterey

Additional Information

No response

Activity

  1. changed the title [-]bug: using a fragment with IonCard and IonModal results in error after upgrading to ionic 6 on [/-] [+]bug: using a fragment with IonCard and IonModal results in error after upgrading to ionic 6 [/+] on Mar 7, 2022
  2. self-assigned this
    on Mar 7, 2022
  3. liamdebeasi commented on Mar 7, 2022

    @liamdebeasi
    Contributor

    Thanks for the issue. In Ionic 6 we updated ion-modal and ion-popover to work declaratively in your template. This means that when you write <IonModal> in your template you are initializing an instance of the <ion-modal> component. In Ionic 5, the IonModal component was a wrapper for the modal controller that would programmatically create/destroy an <ion-modal>.

    In your application, the modal starts inside of a Fragment in the FragmentWithCardAndModal component. When you present the modal, the modal moves from inside of that component to the root of your application. This allows the modal to overlay your entire application. When the modal is dismissed, it should be moved back inside of the fragment.

    Clicking the button destroys the fragment, leading React to try and destroy ion-modal because it thinks it is a child of the fragment. This leads to the error you received because ion-modal was moved outside of your fragment.

    For this use case, I think it would be better to use Overlay Hooks. This is the same <ion-modal> component, but the difference is that it is not added to the DOM until you present it. As a result, the modal is never added as a child of your fragment. Can you try the overlay hook and let me know if that fixes the issue?

  4. added
    needs: reply
    on Mar 7, 2022
  5. removed their assignment
    on Mar 7, 2022
  6. ldikmans commented on Mar 8, 2022

    @ldikmans
    Author

    Hi,

    I have updated the sample app. It works when I use useIonModal, I no longer have the error.

    However, I have two other challenges in this case:

    • a memory leak when I use a state to hide the card. I have a workaround to set the card.style.display='none'.
    • We card modals in iOS mode that have the ability to be presented in a card-style and swiped to close. I am not sure how to solve that when using the overlay hook.
  7. added and removed
    needs: reply
    on Mar 8, 2022
  8. liamdebeasi commented on Mar 8, 2022

    @liamdebeasi
    Contributor

    Can you provide an example of the memory leak?

    The overlay hook accepts the same properties as IonModal. So you would still set swipeToClose: true and pass your reference element to presentingElement.

  9. added
    needs: reply
    on Mar 8, 2022
  10. ldikmans commented on Mar 8, 2022

    @ldikmans
    Author

    Yes, it is in the code base: https://github.com/ldikmans/FragmentBug

    I added a fourth card that has the memory leak

    adding the properties I will need to figure out, do you have an example?

  11. added and removed
    needs: reply
    on Mar 8, 2022
  12. liamdebeasi commented on Mar 8, 2022

    @liamdebeasi
    Contributor

    We have examples in our docs: https://ionicframework.com/docs/api/modal#useionmodal-hook

    const [present, dismiss] = useIonModal(ModalCmp, { ... });
    
    ...
    
    <IonButton
      onClick={() => {
        present({
          swipeToClose: true,
          presentingElement: [router outlet reference]
         });
       }}
    >
      Show Modal
    </IonButton>

    Thanks for the repo -- I'll take a look.

  13. liamdebeasi commented on Mar 8, 2022

    @liamdebeasi
    Contributor

    Do you have steps to reproduce the memory leak? What should I be looking for?

  14. added
    needs: reply
    on Mar 8, 2022
  15. ldikmans commented on Mar 8, 2022

    @ldikmans
    Author
    1. when you run the app, open the developer console in the browser.
    2. Click on the fourth card (bottom).
    3. Scroll down, to see the button.
    4. Click on it. The modal closes, the card is gone.
      An error occurs (see image)

    image

  16. added and removed
    needs: reply
    on Mar 8, 2022
  17. liamdebeasi commented on Mar 9, 2022

    @liamdebeasi
    Contributor

    Thanks. I took a look, and this appears to be a memory leak in your application, not in Ionic React. The problem is that you are trying to update the useIonModal context after CardWithOverlayWithMemoryLeak has already been destroyed:

    async function handleDismiss() {
      hideCard();
      dismiss();
    }

    To fix this, call dismiss first and await the call before unmounting the component. Alternatively, you could add the useIonModal context to ExploreContainer and control modals from there.


    To summarize what we've discussed in this thread:

    • There was a change in Ionic 6 that resulted in IonModal rendering an actual <ion-modal> element rather than a <div> that is artificially controlled by the modalController.
    • The modal is initially added as a child of a Fragment. When the modal is presented, it is moved outside of the Fragment.
    • When destroying the Fragment, React thinks the modal is still a child of the Fragment, so the remove operation fails. As an alternative, we recommend using the useIonModal hook.
    • React errored on a potential memory leak, but this was happening because the useIonModal context was being modified after its containing component was already destroyed. I detailed a fix in the steps above.

    This is more of a support question, so I am going to close this issue. I recommend posting on the Ionic Forums if you have additional questions on how to implement modals in your application.

    Thanks!

  18. ldikmans commented on Mar 9, 2022

    @ldikmans
    Author

    I agree that it is a separate issue. The 'await dismiss' solves the memory leak, but want to avoid that the user sees the card disappear after the modal has closed.
    Thanks for the excellent summary and solution

  19. ionitron-bot commented on Apr 8, 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.

  20. locked and limited conversation to collaborators on Apr 8, 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

    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