Build Reliable Toasts and Alerts with react-notifications-component
This practical guide covers react-notifications-component from installation to advanced customization.
You'll get working examples, hooks & store patterns, accessibility tips, and copy-ready code for producing toast notifications and alert messages in React apps.
Installation & Getting Started
To start using the React notification system, install the library and its styles. The most common package is
react-notifications-component, which provides a compact API for creating toast messages and alert notifications.
Run either npm or yarn from your project root:
npm install react-notifications-component --save
# or
yarn add react-notifications-component
After installation, register the component once near your app root (App.jsx or index.jsx). The ReactNotification container is where toasts mount; you typically render it once and call the exposed API to add notifications.
This keeps notifications decoupled from UI screens and avoids duplicated containers.
If you want a step-by-step walkthrough, check this react-notifications-component tutorial for a complete demo and sample repo.
The example there also demonstrates using the library with both function and class components.
(Link: react-notifications-component tutorial)
Basic Example: Toasts and Alert Notifications
A minimal setup creates a container and then triggers notifications via the API. Put the container near the app root:
import React from 'react';
import ReactNotification from 'react-notifications-component';
import 'react-notifications-component/dist/theme.css';
function App() {
return (
<>
{/* rest of your app */}
>
);
}
To show a toast, call the addNotification method exposed via a ref or store. The simplest approach uses the exported store:
import { store } from 'react-notifications-component';
store.addNotification({
title: 'Success',
message: 'Data saved.',
type: 'success',
insert: 'top',
container: 'top-right',
dismiss: { duration: 3000, onScreen: true }
});
This pattern yields standard React toast notifications and alert behavior: type-based styling (success, danger, info), positioning, and auto-dismiss.
Because the notification container is a single global mount, you can trigger toasts from anywhere in your component tree without prop drilling.
Customization and Styling
react-notifications-component supports CSS themes and inline configuration for each toast. You can override the provided theme.css or supply your own classNames per notification.
For example, add a custom animation or tweak colors by targeting the container and item classes in your stylesheet.
Each notification can include custom content or a React element for rich messages (buttons, links, or images). Use the content property to render JSX:
store.addNotification({
content: ({ id }) => <div><strong>Action complete</strong></div>,
// other props
});
For consistent design, centralize common options (duration, placement, animation) into a factory function. This makes your toast library predictable and easier to test.
Remember that over-styled toasts can reduce clarity—keep notifications concise, use types for urgency, and prefer accessible color contrasts.
- Common options: type, container, insert, dismiss.duration, animation
- Custom content: JSX via content prop
Hooks, Store, and Advanced Patterns
The library exposes a global store you can import to add and remove notifications. For modern React apps, wrap the store into custom hooks to keep your components declarative and testable.
A simple hook abstracts the store API and returns helper functions:
import { useCallback } from 'react';
import { store } from 'react-notifications-component';
export function useToast() {
const notify = useCallback((opts) => store.addNotification(opts), []);
const remove = useCallback(id => store.removeNotification(id), []);
return { notify, remove };
}
Use this hook in components to trigger toasts without exposing the global store across your app, which simplifies mocking in unit tests.
For advanced state-driven notifications (e.g., queueing, deduplication), maintain a notification queue in context and dispatch add/remove operations through that provider.
The library also supports storing notification IDs and programmatically removing or updating toasts. Implement business logic—such as deduplication—by checking for existing message fingerprints before calling addNotification. This avoids duplicate React toast messages when actions fire repeatedly.
Accessibility, Performance, and Best Practices
Accessibility matters: ensure toasts are announced to screen readers. Wrap your notification content in ARIA-friendly roles or use an SR-only live region. The library itself does not automatically set polite/assertive live regions for every configuration, so add them where urgency dictates.
For performance, avoid rendering heavy elements inside notifications. Keep content lightweight and avoid long DOM trees. If an alert requires a large UI (forms, images), consider linking to a modal or dedicated page from the toast instead of embedding it.
Best practices for a production notification system: limit concurrent toasts, set sensible durations, provide manual dismissal, and choose neutral animations. For mobile users, test touch targets for dismiss buttons and ensure toasts don’t obscure critical UI controls.
- Quick checklist: single container, dedupe messages, accessible ARIA roles, sensible durations
Troubleshooting & Common Issues
If notifications aren’t showing, first ensure the ReactNotification component is rendered exactly once in the component tree. Duplicate containers or missing mounts are the most common causes.
Confirm you imported the CSS theme or your custom styles to ensure visibility and proper z-index.
If addNotification returns no visible toast but a notification ID, check container and insert options (e.g., top-right, bottom-left). An incorrect container string will prevent placement. Also verify that dismiss.duration isn't set to 0 (instant removal), and that onScreen is configured correctly.
For server-side rendering or hydration issues, render the notification container only on the client. Wrap the ReactNotification mount in a client-only guard (useEffect toggled render) to avoid server markup mismatches.
Conclusion
react-notifications-component is a compact and flexible React toast library suitable for most notification needs: simple success/failure toasts, custom alert components, and store-driven patterns.
Use a single container, abstract the store with hooks, and centralize configuration to keep notifications consistent across your app.
Want a guided tutorial and sample project? See the linked react-notifications-component tutorial with examples that parallel the patterns shown here.
(See: React notification system tutorial)
Implement toasts thoughtfully: clarity beats flashiness. Keep messages actionable and avoid flooding users—your users will thank you with fewer dismiss clicks and better retention.
FAQ
- How do I install and initialize react-notifications-component?
-
Install via npm or yarn and render the
ReactNotificationcontainer near your app root. Import the theme CSS and usestore.addNotification({...})to show toasts. - Can I use hooks with react-notifications-component?
-
Yes. Wrap the global
storein a custom hook (e.g.,useToast) to exposenotifyandremovehelpers. This improves testability and keeps components declarative. - How do I customize toast styles and behavior?
-
Override the theme CSS or supply
contentwith JSX. Configure per-toast options (type, container, dismiss.duration, animation) and centralize defaults in a factory to keep styling consistent.