<?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: Nick Vu</title>
    <description>The latest articles on DEV Community by Nick Vu (@vuthanhnguyen).</description>
    <link>https://dev.to/vuthanhnguyen</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%2F814179%2Fe5bebba2-3364-44db-9137-f2767c072119.png</url>
      <title>DEV Community: Nick Vu</title>
      <link>https://dev.to/vuthanhnguyen</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vuthanhnguyen"/>
    <language>en</language>
    <item>
      <title>Make our component's Tailwind class names more understandable in React's JSX</title>
      <dc:creator>Nick Vu</dc:creator>
      <pubDate>Sat, 15 Jun 2024 16:57:58 +0000</pubDate>
      <link>https://dev.to/vuthanhnguyen/make-our-components-tailwind-class-names-more-understandable-in-reacts-html-1e5h</link>
      <guid>https://dev.to/vuthanhnguyen/make-our-components-tailwind-class-names-more-understandable-in-reacts-html-1e5h</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Have you ever seen a long list of class names with barely knowing which components we try to debug?&lt;/p&gt;

&lt;p&gt;I cannot deny that Tailwind helps our web development faster, especially, with styling, but when we try to use many of its utility class names within a single element. As a result, we cannot see the actual identity of that component and it may become a guessing game.&lt;/p&gt;

&lt;p&gt;One of the solutions is that we can add a unique id or class name to identify that component &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2r5ilnrr6zotvebvwwvf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2r5ilnrr6zotvebvwwvf.png" alt="Added class name c_NPLButton" width="545" height="103"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;but I'd admit as a developer that sometimes, I'm too lazy to put it, or copy/paste components somewhere and forget to change their class name identities.&lt;/p&gt;

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

&lt;p&gt;To solve the above problem programmatically, I implemented &lt;a href="https://legacy.reactjs.org/docs/higher-order-components.html"&gt;a higher-order component&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withComponentClassName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&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;WrapperComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;componentName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;displayName&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;className&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;componentName&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="s2"&gt;`c-&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;componentName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&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="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;className&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;WrappedComponent&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&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="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;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;WrapperComponent&lt;/span&gt;&lt;span class="p"&gt;;&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;withComponentClassName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then use it in components&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;withComponentClassName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, it generates JSX like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&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;c-MyComponent&lt;/span&gt;&lt;span class="dl"&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;MyComponent&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;props&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="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now every single component will have this pattern in DOM. We can clearly see where those elements are from.&lt;/p&gt;

&lt;p&gt;The only trade-off with this approach is that we have an extra &lt;code&gt;div&lt;/code&gt; element wrapping our component that increases our DOM size, but in the long run, it has boosted my team's development as well as UI debugging.&lt;/p&gt;




&lt;p&gt;3 questions I stumped upon when I developed this approach:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We still can forget to place it in our components, how to prevent that? &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We implemented a custom ESLint rule to verify &lt;code&gt;export default&lt;/code&gt; of our components with &lt;code&gt;withComponentClassName&lt;/code&gt; usage (I'll share it with more detail in another post)&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Can we embed that class name into the component's elements directly instead of adding an extra &lt;code&gt;div&lt;/code&gt;? &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The solution is more complicated and actually, it may make unexpected re-renderings due to class name prop/state updates, so we've accepted this simpler solution&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Can we hide that class name addition layer on production? &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes, we can add a condition in &lt;code&gt;withComponentClassName&lt;/code&gt; like below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withComponentClassName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&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="c1"&gt;//production variable depends on your codebase setup&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;production&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;true&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="nx"&gt;WrappedComponent&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;WrapperComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;WrapperComponent&lt;/span&gt;&lt;span class="p"&gt;;&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;withComponentClassName&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>tailwindcss</category>
      <category>react</category>
      <category>javascript</category>
      <category>hoc</category>
    </item>
    <item>
      <title>ReactJS vs SolidJS</title>
      <dc:creator>Nick Vu</dc:creator>
      <pubDate>Sun, 07 Aug 2022 15:52:00 +0000</pubDate>
      <link>https://dev.to/vuthanhnguyen/reactjs-vs-solidjs-25m4</link>
      <guid>https://dev.to/vuthanhnguyen/reactjs-vs-solidjs-25m4</guid>
      <description>&lt;p&gt;I have had experience with &lt;a href="https://github.com/facebook/react"&gt;ReactJS&lt;/a&gt; for a while, and recently, I found a new interesting framework which is &lt;a href="https://www.solidjs.com/"&gt;SolidJS&lt;/a&gt;. Its community is still very small (~21k stars now on &lt;a href="https://github.com/solidjs/solid"&gt;Github&lt;/a&gt;), so I'm not sure it's mature enough for enterprise products.&lt;/p&gt;

&lt;p&gt;Right now I'm standing for ReactJS because of the huge community support but I believe SolidJS will have a bright future too.&lt;/p&gt;

&lt;p&gt;I raised this post to get your opinions about SolidJS vs ReactJS.&lt;/p&gt;

&lt;p&gt;A few questions in my mind&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;SolidJS is good enough for real web development?&lt;/li&gt;
&lt;li&gt;Which one is faster in terms of web performance?&lt;/li&gt;
&lt;li&gt;If you choose SolidJS, why?&lt;/li&gt;
&lt;li&gt;Any strong points from SolidJS that can replace ReactJS in the future?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I really appreciate your precious ideas and welcome all open discussions.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>solidjs</category>
      <category>discuss</category>
    </item>
  </channel>
</rss>
