react-hot-toast

repository·main·Indexed 27 days ago

https://github.com/timolins/react-hot-toast

A lightweight (< 5kb), customizable, and accessible notification library for React applications. It features a simple API for triggering toasts, a Promise API for handling asynchronous states, and headless hooks via useToaster(). The library provides various toast variants including success, error, loading, and custom JSX, with support for multiple independent Toaster instances and global or individual styling.

Tokens
8K
Snippets
30
Records
53
Agent score
90%

What's inside react-hot-toast

  1. Overview of react-hot-toast features

    main

    react-hot-toast is a lightweight (< 5kb) and accessible notification library for React. Key features include:

    • Hot by default: Beautiful styles out of the box.
    • Easily Customizable: Highly configurable components.
    • Promise API: Automatic loading states derived from promises.
    • Headless Hooks: Use useToaster() to build your own custom notification logic and UI.
  2. Change the notification offset and position

    main

    To adjust the distance of notifications from the edges of the window or change the toaster's positioning (e.g., from fixed to relative), use the containerStyle prop on the Toaster component.

    // To change offset from edges
    <Toaster
      containerStyle={{
        top: 20,
        left: 20,
        bottom: 20,
        right: 20,
      }}
    />
    
    // To change position type
    <Toaster
      containerStyle={{
        position: 'relative',
      }}
    />
  3. Get started with basic toast usage

    main

    To use react-hot-toast, you must include the <Toaster /> component in your application's component tree to render the notifications. You can then trigger toasts from anywhere in your app using the toast() function.

    import toast, { Toaster } from 'react-hot-toast';
    
    const notify = () => toast('Here is your toast.');
    
    const App = () => {
      return (
        <div>
          <button onClick={notify}>Make me a toast</button>
          <Toaster />
        </div>
      );
    };
  4. Get started with react-hot-toast

    main

    To use react-hot-toast, you must first add the <Toaster /> component to your application. The <Toaster /> component handles the rendering of all notifications. Once the <Toaster /> is mounted, you can trigger notifications using the toast() function from anywhere in your component tree.

    import toast, { Toaster } from 'react-hot-toast';
    
    const notify = () => toast('Here is your toast.');
    
    const App = () => {
      return (
        <div>
          <button onClick={notify}>Make me a toast</button>
          <Toaster />
        </div>
      );
    };