<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://userhundredth.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 18:28:31 GMT</lastBuildDate><atom:link href="https://userhundredth.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Create Notification Component in React: A Step-by-Step Guide]]></title><description><![CDATA[How to Create a Notification Component in React
Notifications play a crucial role in user interfaces, providing important information or updates to users. In React applications, creating notifications can be achieved with relative ease, offering a se...]]></description><link>https://userhundredth.hashnode.dev/how-to-create-notification-component-in-react-a-step-by-step-guide</link><guid isPermaLink="true">https://userhundredth.hashnode.dev/how-to-create-notification-component-in-react-a-step-by-step-guide</guid><category><![CDATA[react js]]></category><category><![CDATA[JavaScript]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[Tailwind CSS]]></category><category><![CDATA[components]]></category><dc:creator><![CDATA[shubham patil ]]></dc:creator><pubDate>Wed, 03 Apr 2024 18:30:00 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1712761339131/419950c7-8be2-4d00-8908-f8267481be3b.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p><strong>How to Create a Notification Component in React</strong></p>
<p>Notifications play a crucial role in user interfaces, providing important information or updates to users. In React applications, creating notifications can be achieved with relative ease, offering a seamless way to communicate with users. In this guide, we'll walk through the process of creating notifications in a React application.</p>
<p><strong>Getting Started</strong></p>
<p>To begin, set up a basic React application with your choice of tools and styling libraries. You can also create this component in an existing React application.</p>
<p>I'm using Vite with Tailwind CSS for styling, but we will focus more on the logic part instead of styling, which you can do according to your UI needs.</p>
<p><strong>Receiving Notification Data from the Backend</strong></p>
<p>In a real-world scenario, notification data is typically fetched from a backend server. This data might include information such as notification title, message, timestamp, and other relevant details. For the purpose of this tutorial, we'll simulate dummy notification data in a separate file called <code>notificationsData.js</code>.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// notificationsData.js</span>

<span class="hljs-keyword">const</span> notificationsData = [
  {
    <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">title</span>: <span class="hljs-string">"New Message"</span>,
    <span class="hljs-attr">message</span>: <span class="hljs-string">"You have received a new message."</span>,
    <span class="hljs-attr">timestamp</span>: <span class="hljs-string">"2024-04-12T10:30:00Z"</span>
  },
  {
    <span class="hljs-attr">id</span>: <span class="hljs-number">2</span>,
    <span class="hljs-attr">title</span>: <span class="hljs-string">"New Friend Request"</span>,
    <span class="hljs-attr">message</span>: <span class="hljs-string">"You have a new friend request."</span>,
    <span class="hljs-attr">timestamp</span>: <span class="hljs-string">"2024-04-11T15:45:00Z"</span>
  },
  <span class="hljs-comment">// Add more dummy notification data as needed</span>
];

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> notificationsData;
</code></pre>
<p>In a real-world application, you would replace this dummy data with actual data fetched from your backend API.</p>
<p><strong>Creating Notification Container</strong></p>
<p>First, we will need the Notification Container inside which we will render our list of multiple notifications. We can achieve this by mapping over the notification array from the <code>notificationsData.js</code> file.</p>
<p>First, import the notification data from the <code>notificationsData.js</code> file, then create a state to hold this notification array:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [notifications, setNotifications] = useState([]);
</code></pre>
<p>Then use the <code>useEffect</code> hook to set the notification array:</p>
<pre><code class="lang-javascript">useEffect(<span class="hljs-function">() =&gt;</span> {
  setNotifications(notificationsArray);
}, []);
</code></pre>
<p>Now we will create a card with basic width and height styling inside which we will map over this notifications array:</p>
<pre><code class="lang-javascript">{notifications.map(<span class="hljs-function">(<span class="hljs-params">noti</span>) =&gt;</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Notification</span>
    <span class="hljs-attr">key</span>=<span class="hljs-string">{noti.id}</span>
    <span class="hljs-attr">title</span>=<span class="hljs-string">{noti.title}</span>
    <span class="hljs-attr">message</span>=<span class="hljs-string">{noti.message}</span>
  /&gt;</span></span>
))}
</code></pre>
<p>We will render each Notification component like a list. For this, we will need to create our main component, which is <code>&lt;Notification/&gt;</code>. This component receives three props:</p>
<ul>
<li><p><code>key={</code><a target="_blank" href="http://noti.id"><code>noti.id</code></a><code>}</code></p>
</li>
<li><p><code>title={noti.title}</code></p>
</li>
<li><p><code>message={noti.message}</code></p>
</li>
</ul>
<p><strong><em>App.jsx :</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useEffect, useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> notificationsArray <span class="hljs-keyword">from</span> <span class="hljs-string">"./data/data"</span>;
<span class="hljs-keyword">import</span> Notification <span class="hljs-keyword">from</span> <span class="hljs-string">"./components/Notification"</span>;

<span class="hljs-keyword">const</span> StarterTemplateInstructions = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [notifications, setNotifications] = useState([]);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    setNotifications(notificationsArray);
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-start justify-center "</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"my-20 bg-base-300 card w-[500px] text-wrap rounded-none"</span>&gt;</span>
          {notifications.map((noti) =&gt; (
            <span class="hljs-tag">&lt;<span class="hljs-name">Notification</span>
              <span class="hljs-attr">key</span>=<span class="hljs-string">{noti.id}</span>
              <span class="hljs-attr">title</span>=<span class="hljs-string">{noti.title}</span>
              <span class="hljs-attr">message</span>=<span class="hljs-string">{noti.message}</span>
            /&gt;</span>
          ))}
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> StarterTemplateInstructions;
</code></pre>
<p>Now let's move on to creating <code>Notification.jsx</code>.</p>
<p>In this component, we need to implement the logic to close the notification. For this, we will create a boolean state:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [show, setShow] = useState(<span class="hljs-literal">true</span>);
</code></pre>
<p>And whenever the user clicks on the close button, it will have a <code>handleClick</code> function:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> handleClose = <span class="hljs-function">() =&gt;</span> {
  setShow(<span class="hljs-literal">false</span>);
};
</code></pre>
<p>Next, we will render our component's Title and Message with appropriate styling, and for the close button, I'm using an <code>img</code> tag which displays an SVG of the close button. This <code>img</code> has an <code>onClick</code> handler for which we set the <code>handleClick</code> function earlier.4</p>
<p><strong><em>Notification.jsx</em></strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> close <span class="hljs-keyword">from</span> <span class="hljs-string">"../assets/close.svg"</span>;

<span class="hljs-keyword">const</span> Notification = <span class="hljs-function">(<span class="hljs-params">{ title, message }</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> [show, setShow] = useState(<span class="hljs-literal">true</span>);

  <span class="hljs-keyword">const</span> handleClose = <span class="hljs-function">() =&gt;</span> {
    setShow(<span class="hljs-literal">false</span>);
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      {show &amp;&amp; (
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"bg-gray-950 px-4 py-2 outline outline-1  "</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"font-semibold text-slate-100 items-start"</span>&gt;</span>{title}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"flex items-center justify-between "</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"block sm:inline text-slate-400"</span>&gt;</span>{message}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">""</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">img</span>
                <span class="hljs-attr">src</span>=<span class="hljs-string">{close}</span>
                <span class="hljs-attr">alt</span>=<span class="hljs-string">"Vite logo"</span>
                <span class="hljs-attr">className</span>=<span class="hljs-string">"cursor-pointer"</span>
                <span class="hljs-attr">width</span>=<span class="hljs-string">{30}</span>
                <span class="hljs-attr">height</span>=<span class="hljs-string">{50}</span>
                <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleClose}</span>
              /&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      )}
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
};

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> Notification;
</code></pre>
<p>And that's it! This is very simple to achieve once you get the grasp of it. Usually, you won't be using a card as a container to show the notifications; instead, you might use a dropdown in the navigation bar of the website, but that is another topic for a blog post.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>n conclusion, creating a notification component in React is a straightforward process that can greatly enhance user experience in your application. By following the steps outlined in this guide, you can efficiently receive notification data from a backend server, create a notification container, and implement the logic for displaying and closing notifications.</p>
<p>Remember to adapt the styling and placement of notifications according to your application's design and user interface requirements. Whether you choose to display notifications in a card format or utilize a dropdown menu, the key is to ensure that notifications are easily accessible and provide valuable information to the user.</p>
<p>I hope this guide has been helpful in understanding how to create notifications in React. Happy coding!</p>
<h3 id="heading-output">Output :</h3>
<p>This is how the notification card looks like:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1712750999586/d7d84142-e450-49fc-ba98-e3cc38e47ff6.png" alt class="image--center mx-auto" /></p>
]]></content:encoded></item></channel></rss>