React-Toastify: Highly Customizable Notifications

11.0.5 · active · verified Sun Apr 19

React-Toastify is a widely adopted library for adding highly customizable, non-blocking notifications, often called "toasts" or "snackbars," to React applications. The current stable version is 11.0.5. It maintains an active development cycle, frequently releasing patch and minor updates, with significant major versions (like v10 and v11) introducing substantial architectural changes and feature enhancements. Key differentiators include its ease of integration, comprehensive customization options allowing it to blend into any design system, full RTL support, intuitive swipe-to-close gestures, the ability to render React components within toasts, programmatic toast updates, and a configurable progress bar. Version 11 streamlined setup by automatically injecting its stylesheet, eliminating the need for manual CSS imports. Version 10 involved a significant internal rewrite, improving stability and fixing long-standing issues. It ships with TypeScript types, facilitating robust development.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates a basic toast notification and a promise-based toast with success/error states, showcasing `ToastContainer` and `toast` utility.

import React, { useState } from 'react';
import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css'; // Optional since v11, but good for backward compatibility

interface MyToastData {
  username: string;
}

function App() {
  const [username, setUsername] = useState('');

  const notify = () => {
    toast.info(`Hello ${username || 'Guest'}! Welcome.`, {
      position: 'top-right',
      autoClose: 3000,
      hideProgressBar: false,
      closeOnClick: true,
      pauseOnHover: true,
      draggable: true,
      progress: undefined,
      theme: 'light',
    });
  };

  const promiseNotify = () => {
    const myPromise = new Promise<MyToastData>((resolve, reject) => {
      setTimeout(() => {
        if (Math.random() > 0.5) {
          resolve({ username: 'JaneDoe' });
        } else {
          reject({ err: 'Failed to fetch user' });
        }
      }, 2000);
    });

    toast.promise(myPromise, {
      pending: 'Fetching user data...',
      success: {
        render({ data }) {
          return `User ${data?.username} fetched successfully!`;
        }
      },
      error: {
        render({ data }) {
          return `Error: ${data?.err}`;
        }
      }
    }, { autoClose: 5000 });
  };

  return (
    <div style={{ padding: '20px' }}>
      <h1>React-Toastify Example</h1>
      <input
        type="text"
        value={username}
        onChange={(e) => setUsername(e.target.value)}
        placeholder="Enter your name"
        style={{ marginRight: '10px', padding: '8px' }}
      />
      <button onClick={notify} style={{ padding: '10px 15px', cursor: 'pointer' }}>Show Basic Toast</button>
      <button onClick={promiseNotify} style={{ marginLeft: '10px', padding: '10px 15px', cursor: 'pointer' }}>Show Promise Toast</button>
      <ToastContainer />
    </div>
  );
}

export default App;

view raw JSON →