<?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: Suman Ghosh</title>
    <description>The latest articles on DEV Community by Suman Ghosh (@suman85).</description>
    <link>https://dev.to/suman85</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%2F1073970%2F6f9f828b-6d23-4f99-ba69-ced8afdc322a.jpeg</url>
      <title>DEV Community: Suman Ghosh</title>
      <link>https://dev.to/suman85</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suman85"/>
    <language>en</language>
    <item>
      <title>React Messaging Design</title>
      <dc:creator>Suman Ghosh</dc:creator>
      <pubDate>Mon, 21 Jul 2025 17:53:23 +0000</pubDate>
      <link>https://dev.to/suman85/react-messaging-design-2ia5</link>
      <guid>https://dev.to/suman85/react-messaging-design-2ia5</guid>
      <description>&lt;h2&gt;
  
  
  Problem Statement:
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;How do various components communicate with each other?&lt;br&gt;
Problem is to communicate each other component like we have parent component A and child &lt;br&gt;
components b and c and d so we want when we click on b components c and d component also &lt;br&gt;
change for this problem we can use different ways using global library like redux, zustand, jotai but this architecture is hard to manage &lt;br&gt;
 write more code manage many file and slow performance so for solve this problem simply we create a global hub where we manage this so first when we click any component its go to global component and send information what they want &lt;br&gt;
to send and the global hub react according to it and send data to child component .&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;We can use a custom hook that allows us to decouple event handling and data flow. Instead of relying on props drilling for passing data and events between deeply nested components, we can create an event-driven architecture. In this solution, the custom hook helps listen to events &lt;br&gt;
and allows components to communicate without having to pass data through intermediate &lt;br&gt;
components.&lt;/p&gt;
&lt;h2&gt;
  
  
  Custom Hook (For React Only):
&lt;/h2&gt;

&lt;p&gt;The useEvent hook allows us to subscribe to specific events and dispatch events to notify &lt;br&gt;
listeners across different components. It enables components to react to events like mapClick &lt;br&gt;
and perform actions, such as updating state or logging data, without the need for props to be &lt;br&gt;
passed explicitly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect, useCallback } from "react";
interface AppEvent&amp;lt;PayloadType = unknown&amp;gt; extends Event {
 detail: PayloadType;
}
export const useEvent = &amp;lt;PayloadType = unknown&amp;gt;(
 eventName: string,
 callback?: (payload: PayloadType) =&amp;gt; void,
) =&amp;gt;  useEffect(() =&amp;gt; {
 if (!callback) return;
 const listener = (event: CustomEvent&amp;lt;PayloadType&amp;gt;) =&amp;gt; {
 callback(event.detail);
 };
 window.addEventListener(eventName, listener);
 return () =&amp;gt; {
 window.removeEventListener(eventName, listener);
 };
 }, [callback, eventName]);
 const subscribe = useCallback(
 ({ source, input, data }: { source: string; input: string; data: any[] }) =&amp;gt; {
 const event = new CustomEvent(source, {
 detail: { input, data },
 });
 window.dispatchEvent(event);
 },
 [],
 );
 return { subscribe };
};         
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How It Works:
&lt;/h2&gt;

&lt;p&gt;Listening for Events: The useEvent hook listens for a specific event (e.g., mapClick) and &lt;br&gt;
executes the provided callback function when that event is triggered&lt;br&gt;
Subscribing to Events: The subscribe function inside the hook allows components to &lt;br&gt;
trigger events by dispatching them with custom data. In the code example, the subscribe &lt;br&gt;
function is used to send selected data when a map is clicked.&lt;br&gt;
Decoupling Components: With this setup, components no longer need to pass props &lt;br&gt;
down the hierarchy. Instead, they can use the useEvent hook to subscribe to and &lt;br&gt;
dispatch events, making communication between components more efficient and decoupled.&lt;/p&gt;

&lt;p&gt;Example Usage:&lt;br&gt;
In the application, we have the following usage:&lt;br&gt;
Subscribing to the mapClick event:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { subscribe } = useEvent("mapClick");
subscribe({
 source: "mapClick",
 input: "select",
 data: [selectedUnitIds],
});
Handling the event:
useEvent("mapClick", (data: any) =&amp;gt; {
 console.log(data);
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Benefits:
&lt;/h2&gt;

&lt;p&gt;Decoupled Architecture: Components no longer need to be aware of each other's &lt;br&gt;
internal state or pass props down multiple layers. They can communicate via events.&lt;br&gt;
Simplified Code: The custom hook abstracts away the complexity of handling events and &lt;br&gt;
subscribing to them, making the code easier to maintain and extend.&lt;br&gt;
Reusable Logic: The useEvent hook can be reused across different components that &lt;br&gt;
need to listen for or trigger events, leading to a more modular design.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
      <category>redux</category>
    </item>
    <item>
      <title>Hello 👋</title>
      <dc:creator>Suman Ghosh</dc:creator>
      <pubDate>Sat, 29 Apr 2023 14:37:15 +0000</pubDate>
      <link>https://dev.to/suman85/hello-3m4d</link>
      <guid>https://dev.to/suman85/hello-3m4d</guid>
      <description>&lt;p&gt;&lt;code&gt;console.log("i am new");&lt;/code&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
