Skip to content
Discussion options

You must be logged in to vote

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({

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@runoncedev
Comment options

@alamenai
Comment options

Answer selected by runoncedev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants