<?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: Hossain MD Athar</title>
    <description>The latest articles on DEV Community by Hossain MD Athar (@hossain45).</description>
    <link>https://dev.to/hossain45</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%2F1137388%2Fd9564303-d558-4458-98ac-a6c8932ecda8.JPG</url>
      <title>DEV Community: Hossain MD Athar</title>
      <link>https://dev.to/hossain45</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hossain45"/>
    <language>en</language>
    <item>
      <title>Stop Using “?” Everywhere! Use `Omit` ( i.e. Utility Types) for Safer TypeScript</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Sat, 29 Nov 2025 05:21:39 +0000</pubDate>
      <link>https://dev.to/hossain45/stop-using-everywhere-use-omit-utility-types-for-safer-typescript-3gd9</link>
      <guid>https://dev.to/hossain45/stop-using-everywhere-use-omit-utility-types-for-safer-typescript-3gd9</guid>
      <description>&lt;p&gt;TypeScript was created for one purpose: &lt;strong&gt;to bring safety and clarity to JavaScript&lt;/strong&gt;. When Microsoft first released it, &lt;a href="https://www.typescriptlang.org/docs/handbook/typescript-from-scratch.html" rel="noopener noreferrer"&gt;their mission&lt;/a&gt; was simple - give developers predictable types, reduce runtime bugs, and make debugging easier.&lt;/p&gt;

&lt;p&gt;But even with all the guardrails TypeScript provides, there’s one tiny operator that many developers misuse without realizing the long-term cost:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;?&lt;/code&gt; — the optional modifier.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It looks harmless. It’s convenient. But using it in the wrong places quietly destroys the type safety TypeScript was built for.&lt;/p&gt;

&lt;p&gt;Let’s talk about why — with real-world examples, better patterns, and official TS-backed solutions.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Where Things Start to Go Wrong&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Imagine a simple model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A &lt;code&gt;User&lt;/code&gt; &lt;em&gt;always&lt;/em&gt; has an ID.&lt;br&gt;
This is part of your domain logic. The backend guarantees it.&lt;/p&gt;

&lt;p&gt;But when you’re building a form to &lt;em&gt;create&lt;/em&gt; a user, you don’t have an &lt;code&gt;id&lt;/code&gt; yet. And this is where many developers introduce a subtle bug:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks tiny, but this changes everything. Because now, TypeScript thinks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A user &lt;strong&gt;might&lt;/strong&gt; have an ID&lt;/li&gt;
&lt;li&gt;Or might &lt;strong&gt;not&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Anywhere in the entire codebase&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Your core type becomes weaker, and the compiler loses its ability to actually protect you.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Real Life Example: When TS Yells at You&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Let me show you how this problem shows up at work. I had code like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customerId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;updatedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;updateCustomerById&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing looks wrong…&lt;br&gt;
But TypeScript hits you with this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'client' is possibly 'undefined'.ts(*****)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why?&lt;br&gt;
Because somewhere in the type definitions, someone got lazy and wrote:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;...&lt;/span&gt; 
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That &lt;strong&gt;one little &lt;code&gt;?&lt;/code&gt;&lt;/strong&gt; forces TypeScript to assume:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;client.customerId&lt;/code&gt; might be undefined&lt;/li&gt;
&lt;li&gt;and you must guard every line of code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So you end up writing completely unnecessary defensive code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Client id missing!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// or maybe, you add condition in submit function &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The real problem is the &lt;strong&gt;type design&lt;/strong&gt;, not the logic. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Fix the TypeScript Docs Recommend&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;TypeScript’s official documentation gives us tools for this exact situation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;TypeScript provides several utility types to facilitate common type transformations. These utilities are available globally. [TS docs]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The solution is &lt;strong&gt;not&lt;/strong&gt; to make everything optional.&lt;/p&gt;

&lt;p&gt;The solution is to derive &lt;strong&gt;specific types for specific contexts&lt;/strong&gt; using &lt;a href="https://www.typescriptlang.org/docs/handbook/utility-types.html" rel="noopener noreferrer"&gt;utility types.&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Use &lt;code&gt;Omit&lt;/code&gt; — Not &lt;code&gt;?&lt;/code&gt;&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;If a &lt;code&gt;User&lt;/code&gt; always has an ID, but your &lt;em&gt;form&lt;/em&gt; doesn’t, you create a separate type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CreateUserInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Omit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done.&lt;br&gt;
Clean.&lt;br&gt;
Safe.&lt;br&gt;
Predictable.&lt;/p&gt;

&lt;p&gt;Your domain stays strict:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is exactly what TypeScript recommends in its documentation on building new types from existing ones. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Final Thoughts&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The next time you feel the urge to sprinkle &lt;code&gt;?&lt;/code&gt; everywhere to make TS stop complaining, pause a moment and ask:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;“Am I modeling reality… or am I just silencing TypeScript?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;Omit&lt;/code&gt;, &lt;code&gt;Pick&lt;/code&gt;, and TypeScript’s built-in utilities. Your future self — and TypeScript — will thank you. Explore more and comment below how utility types saved you!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>RTK Query vs Custom Axios Hooks: What Actually Happens When Your Project Grows</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Sun, 23 Nov 2025 11:51:05 +0000</pubDate>
      <link>https://dev.to/hossain45/rtk-query-vs-custom-axios-hooks-what-actually-happens-when-your-project-grows-b69</link>
      <guid>https://dev.to/hossain45/rtk-query-vs-custom-axios-hooks-what-actually-happens-when-your-project-grows-b69</guid>
      <description>&lt;p&gt;I started a React project with the usual “simple” setup: Axios, a custom data-fetching hook, and a clean little useEffect here and there. Early on, it felt totally manageable. One hook per endpoint, a bit of state for loading, and everything stayed readable.&lt;/p&gt;

&lt;p&gt;But once the app grew, the weaknesses of that setup showed up fast. Different parts of the app needed the same data, hooks started duplicating logic, and suddenly every new feature required more boilerplate. I kept feeling like I was rebuilding the same patterns across the codebase.&lt;/p&gt;

&lt;p&gt;That’s when I committed to RTK Query—and the difference was immediate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The RTK Query Version (clean, obvious, predictable)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Fetching data with RTK Query feels almost peaceful. The intent of the component is clear at a glance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dispatch = useDispatch();

// RTK Query: fetch ticket data with automatic caching, loading state, and refetch logic
const { data: ticket, isLoading } = useGetTicketsByCompanyIdQuery(
  company?.id ?? 0,
  { skip: !company?.id }
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s the entire fetching setup. No manual state, no useEffect, no memoization, no “did this refetch correctly?” debugging.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it reads better&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The component tells you what data it needs, not how to get it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it scales better&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Data is cached and shared automatically across components. Duplicate requests disappear. Adding new endpoints keeps code centralized (API slice), not scattered across hooks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why it’s more efficient&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Built-in caching prevents redundant network calls. Invalidations and refetching are handled by a unified system, not random effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Axios Hook Version (works… until it doesn’t)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here’s the hook-based version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const dispatch = useDispatch();

// Custom hook using Axios
const { fetchData } = useGetData&amp;lt;TicketDetails[]&amp;gt;();

useEffect(() =&amp;gt; {
  const fetchTickets = async () =&amp;gt; {
    const apiRoute = `/api/tickets/company/${company?.id}`;
    const response = await fetchData(apiRoute);
    response &amp;amp;&amp;amp; setTickets(response); // assuming local useState
  };

  fetchTickets();
}, [company, fetchData]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nothing wrong with it… but look at all the moving parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual loading state&lt;/li&gt;
&lt;li&gt;Manual caching (if you want it)&lt;/li&gt;
&lt;li&gt;Manual dependency management&lt;/li&gt;
&lt;li&gt;Manual refetch rules&lt;/li&gt;
&lt;li&gt;Manual error handling&lt;/li&gt;
&lt;li&gt;Local state everywhere&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This grows messy quickly because each component is responsible for its own data lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Readability issues&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your UI logic gets mixed with loading logic, effect logic, error handling, and state updates. Two months later, you forget why something re-fetches—or why it doesn’t.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Scalability issues&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As soon as multiple components need the same API data, you either: duplicate calls, or build your own caching layer (which… is basically reinventing RTK Query).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Efficiency issues&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Every mount triggers a fetch unless you build custom memoization/caching. Not wrong—just extra work.&lt;/p&gt;

&lt;p&gt;RTK Query gives you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;shared data&lt;/li&gt;
&lt;li&gt;request deduping&lt;/li&gt;
&lt;li&gt;caching&lt;/li&gt;
&lt;li&gt;invalidation&lt;/li&gt;
&lt;li&gt;polling&lt;/li&gt;
&lt;li&gt;refetch-on-focus&lt;/li&gt;
&lt;li&gt;consistent patterns&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without you maintaining any of it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final Thoughts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your project is tiny, Axios hooks are fine. But once your state logic starts spreading out—multiple components relying on shared data, multiple endpoints, or complex flows—RTK Query shines.&lt;/p&gt;

&lt;p&gt;You get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;cleaner components&lt;/li&gt;
&lt;li&gt;clearer intent&lt;/li&gt;
&lt;li&gt;centralized logic&lt;/li&gt;
&lt;li&gt;fewer bugs&lt;/li&gt;
&lt;li&gt;fewer network calls&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’re feeling your data-fetching logic getting harder to wrangle, that’s usually the moment RTK Query becomes worth it. It made my project much easier to maintain as it grew.&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>react</category>
      <category>redux</category>
      <category>axios</category>
    </item>
    <item>
      <title>Answer: React-PDF Slow Performance with large PDF, reneders unneccessarily.</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Wed, 19 Nov 2025 06:35:56 +0000</pubDate>
      <link>https://dev.to/hossain45/answer-react-pdf-slow-performance-with-large-pdf-reneders-unneccessarily-5f6</link>
      <guid>https://dev.to/hossain45/answer-react-pdf-slow-performance-with-large-pdf-reneders-unneccessarily-5f6</guid>
      <description>&lt;p&gt;If, in 2025, anyone is facing &lt;a href="https://stackoverflow.com/a/79824125/23295636" rel="noopener noreferrer"&gt;the same issue&lt;/a&gt; with react-pdf causing excessive bundle size or initial page load lag, you can try the following approach. Instead of using React.lazy() (which is for JSX components), I'm using standard dynamic imports for the required functions and components. This ensures they are imported and processed only when the PDF button is clicked, not on every page render. This solves the performance and bundle size issues.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleGeneratePdf = async () =&amp;gt; {
    if (!ticket || !company) return;

    try {
      const { pdf } = await import("@react-pdf/renderer");
      const { default: CertiPdf } = await import("../CertPdf");

      // Generate PDF blob
      const blob = await pdf(&amp;lt;CertPdf company={company} ticket={ticket} /&amp;gt;).toBlob();

      // Create URL for viewing/printing
      const url = URL.createObjectURL(blob);
      setPdfUrl(url);
    } catch (error) {
      console.error("Error generating PDF:", error);
    } 
  };

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>performance</category>
      <category>react</category>
    </item>
    <item>
      <title>Answer: Cannot read properties of undefined (reading 'invalidatesTags') [duplicate]</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Sun, 26 May 2024 09:36:18 +0000</pubDate>
      <link>https://dev.to/hossain45/answer-cannot-read-properties-of-undefined-reading-invalidatestags-duplicate-igp</link>
      <guid>https://dev.to/hossain45/answer-cannot-read-properties-of-undefined-reading-invalidatestags-duplicate-igp</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&gt;
          &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/75126245/cannot-read-properties-of-undefined-reading-invalidatestags/77897380#77897380" rel="noopener noreferrer"&gt;
            &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: Cannot read properties of undefined (reading 'invalidatesTags')
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;Jan 29 '24&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/75126245/cannot-read-properties-of-undefined-reading-invalidatestags/77897380#77897380" rel="noopener noreferrer"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          3
        &lt;/div&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;I solved my problem by following their docs!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://redux-toolkit.js.org/rtk-query/api/createApi#reducerpath" rel="nofollow noreferrer noopener"&gt;This link will take you exactly where they (RTK docs) talk about it&lt;/a&gt;&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The reducerPath is a unique key that your service will be mounted to in your store. If you call createApi more than once in your application, you will need…&lt;/p&gt;
&lt;/blockquote&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    &lt;a href="https://stackoverflow.com/questions/75126245/cannot-read-properties-of-undefined-reading-invalidatestags/77897380#77897380" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Answer: How I can count how many times my component rendered in a react component</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Sun, 26 May 2024 09:08:59 +0000</pubDate>
      <link>https://dev.to/hossain45/answer-how-i-can-count-how-many-times-my-component-rendered-in-a-react-component-1nmc</link>
      <guid>https://dev.to/hossain45/answer-how-i-can-count-how-many-times-my-component-rendered-in-a-react-component-1nmc</guid>
      <description>&lt;div class="ltag__stackexchange--container"&gt;
  &lt;div class="ltag__stackexchange--title-container"&gt;
    
      &lt;div class="ltag__stackexchange--title"&gt;
        &lt;div class="ltag__stackexchange--header"&gt;
          &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackoverflow-logo-b42691ae545e4810b105ee957979a853a696085e67e43ee14c5699cf3e890fb4.svg" alt=""&gt;
          &lt;a href="https://stackoverflow.com/questions/72773150/how-i-can-count-how-many-times-my-component-rendered-in-a-react-component/78534775#78534775" rel="noopener noreferrer"&gt;
            &lt;span class="title-flare"&gt;answer&lt;/span&gt; re: How I can count how many times my component rendered in a react component
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="ltag__stackexchange--post-metadata"&gt;
          &lt;span&gt;May 26 '24&lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;a class="ltag__stackexchange--score-container" href="https://stackoverflow.com/questions/72773150/how-i-can-count-how-many-times-my-component-rendered-in-a-react-component/78534775#78534775" rel="noopener noreferrer"&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-up-eff2e2849e67d156181d258e38802c0b57fa011f74164a7f97675ca3b6ab756b.svg" alt=""&gt;
        &lt;div class="ltag__stackexchange--score-number"&gt;
          0
        &lt;/div&gt;
        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fassets.dev.to%2Fassets%2Fstackexchange-arrow-down-4349fac0dd932d284fab7e4dd9846f19a3710558efde0d2dfd05897f3eeb9aba.svg" alt=""&gt;
      &lt;/a&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--body"&gt;
    
&lt;p&gt;If you are working on a big project, create a custom hook. In this way, u can tackle unnecessary renders.&lt;/p&gt;
&lt;p&gt;Creating Custom Hook for renderCount.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;import { useRef, useEffect } from "react"
export default () =&amp;gt; {
  const renderCount = useRef(0);
  useEffect(() =&amp;gt; {
    renderCount.current++;
  });
  return renderCount.current;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;then…&lt;/p&gt;
    
  &lt;/div&gt;
  &lt;div class="ltag__stackexchange--btn--container"&gt;
    &lt;a href="https://stackoverflow.com/questions/72773150/how-i-can-count-how-many-times-my-component-rendered-in-a-react-component/78534775#78534775" class="ltag__stackexchange--btn" rel="noopener noreferrer"&gt;Open Full Answer&lt;/a&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>How to Persist User Info Using Redux Persist with TypeScript</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Mon, 13 May 2024 09:39:03 +0000</pubDate>
      <link>https://dev.to/hossain45/how-to-persist-user-info-using-redux-persist-with-typescript-54g9</link>
      <guid>https://dev.to/hossain45/how-to-persist-user-info-using-redux-persist-with-typescript-54g9</guid>
      <description>&lt;p&gt;As I was doing my React project, I was trying to persist user data using redux. And, here I am now to share with you how I solved my problem. Are we on the same page? Let's dive in!&lt;/p&gt;

&lt;p&gt;Redux Persist is a library that enables seamless persisting and rehydrating of your Redux store state. By persisting your state, you ensure that user data remains intact even if the application is refreshed or closed temporarily. Installing Redux Persist is the first step towards achieving this persistence.&lt;/p&gt;

&lt;p&gt;PS: Are you getting an error "A non-serializable value was detected in the state" using Redux-persist? Let's take of it as well.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Install Redux Persist Package
&lt;/h3&gt;

&lt;p&gt;First things first, let's get Redux Persist up and running in your project. Install the package using npm or yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;redux-persist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Setup Persist Config
&lt;/h3&gt;

&lt;p&gt;In this step, you define a persist configuration object e.g. &lt;code&gt;authPersistConfig&lt;/code&gt;. This object specifies how Redux Persist should handle the persistence of your Redux state. The key property uniquely identifies the slice of state you want to persist (in our case, "auth"). The storage property determines where the state will be stored (e.g., local storage). Additionally, you can specify any properties to &lt;a href="https://www.npmjs.com/package/redux-persist#blacklist--whitelist" rel="noopener noreferrer"&gt;blacklist&lt;/a&gt; from being persisted (e.g., "somethingTemporary"). Refer back to their official docs for additional features.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;storage&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist/lib/storage&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;persistReducer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;authReducer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./reducers/authReducer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;authPersistConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;auth&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;blacklist&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;somethingTemporary&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;persistedReducer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;persistReducer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;authPersistConfig&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;authReducer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Configure Store Setup
&lt;/h3&gt;

&lt;p&gt;Once you have your persist configuration set up, it's time to integrate it into your Redux store. You create a persisted reducer by passing your persist configuration and your reducer function to &lt;code&gt;persistReducer&lt;/code&gt;. Then, you configure your Redux store using &lt;code&gt;configureStore&lt;/code&gt; from Redux Toolkit, ensuring that the persisted reducer is included in the store configuration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;configureStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;configureStore&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;persistedReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: Debugging Error of Serialization
&lt;/h3&gt;

&lt;p&gt;We are there almost! Now we have an error. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"A non-serializable value was detected in the state"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Serialization errors can occur when trying to persist certain types of data that cannot be serialized properly (e.g., functions or circular references). Redux Persist provides a &lt;a href="https://www.npmjs.com/package/redux-persist#transforms" rel="noopener noreferrer"&gt;built-in solution for handling serialization errors&lt;/a&gt;, but in this guide, we opt for a simpler approach using &lt;a href="https://redux-toolkit.js.org/usage/usage-guide#use-with-redux-persist" rel="noopener noreferrer"&gt;Redux Toolkit Serializability Middleware&lt;/a&gt;. We configure the middleware to ignore specific actions that Redux Persist dispatches internally to serialize and deserialize state (FLUSH, REHYDRATE, etc.).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;configureStore&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getDefaultMiddleware&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@reduxjs/toolkit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FLUSH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;REHYDRATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PAUSE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PERSIST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PURGE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;REGISTER&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;persistReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;persistStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Store&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;configureStore&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;reducer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;auth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;persistedReducer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;middleware&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;getDefaultMiddleware&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="nf"&gt;getDefaultMiddleware&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;serializableCheck&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;ignoredActions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;FLUSH&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;REHYDRATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PAUSE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PERSIST&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PURGE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;REGISTER&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: Export Persistor
&lt;/h3&gt;

&lt;p&gt;After configuring Redux store with Redux Persist, export a persistor object using &lt;code&gt;persistStore&lt;/code&gt;. This persistor object is responsible for managing the persistence and rehydration of your Redux store state. It's typically used in conjunction with the PersistGate component in your application's entry point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;persistStore&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;persistor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;persistStore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Store&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 6: Integrate with React
&lt;/h3&gt;

&lt;p&gt;Finally, integrate Redux Persist with React by wrapping the application with the &lt;code&gt;PersistGate&lt;/code&gt; component. The &lt;code&gt;PersistGate&lt;/code&gt; ensures that your application waits for the persisted state to be loaded before rendering. This prevents any rendering inconsistencies that may occur while the state is being rehydrated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PersistGate&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;redux-persist/integration/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-redux&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./App&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;store&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PersistGate&lt;/span&gt; &lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;persistor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;persistor&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/PersistGate&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Provider&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;,
&lt;/span&gt;  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Congratulations! You've successfully persisted user info using Redux Persist with TypeScript. Now sit back, relax, and enjoy the seamless user experience.&lt;/p&gt;

&lt;p&gt;Leave a comment! Happy coding! &lt;/p&gt;

&lt;p&gt;&lt;em&gt;WebCraft With Hossain&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>redux</category>
      <category>reduxpersist</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How to use React `forwardRef` with TypeScript!</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Fri, 10 May 2024 10:55:43 +0000</pubDate>
      <link>https://dev.to/hossain45/implementing-react-forwardref-with-typescript-5ka</link>
      <guid>https://dev.to/hossain45/implementing-react-forwardref-with-typescript-5ka</guid>
      <description>&lt;p&gt;React's &lt;code&gt;forwardRef&lt;/code&gt; is a powerful feature that allows you to pass a ref through a component to one of its children. This is particularly useful when you are working with reusable components or when you need to manipulate a DOM element in React. Today, we'll explore how to implement &lt;code&gt;forwardRef&lt;/code&gt; in React using TypeScript, focusing on a simple example of a text input component. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is &lt;code&gt;forwardRef&lt;/code&gt;?
&lt;/h2&gt;

&lt;p&gt;Before we dive into the implementation, let's briefly explain what &lt;code&gt;forwardRef&lt;/code&gt; does. When you use &lt;code&gt;forwardRef&lt;/code&gt;, you can forward a &lt;code&gt;ref&lt;/code&gt; to a child component. This means that you can access the underlying DOM node or React component instance of the child component from its parent. This is especially handy when you need to interact with a child component imperatively.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"forwardRef lets your component expose a DOM node to parent component with a ref." - react official &lt;a href="https://react.dev/reference/react/forwardRef" rel="noopener noreferrer"&gt;docs&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Remember, in react we do not manipulate DOM directly. But there might be some cases where we need to manipulate DOM e.g. working with react hook forms &lt;code&gt;Controller&lt;/code&gt; component. In that case, we use &lt;code&gt;ref&lt;/code&gt; but to work with the child component we implement &lt;code&gt;forwardRef&lt;/code&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: &lt;em&gt;Every JSX/TSX we write becomes a valid JS object behind the scenes.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are many ambiguous uses of &lt;code&gt;forwardRef&lt;/code&gt;I found on the internet while I got errors in my project. Then I found a simple solution which I will share with you in step by step process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step Implementation
&lt;/h2&gt;

&lt;p&gt;Let's walk through the process of implementing a text input component for React hook form with &lt;code&gt;forwardRef&lt;/code&gt; in React using TypeScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Define Props Interface
&lt;/h3&gt;

&lt;p&gt;First, define an interface for the props that the &lt;code&gt;TextInput&lt;/code&gt; component will accept. In our example, we'll create an interface called &lt;code&gt;FieldDataProps&lt;/code&gt; with &lt;code&gt;label&lt;/code&gt;, &lt;code&gt;value&lt;/code&gt;, and &lt;code&gt;onChange&lt;/code&gt; properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FieldDataProps&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// input field label&lt;/span&gt;
  &lt;span class="nl"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Create the &lt;code&gt;TextInput&lt;/code&gt; Component
&lt;/h3&gt;

&lt;p&gt;Next, create the text input component using the &lt;code&gt;ForwardRefRenderFunction&lt;/code&gt; type from React. This type represents a function component that accepts props and a &lt;code&gt;ref&lt;/code&gt;. Inside the component, handle the &lt;code&gt;onChange&lt;/code&gt; event of the input element and invoke the provided &lt;code&gt;onChange&lt;/code&gt; function with the new value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ForwardRefRenderFunction&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;TextInput&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ForwardRefRenderFunction&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;FieldDataProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="nx"&gt;ref&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleTextInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ChangeEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLInputElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newValue&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;provide-your-classes&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// using tailwindcss&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
        &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleTextInput&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;given&lt;/span&gt; &lt;span class="nx"&gt;dynamically&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Forward the Ref
&lt;/h3&gt;

&lt;p&gt;Now, use &lt;code&gt;React.forwardRef&lt;/code&gt; to forward the ref to the text input component. Quite simple as it is, just put the whole TSX/JSX function inside &lt;code&gt;React.forwardRef()&lt;/code&gt; and export. This allows parent components to access the input element's DOM node or React component instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ForwardedTextInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forwardRef&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ForwardedTextInput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Usage
&lt;/h3&gt;

&lt;p&gt;You can now use the &lt;code&gt;TextInput&lt;/code&gt; component in your application. Pass the necessary props and a ref, and you're good to go!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Impleting in react hook form&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;TextInput&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./ForwardedTextInput&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// put necessary code here&lt;/span&gt;
&lt;span class="c1"&gt;// following controller component is from React Hook Form&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Controller&lt;/span&gt;
       &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;your_input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
       &lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;control&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="nx"&gt;rules&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;
       &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{({&lt;/span&gt; &lt;span class="na"&gt;field&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
         &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TextInput&lt;/span&gt;
           &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
           &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
           &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
           &lt;span class="nx"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Your Input&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
          &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;)}&lt;/span&gt;
      &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Congratulations to us, we've learned how to implement React's &lt;code&gt;forwardRef&lt;/code&gt; feature with TypeScript by creating a simple text input component. By following these steps, you can create reusable components that accept refs and provide more flexibility in your React applications. &lt;/p&gt;

&lt;p&gt;Got any questions? leave a comment below.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;WebCraft with Hossain&lt;/strong&gt;&lt;/em&gt;!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>typescript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How Do You Declare Custom Classes in a Tailwindcss-React-ts project?</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Sat, 03 Feb 2024 16:27:23 +0000</pubDate>
      <link>https://dev.to/hossain45/how-do-you-declare-custom-classes-in-a-tailwindcss-react-ts-project-1jag</link>
      <guid>https://dev.to/hossain45/how-do-you-declare-custom-classes-in-a-tailwindcss-react-ts-project-1jag</guid>
      <description>&lt;p&gt;Hey fellow developers,&lt;/p&gt;

&lt;p&gt;So, picture this: knee-deep in my project, I hit the classic developer's roadblock - the need for custom classes. Here's my journey, a tale of trials and triumphs that might just save you some precious coding time!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Dilemma of Custom CSS in Tailwind
&lt;/h2&gt;

&lt;p&gt;In the vast tapestry of React Tailwind projects, customizing styles can feel like a puzzle. I explored three paths, each with its quirks and perks. Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Wrangling Tailwind Classes in a Variable!
&lt;/h2&gt;

&lt;p&gt;First up, I tried corralling those repetitive Tailwind classes into a variable tucked away in a cozy &lt;code&gt;.ts&lt;/code&gt; file. Everything seemed peachy until the day I wanted a little flair, a touch of customization. Spoiler alert: couldn't pull it off. My dreams of a unique twist were dashed. It's like committing to a tattoo; changing your mind becomes a tad complicated.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Global Stylesheet Declaration
&lt;/h2&gt;

&lt;p&gt;Moving on, I experimented with declaring a class in a global stylesheet. A good practice, you'd think. Worked like a charm, until I craved more control. Trying to finesse the styles proved trickier than juggling flaming torches. Need a smaller font? Tough luck! And let's not even start on the exclamation marks – felt like I was in a shouting match with my styles. Ugh!&lt;/p&gt;

&lt;h2&gt;
  
  
  3. The @layer Revelation in &lt;code&gt;index.css&lt;/code&gt;!
&lt;/h2&gt;

&lt;p&gt;And here comes the star of the show—the class declared as a component layer. This, my friends, is the holy grail. Why? Because it behaves like any default Tailwind CSS class, and you can customize it without breaking a sweat.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv6p53qy0ywb2xtpjbbo2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv6p53qy0ywb2xtpjbbo2.png" alt="code snap- an example of using @layer for custom class" width="800" height="942"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The Unmatched Prowess of Component Layer Classes
&lt;/h2&gt;

&lt;p&gt;Why do I vouch for the third approach? Brace yourself for the pros:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Seamless Overrides:&lt;/strong&gt; No more banging your head against the wall with &lt;code&gt;!important&lt;/code&gt;. The component layer class gracefully accepts overrides like a seasoned actor playing multiple roles.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scoped and Neat:&lt;/strong&gt; Your styles remain scoped within the component, keeping your project neat and tidy. No more global chaos!&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dynamic Customization:&lt;/strong&gt; Embrace dynamic styles effortlessly. This approach opens the door to a world where your styles adapt and evolve based on dynamic content.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Integrates Like a Charm:&lt;/strong&gt; Remember, it's Tailwind CSS. Your custom class integrates seamlessly, like it was always meant to be there.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, fellow coders, spill the beans! How do you deck out your project with custom CSS? Let's swap war stories and share the coding wisdom. &lt;/p&gt;

&lt;p&gt;*Hint: Feel free to sprinkle in some more details if your secret sauce involves extra flair! &lt;/p&gt;

</description>
      <category>tailwindcss</category>
      <category>react</category>
      <category>typescript</category>
      <category>css</category>
    </item>
    <item>
      <title>500 open-source components for TailwindCSS</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Thu, 18 Jan 2024 09:03:55 +0000</pubDate>
      <link>https://dev.to/hossain45/500-open-source-components-for-tailwindcss-5b30</link>
      <guid>https://dev.to/hossain45/500-open-source-components-for-tailwindcss-5b30</guid>
      <description>&lt;p&gt;&lt;a href="https://tw-elements.com/" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmozxont96up3dvv7vl44.jpg" alt="TW components" width="800" height="403"&gt;&lt;/a&gt;&lt;br&gt;&lt;br&gt;
                       I'd like to share my latest discovery with you. &lt;br&gt;
       &lt;a href="https://tw-elements.com/" rel="noopener noreferrer"&gt;TW Elements&lt;/a&gt; is currently, the most popular 3rd party UI kit for TailwindCSS with over 10k Github stars.        &lt;a href="https://github.com/mdbootstrap/TW-Elements/" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimg.shields.io%2Fgithub%2Fstars%2Fmdbootstrap%2Ftw-elements%3Fstyle%3Dsocial" alt="GitHub Repo stars" width="88" height="20"&gt;&lt;/a&gt;        It's a &lt;strong&gt;huge collection of stunning components&lt;/strong&gt; made with attention to the smallest detail.         Forms, cards, buttons, and hundreds of others.         All components have &lt;strong&gt;dark mode&lt;/strong&gt; and very intuitive &lt;strong&gt;theming options&lt;/strong&gt;.        The project is supported by an &lt;a href="https://github.com/mdbootstrap/TW-Elements/discussions" rel="noopener noreferrer"&gt;engaged community on GitHub&lt;/a&gt;, I recommend you check it out and join one of the many discussions.&lt;br&gt;&lt;br&gt;
       You will find installation instructions &lt;a href="https://tw-elements.com/docs/getting-started/installation" rel="noopener noreferrer"&gt;here&lt;/a&gt;, and you can track the progress of the project live         &lt;a href="https://tw-elements.com/docs/standard/getting-started/changelog/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;&lt;br&gt;
       The project was kickstarted by @MDBootstrap, a group of open-source developers behind &lt;a href="https://github.com/mdbootstrap/mdb-ui-kit" rel="noopener noreferrer"&gt;MDB UI Kit&lt;/a&gt; - a high-quality UI kit for Bootstrap, and also behind &lt;a href="https://mdbgo.com/" rel="noopener noreferrer"&gt;MDB GO&lt;/a&gt; - hosting and deployment platform.&lt;br&gt;&lt;br&gt;
       I highly recommend you to check it out!&lt;br&gt;&lt;br&gt;
       &lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;div class="ltag__link__content"&gt;
    &lt;div class="missing"&gt;
      &lt;h2&gt;Article No Longer Available&lt;/h2&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


</description>
      <category>tailwindcss</category>
    </item>
    <item>
      <title>Unleashing TypeScript's Power: Exploring Key Concepts with Real-World Examples</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Wed, 17 Jan 2024 06:37:59 +0000</pubDate>
      <link>https://dev.to/hossain45/unleashing-typescripts-power-exploring-key-concepts-with-real-world-examples-55p9</link>
      <guid>https://dev.to/hossain45/unleashing-typescripts-power-exploring-key-concepts-with-real-world-examples-55p9</guid>
      <description>&lt;p&gt;In the dynamic world of web development, TypeScript emerges as a powerful companion to JavaScript. Let's explore some key concepts in a minute introduced by TypeScript, illustrating its features through practical examples. This blog will not div in detail!&lt;/p&gt;

&lt;h2&gt;
  
  
  But Why Typescript?
&lt;/h2&gt;

&lt;p&gt;As they say, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"TypeScript is JavaScript with syntax for types".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;TypeScript emerged to make developers' lives easy by explicitly declaring the types. It made debugging easier than before. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let age: number = 25;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the variable age is explicitly declared as a number, providing clarity and aiding in error detection.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type Annotations
&lt;/h2&gt;

&lt;p&gt;TypeScript introduces type annotations, allowing developers to explicitly specify the type of variables, parameters, and return values. This enhances code documentation and facilitates better understanding. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function greet(name: string): string {
  return `Hello, ${name}!`;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the function &lt;code&gt;greet&lt;/code&gt; takes a &lt;code&gt;string&lt;/code&gt; parameter and returns a &lt;code&gt;string&lt;/code&gt;, clearly defining the expected types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Interfaces for Clear Object Shapes
&lt;/h2&gt;

&lt;p&gt;Interfaces in TypeScript define contracts for object shapes, ensuring that objects adhere to a specific structure. This enhances code readability and maintainability. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Person {
  name: string;
  age: number;
}

const hossain45: Person = {
  name: "Hossain45",
  age: 23,
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Person&lt;/code&gt; interface specifies the structure of a person object, making it easy to create instances with consistent properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  Type Aliases for Simplifying Types
&lt;/h2&gt;

&lt;p&gt;Type aliases create descriptive names for types, making complex types more readable. They are particularly useful when dealing with unions, intersections, or custom types. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Age = number;
type RankAndName = `${string} ${string}`;

let userAge: Age = 23;
let rankAndName : RankAndName = "Develpoer hossain45";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;Age&lt;/code&gt; and &lt;code&gt;RankAndName&lt;/code&gt; provide meaningful names for the &lt;code&gt;number&lt;/code&gt; and template string types, respectively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Union and Intersection Types
&lt;/h2&gt;

&lt;p&gt;TypeScript supports union types, allowing variables to hold values of different types. Intersection types enable the combination of multiple types into one. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type Admin = {
  role: 'admin';
};

type Employee = {
  role: 'employee';
};

type AdminEmployee = Admin | Employee;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;AdminEmployee&lt;/code&gt;type can represent either an admin or an employee, providing flexibility in type definitions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Generics for Reusable Code
&lt;/h2&gt;

&lt;p&gt;Generics in TypeScript enable the creation of functions and data structures that work with various types, promoting code reuse and flexibility. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function identity&amp;lt;T&amp;gt;(arg: T): T {
  return arg;
}

let result: number = identity(42);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;identity&lt;/code&gt;function can accept and return values of any type, providing a versatile and reusable solution.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Power (and Caution) of "Any"
&lt;/h2&gt;

&lt;p&gt;While TypeScript promotes static typing, the &lt;code&gt;any&lt;/code&gt;type provides a way to opt out of type checking. It offers flexibility but comes with a loss of the benefits of static typing. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let dynamicValue: any = 42;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a broader perspective, consider a scenario where data arrives from an external API, and its structure is not fully known. The &lt;code&gt;any&lt;/code&gt;type allows flexibility in handling such dynamic data, but developers need to be cautious due to the lack of type checking.&lt;/p&gt;

&lt;h2&gt;
  
  
  Optional Properties for Enhanced Flexibility
&lt;/h2&gt;

&lt;p&gt;TypeScript's optional properties provide an additional layer of flexibility by allowing properties to be present or absent in an object. For example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface User {
  name: string;
  age?: number;
}

const john: User = {
  name: "hossain45",
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a broader scenario, consider a user profile where an &lt;code&gt;age&lt;/code&gt;property is optional. This accommodates scenarios where not all users may have an age specified, offering enhanced adaptability in data structures.&lt;/p&gt;

&lt;p&gt;That's all for today! WE WILL DIV INTO DEEPER ON EACH TOPIC LATER ON! HAPPY CODING!&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Resolve “TailwindCSS Not Working in React-TypeScript Project with Vite” In Two Minutes!</title>
      <dc:creator>Hossain MD Athar</dc:creator>
      <pubDate>Sun, 14 Jan 2024 12:24:15 +0000</pubDate>
      <link>https://dev.to/hossain45/resolve-tailwindcss-not-working-in-react-typescript-project-with-vite-in-two-minutes-3mnc</link>
      <guid>https://dev.to/hossain45/resolve-tailwindcss-not-working-in-react-typescript-project-with-vite-in-two-minutes-3mnc</guid>
      <description>&lt;p&gt;Developers may face issues configuring Tailwind CSS in React projects built with TypeScript and Vite. These challenges can result in styles not being applied as expected, very FRUSTRATING!&lt;/p&gt;

&lt;p&gt;Well, I was stuck too. Let’s explore a quick and effective way to start a React project with TypeScript and Tailwind CSS.&lt;/p&gt;

&lt;p&gt;I tried many solutions online yet I could not find a suitable solution for my existing project.&lt;/p&gt;

&lt;p&gt;To overcome these hurdles, follow these straightforward steps provided in the official Tailwind CSS documentation ( except for the first command) tailored for Vite React projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Create a New File
&lt;/h2&gt;

&lt;p&gt;Begin by creating a new file in your project. This file will serve as the entry point for Tailwind CSS configuration.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Add react-ts to the Command
&lt;/h2&gt;

&lt;p&gt;Refer to the official &lt;a href="https://tailwindcss.com/docs/guides/vite" rel="noopener noreferrer"&gt;Tailwind CSS documentation&lt;/a&gt; for Vite and React projects. Make sure to navigate to the section specific to Vite. For TypeScript support, use the following command instead of the given command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest my-project --template react-ts // add -ts
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command initializes a new Vite project with the React-TS template, laying the foundation for a smooth integration with Tailwind CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Follow Remaining Documentation
&lt;/h2&gt;

&lt;p&gt;Continue following the remaining steps outlined in the official Tailwind CSS documentation. These steps typically include installing dependencies, configuring your PostCSS and Tailwind CSS settings, and importing styles into your project.&lt;/p&gt;

&lt;p&gt;Well, there you have it! Empower your React-TS development journey by implementing these solutions, and enjoy the seamless synergy between #React, #TypeScript, #Vite, and #TailwindCSS. Happy coding!&lt;/p&gt;

&lt;p&gt;Share how you solved your problem in the comment below!!!&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>vite</category>
      <category>tailwindcss</category>
    </item>
  </channel>
</rss>
