Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
April 20, 2021 11:21 am GMT

React-Notifications

Here Ill show how we can handle notifications in our React project. First, well be using react-notifications, which, as its name suggests, is a notification component for React.

Notifications

Installation:

Go to the client project directory and install the following npm package:

npm install --save react-notifications
or
yarn add react-notifications

Setting up the Notification container:

Now update the App.js file. Import NotificationContainer from react-notifications and the notifications.css file.

...// React Notificationimport 'react-notifications/lib/notifications.css';import { NotificationContainer } from 'react-notifications';......return ( <Router>  <div>   ...   ...   <NotificationContainer />  </div> </Router>);

So far, we have completed our setup for NotificationContainer.

NOTE: Use only one NotificationContainer component in the app.

Now its time to pass notifications from different components to display their message.

Setting Notifications from components:

// React Notificationimport { NotificationManager } from 'react-notifications';// Add this line where you want to show the notificationNotificationManager.info('Hey I am Adyasha', 'Info!', 2000);

Horray, you did it. Now you can run your project.

Available NotificationManager APIs:

You can apply this same method to different components in your project. Notifications will be displayed in different colors depending on the notification type.
For this package, there are four different APIs available to us of the following types:

  • info
  • success
  • warning
  • error

Heres an example for the success type simply replace success with the proper notification type for the given situation :

NotificationManager.success(message, title, timeOut, callback, priority);

You can also pass five different parameters along with the message: message, title, timeOut, callback, and priority.

The parameters that follow the notification type are described below:

  • message: The message we want to pass. It has to be a string.

  • title: The title of the notification. Again, its type is a string.

  • timeOut: The popup timeout in milliseconds. This has to be an integer.

  • callback: We can pass a function (type; function) through the notification. It executes after the popup is called.

  • priority: This is a boolean parameter. We can push any notification to the top at any point by setting the priority to true.

That's all. Thanks for your patience ! Have a nice day ;)

Find me on Twitter @Adyasha8105.


Original Link: https://dev.to/adyasha8105/react-notifications-33do

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To