<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Victor Eleanya</title>
    <description>The latest articles on DEV Community by Victor Eleanya (@mrvicthor).</description>
    <link>https://dev.to/mrvicthor</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F212519%2Fffdf0a0d-f656-4522-ac4a-8b33d290721f.jpeg</url>
      <title>DEV Community: Victor Eleanya</title>
      <link>https://dev.to/mrvicthor</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mrvicthor"/>
    <language>en</language>
    <item>
      <title>How I created my custom react notification component using React createContext hook, and Typescript</title>
      <dc:creator>Victor Eleanya</dc:creator>
      <pubDate>Sat, 26 Oct 2024 12:50:21 +0000</pubDate>
      <link>https://dev.to/mrvicthor/how-i-created-my-custom-react-notification-component-using-react-createcontext-hook-and-typescript-3dfi</link>
      <guid>https://dev.to/mrvicthor/how-i-created-my-custom-react-notification-component-using-react-createcontext-hook-and-typescript-3dfi</guid>
      <description>&lt;p&gt;Imagine an app without notifications. Bad user experience, right? How will the user know if an action/event has been executed? Hence, the importance of notifications. &lt;/p&gt;

&lt;p&gt;Overtime, I have always used either react-toast or react-hook-toast to handle notifications. However, out of a budding curiosity, i chose to create my own custom Notification component from scratch using react createContext hook.&lt;/p&gt;

&lt;p&gt;Initially, i tried creating this as a custom hook. However, the challenge was managing and keeping the state consistent. Hence, I chose to use react createContext hook.&lt;/p&gt;

&lt;p&gt;The benefit of react createContext hook is that it helps you to pass props to only components that need it. Thereby, helping you manage the state of your react application, and avoid prop drilling.&lt;/p&gt;

&lt;p&gt;Enough yapping, below is my solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;step 1&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { createContext, useEffect, useState } from "react";

type NotificationContextType = {
  notification: boolean;
  setNotification: (value: boolean) =&amp;gt; void;
};

export const NotificationContext = createContext&amp;lt;
  NotificationContextType | undefined
&amp;gt;(undefined);

export const NotificationProvider = ({
  children,
}: {
  children: React.ReactNode;
}) =&amp;gt; {
  const [notification, setNotification] = useState&amp;lt;boolean&amp;gt;(false);
  useEffect(() =&amp;gt; {
    const interval = setTimeout(() =&amp;gt; setNotification(false), 3000);
    return () =&amp;gt; clearTimeout(interval);
  }, [notification]);

  return (
    &amp;lt;NotificationContext.Provider value={{ notification, setNotification }}&amp;gt;
      {children}
    &amp;lt;/NotificationContext.Provider&amp;gt;
  );
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code above, I defined the type for my context &lt;strong&gt;NotificationContextType&lt;/strong&gt;, then I created a &lt;strong&gt;NotificationContext&lt;/strong&gt; constant. Afterwards, I created the &lt;strong&gt;NotificationProvider&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;step 2&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useContext } from "react";
import { NotificationContext } from "@/context/NotificationContext";

export const useNotification = () =&amp;gt; {
  const context = useContext(NotificationContext);
  if (!context) {
    throw new Error(
      "useNotification must be used within a NotificationProvider"
    );
  }
  return context;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I created a useNotification Hook.&lt;/p&gt;

&lt;p&gt;Finally, below is the component where it is being used.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type NotificationProps = {
  message: string;
};

const Notification = ({ message }: NotificationProps) =&amp;gt; {
  const { notification } = useNotification();

  if (!notification) return null;
  return (
    &amp;lt;div className="fixed bottom-[5rem] flex justify-center w-full"&amp;gt;
      &amp;lt;div
        className={`
         bg-[#333333] rounded-md p-4 text-white m-auto flex gap-2  w-[22rem]`}
      &amp;gt;
        &amp;lt;img src='image.png' alt="link-copied-icon" /&amp;gt;
        &amp;lt;p&amp;gt;{message}&amp;lt;/p&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Notification;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useNotification } from "@/hooks/useNotification";

const PreviewHeader = () =&amp;gt; {
  const { setNotification } = useNotification();
  return (
    &amp;lt;header className=" h-[4.875rem] container"&amp;gt;
      &amp;lt;nav className="bg-[#ffffff] w-full  flex h-full rounded-md items-center justify-between pl-6 pr-4"&amp;gt;
        &amp;lt;NavLink
          to="/profile"
          className="text-[#633cff] border border-[#633CFF] px-4 py-2 text-sm rounded-md font-medium"
        &amp;gt;
          Back to Editor
        &amp;lt;/NavLink&amp;gt;
        &amp;lt;Button
          variant={"saveButton"}
          className="bg-[#633cff]"
          onClick={() =&amp;gt; setNotification(true)}
        &amp;gt;
          Share Link
        &amp;lt;/Button&amp;gt;
      &amp;lt;/nav&amp;gt;
    &amp;lt;/header&amp;gt;
  );
};

export default PreviewHeader;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. &lt;br&gt;
Please feel free to share your thoughts. Thanks&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
