Skip to content

shadcn.io is an independent project built on shadcn/ui

Free

Error Toast Sonner for React

A Show Error button that opens Something went wrong with the error icon, and with no second line and no Retry.

Show Error is an outline button. The toast says Something went wrong and shows the error icon. There is no second line and no Retry button.

Say what failed and what to do next when you use this for real. This demo is only the short line. A failure people must fix on the page belongs in an Alert. In React, Base UI themes the icon and does not choose the words.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
Use email insteadNo credit card

Anatomy

Sonner anatomy: toaster, toast, title, description, action

Usage

<>
  <Toaster />
  <Button onClick={handleShowToast} variant="outline">
    Show Error
  </Button>
</>

FAQ

How does this demo show an error toast?

The button calls toast.error with "Something went wrong". That sets the type to error. Toaster draws an OctagonXIcon with className="size-4" and aria-hidden.

Which options does this call pass?

Only that message. There is no description, duration, action, or custom icon in this file. The whole notice is the string passed to toast.error.

How do I add what failed under the title?

Pass description as the second argument to toast.error. It renders under the title. This demo does not pass one, so the title is the only line.

How do I add a Retry button?

Pass action with a label and an onClick in the second argument. The button runs onClick and then closes the toast. This call does not pass action.

How long does this error stay up?

This call does not pass duration. The toast uses duration on Toaster, or 4 seconds when Toaster omits it too. Pass duration on this call when this notice should stay longer.

Where is the error icon defined?

Toaster sets icons.error to OctagonXIcon. Pass icon on toast.error when this toast needs a different one. The demo renders its own Toaster; in an app, render one near the root.

API Reference

Sonner

No extra props in this demo. The rest is on the Sonner page.

Button

Sets onClick={handleShowToast} and variant="outline". The rest is on the Button page.