I hope this category is okay - I couldn’t see one for ionic/react.
I’m trying to use Ionic toast notifications in a React app (ionic/react 4.11.3), but can only get them to display once.
I see exactly the same problem if I implement the React example shown here: https://ionicframework.com/docs/api/toast
Click a button and a toast appears. But after it’s gone, clicking the button again has no effect - the toast does not reappear.
Rich
jjdev
November 1, 2019, 3:02pm
2
What does your code look like?
Hello. I get the problem with this code straight from the api page:
import React, { useState } from 'react';
import { IonToast, IonContent, IonButton } from '@ionic/react';
export const ToastExample: React.FC = () => {
const [showToast1, setShowToast1] = useState(false);
const [showToast2, setShowToast2] = useState(false);
return (
<IonContent>
<IonButton onClick={() => setShowToast1(true)} expand="block">Show Toast 1</IonButton>
<IonButton onClick={() => setShowToast2(true)} expand="block">Show Toast 2</IonButton>
<IonToast
isOpen={showToast1}
onDidDismiss={() => setShowToast1(false)}
message="Your settings have been saved."
duration={200}
/>
<IonToast
isOpen={showToast2}
onDidDismiss={() => setShowToast2(false)}
message="Click to Close"
position="top"
buttons={[
{
side: 'start',
icon: 'star',
text: 'Favorite',
handler: () => {
console.log('Favorite clicked');
}
},
{
text: 'Done',
role: 'cancel',
handler: () => {
console.log('Cancel clicked');
}
}
]}
/>
</IonContent>
);
};
jjdev
November 1, 2019, 4:30pm
4
I’m having the same result on the latest CLI and framework. Looks like a bug
jjdev
November 7, 2019, 11:35pm
6
Looks like the latest release now fixes this
Update package.json to
'@ionic/angular': '^4.11.4'
then run $ npm install
in the project directory
I haven’t tested it but the changelog says it’s taken care of
Yes it sorted the toast problem. Unfortunately it seems to have introduced some new problems around ionic/react lifecycle methods which I haven’t had time to get to the bottom of yet.
jjdev
November 8, 2019, 2:39pm
8
They are now individual imports from @ionic/react
Yes - but that hasn’t changed from 4.11.3.