React Hot Toast Notifications
react-hot-toast is a highly regarded, lightweight, and customizable React notification library designed to display "smoking hot" toast messages with minimal setup and a beautiful default aesthetic. Currently stable at version 2.6.0, the package maintains an active and responsive release cadence, frequently introducing new features and performance enhancements. Notable recent updates include support for multiple toasters in v2.6.0, enhanced React Server Components (RSC) compatibility for Next.js 13 App Router in v2.4.1, and significant bundle optimizations in v2.3.0. Its key differentiators include a powerful Promise API that automatically handles loading, success, and error states, an exceptionally small bundle size of less than 5kb, and versatile headless hooks (`useToaster`) for developers requiring complete control over the UI. The library is built with accessibility in mind and ships with TypeScript types, promoting robust and type-safe development practices. It provides a simple `Toaster` component and a `toast` function that can be called anywhere in your application.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use CommonJS `require` syntax in an environment (e.g., modern Next.js, ES Modules in Node.js) that primarily expects or enforces ES Modules after the v2.3.0-beta.1 update.fixRefactor your imports to use ES Module syntax (e.g., `import toast from 'react-hot-toast';`) and ensure your build tools are configured for ESM. -
Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate.
cause This error, particularly during unit testing, was a known issue in versions prior to v2.4.0 due to an infinite loop in component rendering logic.fixUpgrade `react-hot-toast` to version `2.4.0` or higher, which includes a fix for this infinite loop during testing. -
TypeError: Cannot read properties of undefined (reading 'message') when using toast.promise()
cause In versions prior to v2.5.1, the `toast.promise()` API made success/error messages optional, but some internal handling might not have gracefully managed promises resolving or rejecting without specific message properties, or incorrect promise usage.fixEnsure your promise resolution/rejection consistently provides an object with a `message` property for clear toast display, especially for older versions. Upgrade to `v2.5.1` or newer, which improved `toast.promise()` to make messages truly optional and allow async functions.
Warnings
- breaking The package moved to a new ESM compliant build setup using `.mjs` extensions and `tsup` for bundling. This could break existing CommonJS setups or older bundler configurations without proper resolution settings.
- gotcha Improved React Server Components (RSC) support for Next.js 13 App Router requires the 'use client' directive for components interacting with `react-hot-toast` to ensure they render in the client environment as expected.
- gotcha Prior to v2.5.1, `csstype` was not explicitly listed as a direct dependency, which could lead to warnings about missing peer dependencies in some environments or require manual installation.
- gotcha Version 2.6.0 introduces support for multiple toasters, along with new `dismissAll` and `removeAll` APIs. While adding functionality, existing custom multi-toaster implementations might benefit from refactoring to use the new ergonomic API, potentially affecting previous logic for managing multiple toast containers.
- gotcha Version 2.3.0 significantly improved bundle setup and exposed a new `react-hot-toast/headless` entry point. Users consuming headless features from older versions might need to update their import paths or adapt to the new structure.
Install
-
npm install react-hot-toast -
yarn add react-hot-toast -
pnpm add react-hot-toast
Imports
- toast
import { toast } from 'react-hot-toast';import toast from 'react-hot-toast';
- Toaster
import Toaster from 'react-hot-toast';
import { Toaster } from 'react-hot-toast'; - useToaster
const { useToaster } = require('react-hot-toast');import { useToaster } from 'react-hot-toast';
Quickstart
import toast, { Toaster } from 'react-hot-toast';
import React from 'react';
const notify = () => toast('Here is your toast.');
const App = () => {
return (
<div>
<button onClick={notify}>Make me a toast</button>
<Toaster />
</div>
);
};
export default App;