Skip to content

fix(react): present controller overlays in React 18 #25361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 31, 2022
Merged
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
40 changes: 26 additions & 14 deletions packages/react/src/components/createControllerComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { OverlayEventDetail } from '@ionic/core/components';
import React from 'react';

import { attachProps, dashToPascalCase, defineCustomElement, setRef } from './react-component-lib/utils';
import {
attachProps,
dashToPascalCase,
defineCustomElement,
setRef,
} from './react-component-lib/utils';

interface OverlayBase extends HTMLElement {
present: () => Promise<void>;
Expand Down Expand Up @@ -39,7 +44,7 @@ export const createControllerComponent = <

class Overlay extends React.Component<Props> {
overlay?: OverlayType;
isUnmounted = false;
willUnmount = false;

constructor(props: Props) {
super(props);
Expand All @@ -51,14 +56,22 @@ export const createControllerComponent = <
}

async componentDidMount() {
/**
* Starting in React v18, strict mode will unmount and remount a component.
* See: https://reactjs.org/blog/2022/03/29/react-v18.html#new-strict-mode-behaviors
*
* We need to reset this flag when the component is re-mounted so that
* overlay.present() will be called and the overlay will display.
*/
this.willUnmount = false;
const { isOpen } = this.props;
if (isOpen as boolean) {
this.present();
}
}

componentWillUnmount() {
this.isUnmounted = true;
this.willUnmount = true;
if (this.overlay) {
this.overlay.dismiss();
}
Expand All @@ -77,18 +90,17 @@ export const createControllerComponent = <
if (this.props.onDidDismiss) {
this.props.onDidDismiss(event);
}
setRef(this.props.forwardedRef, null)
setRef(this.props.forwardedRef, null);
}

async present(prevProps?: Props) {
const {
isOpen,
onDidDismiss,
onDidPresent,
onWillDismiss,
onWillPresent,
...cProps
} = this.props;
const { isOpen, onDidDismiss, onDidPresent, onWillDismiss, onWillPresent, ...cProps } =
this.props;

if (this.overlay) {
this.overlay.remove();
}

this.overlay = await controller.create({
...(cProps as any),
});
Expand All @@ -107,8 +119,8 @@ export const createControllerComponent = <
);
// Check isOpen again since the value could have changed during the async call to controller.create
// It's also possible for the component to have become unmounted.
if (this.props.isOpen === true && this.isUnmounted === false) {
setRef(this.props.forwardedRef, this.overlay)
if (this.props.isOpen === true && this.willUnmount === false) {
setRef(this.props.forwardedRef, this.overlay);
await this.overlay.present();
}
}
Expand Down