Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/react/src/components/navigation/IonNav.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { FrameworkDelegate, JSX } from '@ionic/core/components';
import { defineCustomElement } from '@ionic/core/components/ion-nav.js';
import React, { useState } from 'react';
import React, { useMemo, useState } from 'react';

import { ReactDelegate } from '../../framework-delegate';
import { createReactComponent } from '../react-component-lib';
Expand All @@ -25,10 +25,10 @@ const IonNavInternal: React.FC<IonNavProps> = ({ children, forwardedRef, ...rest
* Allows us to create React components that are rendered within
* the context of the IonNav component.
*/
const addView = (view: React.ReactElement) => setViews([...views, view]);
const removeView = (view: React.ReactElement) => setViews(views.filter((v) => v !== view));
const addView = (view: React.ReactElement) => setViews((existingViews) => [...existingViews, view]);
const removeView = (view: React.ReactElement) => setViews((existingViews) => existingViews.filter((v) => v !== view));

const delegate = ReactDelegate(addView, removeView);
const delegate = useMemo(() => ReactDelegate(addView, removeView), []);

return (
<IonNavInner delegate={delegate} ref={forwardedRef} {...restOfProps}>
Expand Down
8 changes: 5 additions & 3 deletions packages/react/src/framework-delegate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const ReactDelegate = (
addView: (view: React.ReactElement) => void,
removeView: (view: React.ReactElement) => void
): FrameworkDelegate => {
const refMap = new WeakMap<ReactComponent, React.ReactElement>();
const refMap = new WeakMap<HTMLElement, React.ReactElement>();

const attachViewToDom = async (
parentElement: HTMLElement,
Expand All @@ -24,17 +24,19 @@ export const ReactDelegate = (
const componentWithProps = component(propsOrDataObj);
const hostComponent = createPortal(componentWithProps, div);

refMap.set(component, hostComponent);
refMap.set(div, hostComponent);

addView(hostComponent);

return Promise.resolve(div);
};

const removeViewFromDom = (_container: any, component: ReactComponent): Promise<void> => {
const removeViewFromDom = (_container: any, component: HTMLElement): Promise<void> => {
const hostComponent = refMap.get(component);
hostComponent && removeView(hostComponent);

component.remove();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any considerations we need to make with conditionally rendering pages similar to #25663?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not believe the same considerations apply for IonNav. With IonModal the React node is relocated in the DOM and this causes potential issues when the surrounding React nodes are added or removed (causing React to incorrect remove the modal reference - which is why we render the template buffer elements). With IonNav the React node is stable and the components are added/removed as children of the IonNav.


return Promise.resolve();
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ describe('IonNav', () => {
cy.get('ion-button').contains('Go to Page Two').click();
cy.get('#pageTwoContent').should('be.visible');
cy.get('ion-nav').contains('Page two content');

cy.get('ion-nav .ion-page').should('have.length', 2);
});

it('should pop a page', () => {
Expand All @@ -23,10 +25,14 @@ describe('IonNav', () => {
cy.get('#pageTwoContent').should('be.visible');
cy.get('ion-nav').contains('Page two content');

cy.get('ion-nav .ion-page').should('have.length', 2);

cy.get('.ion-page.can-go-back ion-back-button').click();

cy.get('#pageOneContent').should('be.visible');
cy.get('ion-nav').contains('Page one content');

cy.get('ion-nav .ion-page').should('have.length', 1);
});

it('should pass params to the page', () => {
Expand Down