use-toast.tsx


Overview

use-toast.tsx provides a lightweight, React-based toast notification system inspired by the react-hot-toast library. It enables applications to display transient toast messages with optional actions, descriptions, and titles. The toasts are managed globally through a reducer pattern, supporting adding, updating, dismissing, and removing toast notifications.

This file exports two primary interfaces:


Detailed Explanation

Types and Constants


Internal Variables


Functions and Methods

genId(): string

Generates a unique string ID for each toast by incrementing an internal counter and wrapping around at Number.MAX_SAFE_INTEGER.

Usage example:

const id = genId(); // "1", "2", "3", ...

addToRemoveQueue(toastId: string): void

Schedules a toast for removal after TOAST_REMOVE_DELAY. If a removal timeout already exists for this toast, it does nothing.

Implementation detail: This delayed removal mechanism allows toasts to have a "dismissed" state before being completely removed, enabling animations or other UI effects.


reducer(state: State, action: Action): State

Reducer function managing the toast state based on dispatched actions:

Important:
The reducer contains side effects (addToRemoveQueue) within the DISMISS_TOAST case. This is noted as a deliberate simplification.


dispatch(action: Action): void

Updates memoryState by applying the reducer, then notifies all registered listeners with the new state.


toast(props: Omit<ToasterToast, 'id'>): { id: string; dismiss: () => void; update: (props: ToasterToast) => void }

Primary API to create a toast:

Usage example:

const myToast = toast({
  title: "Success",
  description: "Your action was successful!",
  action: <button onClick={() => alert('Clicked!')}>Undo</button>,
});

// Later update the toast
myToast.update({ description: "Updated description" });

// Dismiss the toast
myToast.dismiss();

useToast(): { toasts: ToasterToast[]; toast: typeof toast; dismiss: (toastId?: string) => void }

React hook exposing toast state and control methods:

Usage example:

function MyComponent() {
  const { toasts, toast, dismiss } = useToast();

  React.useEffect(() => {
    const t = toast({ title: "Hello", description: "This is a toast" });
    // Auto-dismiss after 5 seconds
    setTimeout(() => t.dismiss(), 5000);
  }, [toast]);

  return (
    <div>
      {toasts.map(t => (
        <ToastComponent key={t.id} {...t} />
      ))}
    </div>
  );
}

Implementation Details and Algorithms


Interaction with Other System Parts


Visual Diagram

componentDiagram
    direction TB

    component "useToast Hook" as UseToast
    component "toast() Function" as ToastFn
    component "Reducer" as Reducer
    component "Global State\n(memoryState)" as State
    component "Listeners[]" as Listeners
    component "Toast UI\nComponents" as UIComponents

    UseToast --> State : subscribes to
    UseToast --> Listeners : registers setState
    UseToast --> Reducer : via dispatch()
    ToastFn --> Reducer : dispatch actions (ADD/UPDATE/DISMISS)
    Reducer --> State : updates
    Reducer --> Listeners : notifies on state changes
    Listeners --> UseToast : triggers setState
    UseToast --> UIComponents : provides toasts & control

Summary

use-toast.tsx is a minimal, centralized toast notification manager for React applications that:

This file is a core utility for handling toast notifications consistently across the application UI.