In this Answer, we will go over how to handle notifications in our React project. First, we’ll be using react-notifications
, a notification component for React.
Go to the project directory and install the following npm
package:
npm install --save react-notifications
or
yarn add react-notifications
Now, update the App.js
file. Import NotificationContainer
from react-notifications
and the notifications.css
file.
...
// React Notification
import '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: Only use one
NotificationContainer
component in the app.
Now, it’s time to pass notifications from different components or originate from the App.js
to display their message.
// React Notification
import { NotificationManager } from 'react-notifications';
// Add this line where you want to show the notification
NotificationManager.info('Hey I am Adyasha', 'Info!', 2000);
Hooray, we did it! Now we can run our project.
NotificationManager
APIsYou 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. They are of types:
Here’s 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);
For the other types, we can replace success
with the required type such as error
, info
, and warning
.
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:
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 will execute 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.
Let’s run the following code that originates notifications from the App.js
file and get an idea of how react-notifications
works.
.App { text-align: center; } .App-header { background-color: white; min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: calc(10px + 2vmin); color: white; } .btncss { background-color: #3498db; /* Button background color */ color: white; /* Text color */ border: none; /* Remove button border */ border-radius: 5px; /* Rounded corners */ padding: 10px 20px; /* Padding inside the button */ font-size: 16px; /* Text size */ cursor: pointer; /* Add a pointer cursor on hover */ }
Free Resources