A configurable, composable, toast notification system for react.
npm install react-toast-notificationsThis was a great project to learn from and fulfilled the requirements it set out to. Unfortunately, I can no-longer give this project the time it needs. Consider react-hot-toast as an alternative, or look at the source and make your own 🎉 (there really isn't much to it).

---

A configurable, composable, toast notification system for react.
https://jossmac.github.io/react-toast-notifications
``bash`
yarn add react-toast-notifications
Wrap your app in the ToastProvider, which provides context for the Toast descendants.
`jsx
import { ToastProvider, useToasts } from 'react-toast-notifications';
const FormWithToasts = () => {
const { addToast } = useToasts();
const onSubmit = async value => {
const { error } = await dataPersistenceLayer(value);
if (error) {
addToast(error.message, { appearance: 'error' });
} else {
addToast('Saved Successfully', { appearance: 'success' });
}
};
return
;const App = () => (
);
`
For brevity:
- PlacementType is equal to 'bottom-left' | 'bottom-center' | 'bottom-right' | 'top-left' | 'top-center' | 'top-right'.TransitionState
- is equal to 'entering' | 'entered' | 'exiting' | 'exited'.
| Property | Description |
| -------------------------------------- | ------------------------------------------------------------------------------------------ |
| autoDismissTimeout number | Default 5000. The time until a toast will be dismissed automatically, in milliseconds. |boolean
| autoDismiss | Default: false. Whether or not to dismiss the toast automatically after a timeout. |Node
| children | Required. Your app content. |{ ToastContainer, Toast }
| components | Replace the underlying components. |boolean
| newestOnTop | Default false. When true, insert new toasts at the top of the stack. |PlacementType
| placement | Default top-right. Where, in relation to the viewport, to place the toasts. |string
| portalTargetSelector | Which element to attach the container's portal to. Uses document.body when not provided. |number
| transitionDuration | Default 220. The duration of the CSS transition on the Toast component. |
| Property | Description |
| ---------------------------------- | ------------------------------------------------------------------------ |
| appearance | Used by the default toast. One of success, error, warning, info. |boolean
| children | Required. The content of the toast notification. |
| autoDismiss | Inherited from ToastProvider if not provided. |number
| autoDismissTimeout | Inherited from ToastProvider. |Id => void
| onDismiss: | Passed in dynamically. Can be called in a custom toast to dismiss it. |PlacementType
| placement | Inherited from ToastProvider. |number
| transitionDuration | Inherited from ToastProvider. |TransitionState
| transitionState: | Passed in dynamically. |
The useToast hook has the following signature:
`jsx`
const {
addToast,
removeToast,
removeAllToasts,
updateToast,
toastStack,
} = useToasts();
The addToast method has three arguments:
1. The first is the content of the toast, which can be any renderable Node.Options
1. The second is the object, which can take any shape you like. Options.appearance is required when using the DefaultToast. When departing from the default shape, you must provide an alternative, compliant Toast component.ID
1. The third is an optional callback, which is passed the added toast .
The removeToast method has two arguments:
1. The first is the ID of the toast to remove.
1. The second is an optional callback.
The removeAllToasts method has no arguments.
The updateToast method has three arguments:
1. The first is the ID of the toast to update.Options
1. The second is the object, which differs slightly from the add method because it accepts a content property.ID
1. The third is an optional callback, which is passed the updated toast .
The toastStack is an array of objects representing the current toasts, e.g.
`jsx`
[
{
content: 'Something went wrong',
id: 'generated-string',
appearance: 'error',
},
{ content: 'Item saved', id: 'generated-string', appearance: 'success' },
];
To bring each toast notification inline with your app, you can provide alternative components to the ToastProvider:
`jsx
import { ToastProvider } from 'react-toast-notifications';
const MyCustomToast = ({ appearance, children }) => (
const App = () => (
);
`
To customize the existing component instead of creating a new one, you may import DefaultToast:
`jsx``
import { DefaultToast } from 'react-toast-notifications';
export const MyCustomToast = ({ children, ...props }) => (
);
This library may not meet your needs. Here are some alternative I came across whilst searching for this solution:
- https://github.com/fkhadra/react-toastify
- https://github.com/tomchentw/react-toastr
- https://github.com/jesusoterogomez/react-notify-toast