React, React Tutorial

How to Create Toast Notifications in React with React Toastify?

React Toastify is a widely popular NPM package for displaying toast messages in React.js applications. In this comprehensive guide, we will explore...

Written by Shivangi Rajde · 4 min read >
TOAST NOTIFICATIONS IN REACT​ WITH ​ REACT TOASTIFY

React Toastify is a widely popular NPM package for displaying toast messages in React.js applications. In this comprehensive guide, we will explore how to effectively utilize React Toastify in a simple React.js app focused on guessing nationalities based on given names. Let’s embark on this informative journey and get started with implementing React Toastify to enhance your React.js projects!

Options to display messages in React Applications

There are several options available for displaying messages in React applications. Here are some common approaches:

  • Inline Text: You can simply render a text element directly in your component to display a message. For example:
function MyComponent() {
  return <p>Hello, world!</p>;
}
  • Conditional Rendering: You can conditionally render a message based on a certain condition in your component. For example:
function MyComponent() {
  const showMessage = true;

  return (
    <div>
      {showMessage && <p>Hello, world!</p>}
    </div>
  );
}
  • React-Toastify: React-Toastify, as mentioned earlier, is a popular library specifically designed for displaying toast notifications in React applications. It provides customizable and visually appealing toast messages. Here’s an example of using React-Toastify:
import { toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

function MyComponent() {
  const showMessage = () => {
    toast.info('Hello, world!');
  };

  return (
    <div>
      <button onClick={showMessage}>Show Message</button>
    </div>
  );
}
  • Third-Party Libraries: There are other libraries available that offer message display functionality, such as react-s-alert, react-notifications-component, and react-notification-system. These libraries provide various options and features for showing messages in your React application.
  • Custom Components: You can create your own custom components to display messages in a way that suits your application’s design and requirements. This approach allows for complete control over the message display and styling.

What are toast notifications?

A toast notification is a small, non-intrusive message that appears temporarily on a user’s screen to provide brief and contextual information. It typically pops up at the edge or corner of the screen and then fades away after a certain period of time or when the user dismisses it.

Some common use cases for toast notifications include Success or confirmation messages, Error or warning messages, System or application updates, and Real-time updates.

What is React Toastify?

React Toastify is a highly popular and widely used library for displaying toast notifications in React applications. With React Toastify, you can effortlessly showcase temporary messages, alerts, or notifications to users in an attractive and user-friendly manner. This library offers a customizable toast appearance, easy integration, event-handling capabilities, and strong accessibility support. React Toastify has an active community, ensuring regular updates and reliable functionality. If you’re looking to enhance your React application with visually appealing and informative toast notifications, React Toastify is an excellent choice. For more information, you can refer react-toastify link for further reference.

How popular is react-toastify?

React Toastify is the most popular NPM library for displaying toast messages in React.

npm trends of  react-confirm-alert, react-hot-toast, and react-toastify
popularity of react-toastify

According to NPM Trends, it significantly surpasses its closest competitor, react-hot-toast, with over 2.5 times more downloads. In the week of 7-May-2023, react-toastify had 1.41 million downloads, while react-hot-toast had 565K downloads. Other libraries like react-confirm-alert, react-toastr, and react-toasts fall far behind the top two choices for showing toast messages in React.

For adding toast messages to your React.js app, React Toastify is the safest and most recommended option, followed by React Hot Toast.

Why use react-toastify?

React-Toastify offers several advantages that make it a popular choice for displaying toast messages in React applications:

  1. Easy Integration: React-Toastify is straightforward to integrate into your React application. With a simple installation process and a few lines of code, you can quickly start showing toast notifications.
  2. Feature-Rich: React-Toastify provides a wide range of features to enhance your toast notifications. You can customize the appearance, position, and animation of the toasts. It also supports different types of toasts, such as success, error, warning, and info, allowing you to convey various message types effectively.
  3. Flexibility: The library offers flexibility in terms of configuration options. You can configure global settings for all toasts or provide specific options for individual toasts. This flexibility allows you to tailor the toast behavior to suit your application’s specific needs.
  4. Responsive Design: React-Toastify is built with responsive design principles in mind. The toasts automatically adjust their position and size based on the screen size, ensuring a consistent and user-friendly experience across different devices.
  5. Customizability: You have the freedom to customize the toast notifications according to your application’s branding and design. React-Toastify provides CSS classes that you can override or extend to match your desired styles. This allows you to create a cohesive and visually appealing UI.
  6. Accessibility: React-Toastify supports accessibility features, ensuring that users with disabilities can access and understand the toast notifications. It follows best practices for accessibility, such as providing ARIA attributes and keyboard navigation support.
  7. Active Community: React-Toastify has a vibrant community of developers who actively contribute to its development and provide support. This means you can benefit from ongoing improvements, bug fixes, and new features, ensuring a reliable and up-to-date tool for displaying toast messages.

Overall, React-Toastify simplifies the process of implementing toast notifications in React applications while providing a rich set of features and customization options. It allows you to effectively communicate important messages to users in an intuitive and visually appealing way.

How to use React Toastify?

To use React Toastify in your React application, follow these simple steps:

How to install React Toastify?

Start by installing the React Toastify package. Open your command line interface and run the following command:

npm install react-toastify

Import Required Components

In your React component file, import the necessary components from React Toastify:

import { ToastContainer, toast } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';

Add the ToastContainer Component

Within your component’s JSX code, add the `<ToastContainer />` component. This component will render the toast notifications:

return (
  <div>
    {/* Other components */}
    <ToastContainer />
  </div>
);

Display Toast Notifications

To display a toast notification, call the `toast` function from React Toastify. For example, you can use a button’s `onClick` event to trigger a toast:

<button onClick={() => toast("Hello, world!")}>Show Toast</button>

Customizing React Toastify (Optional)

React Toastify provides various options to customize toast notifications. You can pass additional options as an object parameter to the `toast` function. For example:

const showSuccessMessage = () => {
    toast.success("Success Notification !", {
      position: toast.POSITION.TOP_RIGHT,
    });
};
.
.
.
<div className="col-sm">
   <button onClick={showSuccessMessage}>Success Notification</button>
</div>

Explore Further Configuration

React Toastify offers additional configuration options for further customization. You can refer to the official documentation to learn about advanced features and global configuration settings.

By following these steps, you can easily incorporate React Toastify into your React application and display toast notifications with ease. Make sure to leverage the available customization options to enhance the user experience. Happy toasting!

react-toastify demo
Output of the demo

Conclusion

React Toastify proves to be a powerful tool for implementing toast notifications in React applications. With its easy integration, customizable appearance, and flexibility in usage, React Toastify allows developers to enhance the user experience by displaying temporary messages and alerts in an attractive and non-intrusive manner. By following the steps outlined in this guide, you can quickly integrate React Toastify into your React application and provide users with informative and visually appealing toast notifications. Whether it’s conveying success messages, error alerts, or important updates, React Toastify offers a simple yet effective solution to enhance interactivity and improve user satisfaction in your React projects.

To read more about hooks refer to the links given below:
useEffect: The Effect Hook
useState: Managing State in React functional components

Why use React?
Fundamentals of Routing
How to Improve React Performance with Lazy Loading?
Next.js: The Powerhouse Framework for React Projects

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *