Sonner: Opinionated Toast Component for React

2.0.7 · active · verified Sun Apr 19

Sonner is an opinionated toast component for React, designed to be beautiful by default and simple to use. It emphasizes accessibility and provides a modern look and feel, inspired by macOS notifications. The library is currently stable at version 2.0.7 and receives regular updates, often with multiple patch releases between minor or major versions. Its key differentiators include built-in support for various toast types (success, error, info, warning, loading), rich customization options through props, and a strong focus on developer experience with a simple `toast()` API. Unlike some alternatives, Sonner aims to provide a consistent, aesthetically pleasing experience without requiring extensive styling, while still offering flexibility for specific needs. It also excels at handling asynchronous operations with its promise-based toast updates.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up the Toaster component, trigger various toast types (simple, success, loading, error), and programmatically dismiss a toast using its ID.

import { Toaster, toast } from 'sonner';
import './index.css'; // Or wherever your Sonner CSS is located

function App() {
  return (
    <div>
      <Toaster richColors position="bottom-right" />
      <h1>Welcome to my App</h1>
      <button onClick={() => toast('My first toast!')}>
        Show Simple Toast
      </button>
      <button onClick={() => toast.success('Operation successful!')}>
        Show Success Toast
      </button>
      <button onClick={() => toast.loading('Loading data...', { 
        id: 'loading-toast' 
      })}>
        Show Loading Toast
      </button>
      <button onClick={() => {
        toast.dismiss('loading-toast');
        toast.error('Operation failed!');
      }}>
        Dismiss Loading & Show Error
      </button>
    </div>
  );
}

export default App;

view raw JSON →