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: using a fragment with IonCard and IonModal results in error after upgrading to ionic 6 #24887
I have searched for existing issues that already report this problem, without success.
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.
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
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?
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.
The overlay hook accepts the same properties as IonModal. So you would still set swipeToClose: true and pass your reference element to presentingElement.
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:
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.
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
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
[-]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 [/+]liamdebeasi commentedon Mar 7, 2022
Thanks for the issue. In Ionic 6 we updated
ion-modalandion-popoverto 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, theIonModalcomponent was a wrapper for the modal controller that would programmatically create/destroy an<ion-modal>.In your application, the modal starts inside of a
Fragmentin theFragmentWithCardAndModalcomponent. 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-modalbecause it thinks it is a child of the fragment. This leads to the error you received becauseion-modalwas 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?ldikmans commentedon Mar 8, 2022
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:
liamdebeasi commentedon Mar 8, 2022
Can you provide an example of the memory leak?
The overlay hook accepts the same properties as
IonModal. So you would still setswipeToClose: trueand pass your reference element topresentingElement.ldikmans commentedon Mar 8, 2022
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?
liamdebeasi commentedon Mar 8, 2022
We have examples in our docs: https://ionicframework.com/docs/api/modal#useionmodal-hook
Thanks for the repo -- I'll take a look.
liamdebeasi commentedon Mar 8, 2022
Do you have steps to reproduce the memory leak? What should I be looking for?
ldikmans commentedon Mar 8, 2022
An error occurs (see image)
liamdebeasi commentedon Mar 9, 2022
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
useIonModalcontext afterCardWithOverlayWithMemoryLeakhas already been destroyed:To fix this, call
dismissfirst andawaitthe call before unmounting the component. Alternatively, you could add theuseIonModalcontext toExploreContainerand control modals from there.To summarize what we've discussed in this thread:
IonModalrendering an actual<ion-modal>element rather than a<div>that is artificially controlled by themodalController.Fragment. When the modal is presented, it is moved outside of theFragment.Fragment, React thinks the modal is still a child of theFragment, so the remove operation fails. As an alternative, we recommend using theuseIonModalhook.useIonModalcontext 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!
ldikmans commentedon Mar 9, 2022
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
ionitron-bot commentedon Apr 8, 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.