Skip to content

bug: action sheet, alert, picker, and toast do not open when isOpen is initially true #27928

@legendar

Description

@legendar

Prerequisites

Ionic Framework Version

v7.x

Current Behavior

I want to show IonToast on page load based on some condition, e.g:

const {isToastOpen} = useState(true)
...
<IonToast
  message="Some message"
  isOpen={isOpen}
>

if isOpen is initially true then a toast does not appear. It only appears if isOpen is initially false and then changed to true via timeout or any other event.

Expected Behavior

When I set isOpen={true} I expect that toast appears regardless of any other state.

Steps to Reproduce

Minimal STR is:

<IonToast
  message="Some message"
  isOpen={true}
>

In the provided repo the toast is rendered inside ExploreContainer here: https://github.com/legendar/ion-input-label-issue/blob/main/src/components/ExploreContainer.tsx#L23

Code Reproduction URL

https://github.com/legendar/ion-input-label-issue

Ionic Info

[WARN] Error loading @capacitor/ios package.json: Error: Cannot find module '@capacitor/ios/package.json'

   Require stack:
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/project/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/bin/ionic

[WARN] Error loading @capacitor/android package.json: Error: Cannot find module '@capacitor/android/package.json'

   Require stack:
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/project/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/lib/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/index.js
   - /home/***/.config/yarn/global/node_modules/@ionic/cli/bin/ionic

Ionic:

Ionic CLI : 7.1.1 (/home/***/.config/yarn/global/node_modules/@ionic/cli)
Ionic Framework : @ionic/react 7.1.3

Capacitor:

Capacitor CLI : 5.2.1
@capacitor/android : not installed
@capacitor/core : 5.2.1
@capacitor/ios : not installed

Utility:

cordova-res : not installed globally
native-run : 1.7.2

System:

NodeJS : v16.15.0 (/home/***/.nvm/versions/node/v16.15.0/bin/node)
npm : 8.5.5
OS : Linux 5.10

Additional Information

The following component can be used as a quick workaround:

function InlineToast({
  isOpen,
  ...otherProps
}): JSX.Element {
  const [isOpenInternal, setIsOpenInternal] = useState(false);

  useEffect(() => {
    // setTimeout is needed because sometimes the toast shadowDom is not ready
    setTimeout(() => {
      setIsOpenInternal(isOpen);
    }, 10);
  }, [isOpen]);

  return (
    <IonToast
      isOpen={isOpenInternal}
      {...otherProps}
    />
  );
}

In the provided repo replace IonToast with InlineToast component and the toast will appear.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions