Skip to main content

Overview

The toast() function is the primary way to display notifications in Solid Toast. It returns a unique toast ID that can be used to dismiss or update the toast later.

Basic Usage

import { toast } from 'solid-toast';

function MyComponent() {
  const notify = () => toast('Hello, World!');
  
  return <button onClick={notify}>Show Toast</button>;
}

Function Signature

toast(message: Message, options?: ToastOptions): string
message
Message
required
The content to display in the toast. Can be a string, JSX element, or a function that returns either.
type Message = ValueOrFunction<Renderable, Toast>
type Renderable = JSX.Element | string | null
options
ToastOptions
Optional configuration object for customizing the toast behavior and appearance.
interface ToastOptions {
  id?: string;
  icon?: Renderable;
  duration?: number;
  ariaProps?: {
    role: 'status' | 'alert';
    'aria-live': 'assertive' | 'off' | 'polite';
  };
  className?: string;
  style?: JSX.CSSProperties;
  position?: ToastPosition;
  unmountDelay?: number;
  iconTheme?: IconTheme;
}
returns
string
Returns a unique toast ID that can be used with toast.dismiss() or toast.remove().

Toast Variants

Solid Toast provides several built-in variants for common notification types:
toast.success('Successfully saved!');

// With options
toast.success('User created', {
  duration: 4000,
  position: 'bottom-center',
});
Displays a success toast with a checkmark icon. Default duration: 2000ms

toast.promise()

Automatically handles promise states with different messages:
toast.promise<T>(
  promise: Promise<T>,
  msgs: {
    loading: Renderable;
    success: ValueOrFunction<Renderable, T>;
    error: ValueOrFunction<Renderable, any>;
  },
  opts?: DefaultToastOptions
): Promise<T>

Example

const myPromise = fetchData();

toast.promise(myPromise, {
  loading: 'Loading data...',
  success: (data) => `Successfully loaded ${data.count} items`,
  error: (err) => `Error: ${err.message}`,
});
The promise is returned unchanged, so you can still chain .then() and .catch() as needed.

Dismissing Toasts

toast.dismiss()

Dismiss a toast programmatically:
toast.dismiss(toastId?: string): void
toastId
string
Optional. The ID of the toast to dismiss. If omitted, dismisses all toasts.
const id = toast.loading('Processing...');

// Later, dismiss this specific toast
toast.dismiss(id);

toast.remove()

Immediately remove a toast without animation:
toast.remove(toastId?: string): void
toastId
string
Optional. The ID of the toast to remove. If omitted, removes all toasts.
toast.remove() skips the exit animation. Use toast.dismiss() for a smoother user experience.
// Remove specific toast immediately
const id = toast('Hello');
toast.remove(id);

// Remove all toasts immediately
toast.remove();

Advanced Usage

Dynamic Messages

toast((t) => (
  <div>
    <p>Toast ID: {t.id}</p>
    <p>Created at: {new Date(t.createdAt).toLocaleTimeString()}</p>
    <button onClick={() => toast.dismiss(t.id)}>Dismiss</button>
  </div>
));

Updating Toasts

const id = toast.loading('Uploading...');

// Update the same toast
setTimeout(() => {
  toast.success('Upload complete!', { id });
}, 3000);
By passing the same id in options, you can update an existing toast instead of creating a new one.

Custom Duration

// Short duration
toast('Quick message', { duration: 1000 });

// Persist until manually dismissed
toast('Important notice', { duration: Infinity });

Default Durations

Each toast type has a default duration:
TypeDuration
blank4000ms
success2000ms
error4000ms
loadingInfinity
custom4000ms

See Also

Build docs developers (and LLMs) love