React UI Notification Component

5.6.4 · active · verified Sun Apr 19

The `rc-notification` package provides a foundational React UI component for displaying transient notifications such as toasts or alerts. It functions as a low-level, unstyled primitive, primarily serving as a building block for higher-level UI libraries, most notably Ant Design's notification system. The current stable version, 5.6.4, receives ongoing maintenance, with recent updates focused on bug fixes and performance enhancements within its 5.x series. It's crucial for developers to note that a separate, newer package, `@rc-component/notification`, has also been released. This newer package introduces additional features and potentially different APIs, marking a distinct evolution from `rc-notification`. This distinction is important for selecting the correct package based on project requirements and desired feature set, as the two packages have diverged in their development paths.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to create a notification instance and display multiple notices using `rc-notification` in a React application with TypeScript, including basic styling and container setup.

import Notification from 'rc-notification';
import React from 'react';
import ReactDOM from 'react-dom/client';
import type { NotificationInstance, NoticeContent } from 'rc-notification';

// Assuming a root element exists in your HTML, e.g., <div id="root"></div>
const rootElement = document.getElementById('root');
if (!rootElement) throw new Error('Root element not found');
const root = ReactDOM.createRoot(rootElement);

root.render(
  <React.StrictMode>
    <button onClick={() => {
      Notification.newInstance({
        style: { top: 65, left: '50%', transform: 'translateX(-50%)' }, // Center horizontally
        maxCount: 3,
        getContainer: () => document.body, // Attach notification container to body
      }, (notification: NotificationInstance) => {
        const noticeProps: NoticeContent = {
          content: 'Hello, this is a TypeScript notification!',
          duration: 4.5,
          closable: true,
          onClose() {
            console.log('TypeScript Notification closed!');
          },
        };
        notification.notice(noticeProps);

        notification.notice({
          content: 'Another important message via TypeScript.',
          duration: 3,
          key: 'unique_message_key',
        });
      });
    }} style={{ padding: '10px 20px', fontSize: '16px', cursor: 'pointer' }}>
      Show Notifications
    </button>
  </React.StrictMode>
);

view raw JSON →