DEV Community

MD Taseen Khan
MD Taseen Khan

Posted on • Originally published at reactjsexample.com on

An incredibly simple toast notification system for React

buttered-toast

An incredibly simple toast notification system for React

An incredibly simple toast notification system for React.

  • Only 894 byte (gzipped)
  • No dependencies
  • No styling, only logic
  • No configuration
  • No nonsense
  • Render components
  • No JSX during usage

This is a solution I’m using in several projects, so I decided to extract this into its own package. The existing libraries out there require you to add JSX elements to your React tree and render them based on conditions (controlled way). I never liked such solutions, I prefer a simple function call to show my toast.

Installation

npm install buttered-toast
Enter fullscreen mode Exit fullscreen mode

Usage

Add context provider

import { ToastContextProvider } from 'buttered-toast'

const App = ({ children }) => <ToastContextProvider>{children}</ToastContextProvider>
Enter fullscreen mode Exit fullscreen mode

useToast hook

import { useToast } from 'buttered-toast'

const Component = () => {
    const { show } = useToast()
    return <button onClick={() => show('Button clicked!')}>Click me!</button>
}
Enter fullscreen mode Exit fullscreen mode

API and Customization

Exports

  • ToastContextProvider
  • useToast
  • defaultOptions
  • ToastContext

ToastContextProvider

This is a React context provider that provides the state for useToast hooks. You must have this at least once in your React tree.

Props

  • options – An object of options to override the default options. See defaultOptions for more information.

useToast

This is a React hook that provides the show function to show a toast.

const { show } = useToast()
Enter fullscreen mode Exit fullscreen mode

show

This function takes a two arguments argument. The content to show in the toast and an optional options object to override the options for this specific toast.

  • content – The content to show in the toast. This can be anything that React can render as children.
  • options (optional) – An object of options to override the default options. See defaultOptions for more information.
show(<>Button clicked!</>)
show(<MyToastStyle>Button clicked!</MyToastStyle>)
Enter fullscreen mode Exit fullscreen mode

defaultOptions

An object of default options. These options can be overridden by passing an options object to theToastContextProvider.

  • duration – The duration in milliseconds to show the toast. Defaults to 3000.
  • ref – A ref to the toast element. Defaults to null. In case a ref is provided, React’s createPortal will be used to render the toast. If no ref is provided, the toast will be rendered in the ToastContextProvider.
  • Wrapper– A wrapper component to wrap the toast in. Defaults to and “empty component”:({ children }) => children.
  • wrapperProps – Props to pass to the wrapper component. Defaults to {}.

ToastContext

This is the React context that is provided by the ToastContextProvider. You can use this context to create your own.

Styling

This library does not provide any styling. You can style your toast by creating your own Toast component, and/or by styling your Wrapper element.

Advanced example

import { ToastContextProvider, defaultOptions } from 'buttered-toast'

const App = ({ children }) => (
    <ToastContextProvider
        options={{
            ...defaultOptions,
            duration: 5000,
            Wrapper: ({ children, title }) => (
                <div className="my-wrapper">
                    <div className="my-title">{title}</div>
                    {children}
                </div>
            ),
            wrapperProps: { title: 'System notification' }
        }}
    >
        {children}
    </ToastContextProvider>
)

import { useToast } from 'buttered-toast'

const Component = () => {
    const { show } = useToast()
    return (
        <button
            onClick={() =>
                show(<ToastStyle>Button clicked!</ToastStyle>, {
                    duration: 10000,
                    wrapperProps: { title: 'What just happened?' }
                })
            }
        >
            Click me!
        </button>
    )
}
Enter fullscreen mode Exit fullscreen mode

GitHub

View Github

Developer-first embedded dashboards

Developer-first embedded dashboards

Ship pixel-perfect dashboards that feel native to your app with Embeddable. It's fast, flexible, and built for devs.

Get early access

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay