fix(react): Nav unmounts component while invoking popTo or popToRoot (#27821)

Issue number: Resolves #27798

---------

## What is the current behavior

React IonNav component's views are missing keys, leading to unnecessary
duplicate mounting of components.


## What is the new behavior?
- Adds key to views of React IonNav component.

## Does this introduce a breaking change?

- [ ] Yes
- [x] No

---------

Co-authored-by: Sean Perkins <sean@ionic.io>
This commit is contained in:
zhbhun
2023-09-21 01:31:58 +08:00
committed by GitHub
parent 7b197a3226
commit 0edcb2cd85
3 changed files with 42 additions and 8 deletions

View File

@ -1,6 +1,8 @@
import type { FrameworkDelegate } from '@ionic/core/components';
import { createPortal } from 'react-dom';
import { generateId } from './utils/generateId';
// TODO(FW-2959): types
type ReactComponent = (props?: any) => JSX.Element;
@ -10,6 +12,9 @@ export const ReactDelegate = (
removeView: (view: React.ReactElement) => void
): FrameworkDelegate => {
const refMap = new WeakMap<HTMLElement, React.ReactElement>();
const reactDelegateId = `react-delegate-${generateId()}`;
// Incrementing counter to generate unique keys for each view
let id = 0;
const attachViewToDom = async (
parentElement: HTMLElement,
@ -22,7 +27,8 @@ export const ReactDelegate = (
parentElement.appendChild(div);
const componentWithProps = component(propsOrDataObj);
const hostComponent = createPortal(componentWithProps, div);
const key = `${reactDelegateId}-${id++}`;
const hostComponent = createPortal(componentWithProps, div, key);
refMap.set(div, hostComponent);