Sonner — React Toast Notifications: Setup, Usage & Customization
A compact, pragmatic walkthrough of Sonner for React developers: installation, hooks, promise toasts, customization, and best practices — with code and SEO-friendly tips.
What Sonner is and why you should care
Sonner is a lightweight React toast notification library that aims to be minimal, fast, and flexible. It gives you the essential primitives for toasts and alerts without insisting on a heavy visual system, which is great when you want notifications that behave like a native part of your UI rather than a third-party mess.
From an engineering standpoint, Sonner focuses on simple APIs: provider + toast functions/hooks + render customizations. That means predictable lifecycle handling (show, update, dismiss), support for promise-based flows, and good accessibility defaults out of the box.
If you’re comparing “React toast libraries” (the common search intent here), Sonner sits between tiny utilities and full-blown UI kits — excellent for apps where bundle size, composability, and developer ergonomics matter.
Search intent & competitor analysis (TOP-10 summary)
Primary intents across the top SERP for queries like “sonner”, “React toast notifications”, and “sonner tutorial” are informational and commercial-technical. Users want quick setup guides, API references, and code examples. Some are evaluative: “Which React toast library should I use?”
Typical competitors include the official Sonner docs/GitHub, npm pages, tutorial posts (Dev.to, LogRocket), and comparison articles listing multiple toast libraries. Coverage depth varies — docs are concise, while tutorials add examples and best practices.
To outrank these, a page should: clearly show installation, live examples, hook usage, promise handling, customization paths, and troubleshooting — all with snippets and accessible notes. Feature snippets favor short code examples and a clear “How to install” step for voice and quick answers.
Quick installation & setup (copy-paste)
Install Sonner with npm or yarn, then add the <Toaster /> at the root of your app. Simple as that — but there are subtle options worth knowing when you customize placement, duration, or animations.
Install:
npm install sonner
# or
yarn add sonner
Basic setup (App root):
import { Toaster } from 'sonner'
export default function App() {
return (
<>
{/* your routes / UI */}
>
)
}
Pro tip: add the Toaster once (top-level). Use the provided toast helpers (or hooks) from anywhere in your component tree.
Basic usage: showing toasts and toast hooks
Sonner exposes a simple API. You can call toast directly or use hooks to access a contextual API. For most cases, the functional helpers like toast.success(), toast.error(), and toast() are enough.
Example using helpers:
import { toast } from 'sonner'
function SaveButton() {
async function handleSave() {
toast('Saving...')
await saveData()
toast.success('Saved!')
}
return
}
If you prefer more control, use the hook to keep references to toasts for updates and dismissals — useful for updating the same toast across async operations.
Promise toasts: sync UI to async work
One of Sonner’s killer features is promise-toasts, which map a promise life cycle to pending/success/error states. This is perfect for background operations like network calls and file uploads where you want graceful user feedback.
Usage example:
import { toast } from 'sonner'
async function upload(file) {
await toast.promise(
sendFile(file),
{
loading: 'Uploading...',
success: 'Upload complete',
error: 'Upload failed'
}
)
}
This pattern reduces boilerplate: no manual state for pending, no juggling messages, and clear UX for users who ask “Did it work?” — voice search queries like “sonner promise” often seek precisely this snippet.
Customization & theming
Sonner supports customizing placement, duration, and render structure. You can provide custom content, icons, or even a full custom renderer for toasts if your design system demands strict consistency.
Common customization approaches:
- Override CSS variables / theme tokens to match colors and spacing.
- Pass custom components for icon/content to align with the app’s design language.
Example of custom content:
// render a toast with a custom component
toast.custom((t) => <MyToast key={t.id} message={t.message} />)
Best practices, accessibility & performance
Use toasts sparingly: they should convey transient, peripheral information — not replace modals for critical flows. Keep messages short and actionable. For voice search and featured snippets, include a one-line “how-to” and a concise installation step at the top of the page.
Accessibility: ensure toasts are announced by screen readers. Sonner includes ARIA attributes by default, but confirm that your custom renderer preserves role=”status” or aria-live as appropriate. Also provide dismiss controls for persistent toasts.
Performance: because Sonner is lightweight, bundle impact is small. Still, lazy-load complex toast renderers or heavy icons to avoid shipping unnecessary bytes to first paint.
Troubleshooting & common gotchas
If toasts don’t appear, confirm that <Toaster /> is mounted once in the app tree. Duplicate providers or missing providers will prevent global toast helpers from working correctly.
Another common issue is styling conflicts: if your CSS reset removes default stacking contexts or resets position rules, the toaster might render off-screen. Inspect with devtools and check z-index and position.
For server-side rendering, ensure Toaster is only rendered on the client or guarded behind a “mounted” check to avoid hydration mismatch issues.
SEO, voice search, and featured snippet optimization
To capture voice and featured snippets, place short, direct answers early: a one-line definition, a three-step installation block, and one short code example. That structure is exactly what search engines pull into quick answers.
Sample snippet-friendly block (for quick consumption):
What is Sonner? A lightweight React toast library.
How to install? npm install sonner
How to show a toast? import { toast } from 'sonner'; toast('Hello')
Also include FAQ structured data (we provided JSON-LD) so Google can surface your Q&A in result pages directly.
Semantic core (expanded keyword clusters)
- sonner
- React toast notifications
- sonner tutorial
- React notification library
Secondary (usage & setup)
- sonner installation
- sonner setup
- React toast messages
- sonner example
Supporting / intent-driven
- React toast hooks
- sonner customization
- sonner promise
- React alert notifications
- React notification system
- React toast library
LSI & synonyms
- toast notifications React
- notification hooks
- toast promise handler
- toast custom renderer
- toaster component
Use these clusters organically: lead with core terms in title/H1, add setup/installation near the top, include examples for “example/tutorial” intent, and cover hooks/promise for developer queries.
References & helpful links
Official resources and high-quality tutorials are useful for deeper dives. Bookmark them:
- sonner GitHub — source, issues, and API reference.
- sonner on npm — package details and install instructions.
- sonner tutorial — advanced examples and patterns (Dev.to)
Those anchors use the search keywords directly to provide contextual backlinks that help both users and crawlers understand the topic relationship.
FAQ — top three user questions
How do I install Sonner in a React project?
Install via npm or yarn (npm install sonner). Add <Toaster /> at the app root, import the toast helpers or hook, and call toast('message') or toast.success().
Can Sonner handle promise-based notifications?
Yes — use toast.promise(promise, { loading, success, error }) to show a pending state then automatically switch to success or error based on the promise resolution.
How do I customize styles and icons in Sonner?
Override CSS variables, provide a custom renderer for toast content, or pass custom icon components. Ensure your custom markup keeps ARIA roles for accessibility.

Recente reacties