What's the best way to dismiss a toast when retrying an action? #1897
Answered
by
onurhan1337
runoncedev
asked this question in
Q&A
-
I'm trying to dismiss a toast once the user starts retrying some action. My current solution involves storing the toast ID with
Tried using Do you know if there might be a better way for achieving this? |
Beta Was this translation helpful? Give feedback.
Answered by
onurhan1337
Dec 8, 2023
Replies: 1 comment 2 replies
-
In this example, I've provided the type string | null to useRef to indicate that it will hold a string value or null. This should help TypeScript recognize that toastIdRef.current is indeed a string when you try to use it with dismiss. import { useToast } from "@/components/ui/use-toast";
import { useRef } from "react";
export default function Component() {
const toastIdRef = useRef<string | null>(null); // Provide the type (string) for useRef
const { toast, dismiss } = useToast();
const onSubmit = async () => {
if (toastIdRef.current) {
dismiss(toastIdRef.current);
}
try {
// Some async code here...
} catch (error) {
const { id } = toast({
title: "Error!",
});
toastIdRef.current = id;
}
};
return <p>Some content...</p>;
} |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
runoncedev
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this example, I've provided the type string | null to useRef to indicate that it will hold a string value or null. This should help TypeScript recognize that toastIdRef.current is indeed a string when you try to use it with dismiss.