How to Create a Custom Notification Component in React

There are several third-party packages available in React that can help you create a notification component. However, if you just want a simple notification component, you may want to create your own to avoid adding unnecessary dependencies to your application.

Setting Up the Project

You willuse Vite to set up the React app. Vite is a build tool that lets you quickly scaffold a React project.

To get started, use the yarn package manager to create a new Vite project by running the command below.

4

The command will prompt you to enter a project name. Enter the name of the project and press Enter. Next, it will prompt you to select a framework. Choosereactand press Enter. Finally, it will ask you to select a variant, chooseJavaScriptand then press Enter.

Here are the configurations this tutorial will use:

Screenshot of apps showing notifications in the mail app.

After Vite creates the project, navigate to the project folder and open it using a code editor.

You can then start the development server by running the following command.

screenshot of the project configuration

This will open your new React application in your default browser at http://localhost:5173/.

Designing the Notification Component

In order to create a flexible notification component, it needs to be able to handle different types of notifications with varying titles, descriptions, and styles. For example, it needs to render a warning, success, and error notification.

Here are different variations the notification component should be able to render.

Screenshot of different notifications

You can achieve this by passing props to the component that specifies the type of notification to render, the title, and the description text. By using these props, you can customize the component and reuse it throughout your application with minimal effort. If you need a refresher on props, here is an article that explainshow to use props in React.

Creating the Notification Component

In thesrcfolder, create a new file namedNotification.jsxand add the following code.

This code creates a functional component calledNotificationwith three props:type,title, anddescription. You will use these props to customize the notification’s style and content.

A notebook that’s drawn UI wireframes on a brown table

From the design, the component has a couple of icons, namely information, and a cross icon. You candownload free-to-use iconsor use an icon component from a package such asreact-icons. This tutorial will usereact-iconsso install it by running the command below.

Then import the icons at the top of theNotificationcomponent.

Now, you can modify the component to use the icons, the title, and the description prop values to create the content of the notification.

The next step is to style it depending on the type passed in.

One approach you can take is to define CSS classes for each type of notification you want to display. You can then conditionally apply the appropriate class based on the type that is passed in.

To begin, create a new file callednotification.cssand import it inNotification.jsxby adding the following code at the top.

Then innotification.cssdefine the base styles for the notification component:

You can then define the styles for the different notification types by adding the following code in the CSS file.

The above code styles the notification container based on the type passed in.

To customize the title, use the following styles.

For the custom description text, use these styles.

And for the icons, use the following classes.

Then, in theNotificationcomponent, you can conditionally apply the appropriate class based on thetypeprop, like this:

In this component, you are dynamically setting the class depending on the type such asnotification__successornotification__error.

To see this in action, import the Notification component inApp.jsxand use it as follows:

Now, you can pass a different type to theNotificationcomponent and render an appropriate notification that matches the message.

This is essential for a good user experience because users have come to associate different colors and styles with different scenarios, and it’s important to use those associations consistently. For example, it would be confusing to let a user know they successfully uploaded a photo in a red notification box. They might think the upload failed, even if it was successful.

Adding Interactivity to the Notification Component

You’ve learned how you’re able to use props to create a customizable notification component. To take it even further, you can add transitions to this component to make it more engaging. For example, you can use CSS animations to slide the notification component to the screen and slide it out after a certain duration has passed.

UI libraries help you deliver a first-class user experience, and accessibility is a vital part of that.

Make sure you don’t miss these movies and shows before Netflix removes them.

I found my TV was always listening—so I shut it down.

You can’t call this offline, Notion.

Free AI tools are legitimately powerful; you just need to know how to stack them.

One casual AI chat exposed how vulnerable I was.

Technology Explained

PC & Mobile