<?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: Suan</title>
    <description>The latest articles on DEV Community by Suan (@suantech).</description>
    <link>https://dev.to/suantech</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%2F977510%2Fafc0cb30-7be2-4636-87d6-bac247498c5c.png</url>
      <title>DEV Community: Suan</title>
      <link>https://dev.to/suantech</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/suantech"/>
    <language>en</language>
    <item>
      <title>[React] Rendering in React</title>
      <dc:creator>Suan</dc:creator>
      <pubDate>Tue, 20 Jun 2023 04:57:26 +0000</pubDate>
      <link>https://dev.to/suantech/react-rendering-in-react-3epi</link>
      <guid>https://dev.to/suantech/react-rendering-in-react-3epi</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mHGykXph--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj5ww5fyhrj4lz4ni23j.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mHGykXph--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pj5ww5fyhrj4lz4ni23j.jpg" alt="title image" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🧚🏻‍♀️ Understanding Rendering in React
&lt;/h2&gt;




&lt;h3&gt;
  
  
  Motivation
&lt;/h3&gt;

&lt;p&gt;I've been working on projects using React for some time now, but I've realized that if someone were to ask me, "How does React work?" I would probably freeze up and feel dumbest. &lt;strong&gt;Being a visual learner myself, I've decided to break it down from the basic part of React using diagrams and example codes to make it easier to understand.&lt;/strong&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  What is Rendering?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Rendering&lt;/strong&gt; refers to the process of generating and displaying the final output of a web application or user interface. &lt;/p&gt;

&lt;h3&gt;
  
  
  Rendering in React
&lt;/h3&gt;

&lt;p&gt;In React, rendering specifically refers to how &lt;strong&gt;React updates and displays components on the screen based on changes in their&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;state&lt;/code&gt;&lt;/strong&gt; &lt;strong&gt;or&lt;/strong&gt; &lt;strong&gt;&lt;code&gt;props&lt;/code&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;h4&gt;
  
  
  Initial Rendering
&lt;/h4&gt;

&lt;p&gt;Let’s take a look at how React performs its initial render.&lt;/p&gt;

&lt;p&gt;React uses &lt;strong&gt;&lt;code&gt;root.render()&lt;/code&gt;&lt;/strong&gt; method to initially display components, represented by &lt;strong&gt;JSX&lt;/strong&gt; or &lt;strong&gt;React nodes,&lt;/strong&gt; inside the browser's DOM.&lt;/p&gt;

&lt;p&gt;If your app is fully built with React, your code will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;&lt;/span&gt;My app&lt;span class="nt"&gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;&amp;lt;!-- This is the DOM node --&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;createRoot&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-dom/client&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.js&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./styles.css&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;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&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="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you run this code, it will&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the &lt;strong&gt;browser DOM node(root)&lt;/strong&gt; defined in your HTML.&lt;/li&gt;
&lt;li&gt;Display the &lt;strong&gt;React component&lt;/strong&gt; for your app inside.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After the initial rendering, &lt;strong&gt;React re-renders its components only when there are changes to the state or props of the components.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below are the visualised flow charts of React initial render(mounting) and re-render process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wd7r3xtp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8n0sy2rp2a9k38tkmnl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wd7r3xtp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8n0sy2rp2a9k38tkmnl.png" alt="rendering process" width="800" height="743"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Rendering seems more complicated than &lt;code&gt;root.render()&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With the understanding of &lt;strong&gt;Virtual DOM&lt;/strong&gt;, you'll get a bit more idea.&lt;/p&gt;
&lt;h4&gt;
  
  
  Virtual DOM?
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The Virtual DOM is a concept used by React to optimise the process of updating the user interface.&lt;/strong&gt; It is a lightweight, in-memory representation of the actual browser DOM (Document Object Model).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;During initial render, React creates a Virtual DOM representation of the UI, takes the React elements and converts them into corresponding DOM elements. This conversion process involves creating the necessary DOM nodes and assigning properties and attributes to them based on the React elements. &lt;strong&gt;React then inject Virtual DOM elements to browser’s actual DOM elements.&lt;/strong&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Re-Rendering
&lt;/h4&gt;

&lt;p&gt;As described above, React re-renders the component(s) only when it needs to. Here is the process of re-rendering.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React invokes the component's render method or functional component body to generate &lt;strong&gt;a new virtual DOM&lt;/strong&gt; representation of the component.&lt;/li&gt;
&lt;li&gt;React creates a new Virtual DOM and compares it to exisiting Virtual DOM using &lt;a href="https://www.geeksforgeeks.org/what-is-diffing-algorithm/"&gt;**diffing algorithm&lt;/a&gt;.** (&lt;strong&gt;Reconciliation&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;Once the diffing process identifies the differences between the previous and new virtual DOM, React applies the necessary changes, or "patches," to the actual DOM to reflect the updates made in the virtual DOM.&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  When Does React Re-Render?
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When there are &lt;strong&gt;changes&lt;/strong&gt; to component’s &lt;strong&gt;&lt;code&gt;props&lt;/code&gt;&lt;/strong&gt; or &lt;code&gt;**states**&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;When the &lt;strong&gt;&lt;code&gt;context&lt;/code&gt;&lt;/strong&gt; value changes&lt;/li&gt;
&lt;li&gt;When parent component re-renders&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A Toe Dip Into Reconciliation&lt;/strong&gt;&lt;br&gt;
   In short, it is the process of &lt;strong&gt;comparing&lt;/strong&gt; the previous state of a component's rendered output (virtual DOM) with the new state and &lt;strong&gt;determining&lt;/strong&gt; the minimal set of changes needed to update the UI. React uses the &lt;strong&gt;diffing algorithm&lt;/strong&gt; to perform the comparison.&lt;br&gt;
   React implements a heuristic algorithm based on the following two assumptions, resulting in an O(n) time complexity:&lt;br&gt;
    1. Different types of elements create different trees.&lt;br&gt;
    For elements of the same type, only the changed attributes are updated.&lt;br&gt;
    2. Developers can use the key prop to identify component instances and indicate which child elements should not be changed between multiple renders.&lt;br&gt;
  By comparing and updating only the necessary parts of the DOM, &lt;strong&gt;React minimizes the performance impact and improves the efficiency of rendering&lt;/strong&gt;, resulting in a more responsive and optimized user interface.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this blog post, I've taken the time to simplify the fundamentals of React rendering using visuals and practical examples. By breaking down the rendering process and explaining concepts like initial rendering, the Virtual DOM, and re-rendering, I hope that I have provided a clearer understanding of how React works.&lt;/p&gt;

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

</description>
      <category>react</category>
      <category>rendering</category>
    </item>
    <item>
      <title>[Next.js] User authentication using Cookies with JWT vs NextAuth</title>
      <dc:creator>Suan</dc:creator>
      <pubDate>Tue, 20 Jun 2023 04:40:42 +0000</pubDate>
      <link>https://dev.to/suantech/nextjs-user-authentication-using-cookies-with-jwt-vs-nextauth-5he1</link>
      <guid>https://dev.to/suantech/nextjs-user-authentication-using-cookies-with-jwt-vs-nextauth-5he1</guid>
      <description>&lt;h2&gt;
  
  
  Which Auth Do I Use For My Next.js App?
&lt;/h2&gt;




&lt;h3&gt;
  
  
  Motivation
&lt;/h3&gt;

&lt;p&gt;During the development of my web app "Project Managed," which I built following a tutorial on Frontend Masters, I came across an interesting question: &lt;strong&gt;Why did the tutorial use cookies for user authentication instead of NextAuth?&lt;/strong&gt; This sparked my curiosity, and I began researching to find an answer.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparison: Cookies with JWT vs NextAuth for User Authentication
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Cookies with JWT&lt;/th&gt;
&lt;th&gt;NextAuth&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Implementation&lt;/td&gt;
&lt;td&gt;Manual handling of JWT and cookies&lt;/td&gt;
&lt;td&gt;Leveraging NextAuth library&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;JWT Management&lt;/td&gt;
&lt;td&gt;Custom implementation required&lt;/td&gt;
&lt;td&gt;Handled by NextAuth&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Authentication&lt;/td&gt;
&lt;td&gt;Custom server-side validation&lt;/td&gt;
&lt;td&gt;NextAuth handles validation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Authorization&lt;/td&gt;
&lt;td&gt;Custom implementation required&lt;/td&gt;
&lt;td&gt;NextAuth provides solutions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Session Management&lt;/td&gt;
&lt;td&gt;Manual handling of sessions&lt;/td&gt;
&lt;td&gt;NextAuth handles sessions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Security&lt;/td&gt;
&lt;td&gt;Can set HttpOnly flag for cookies&lt;/td&gt;
&lt;td&gt;Provides built-in security&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Token Inclusion&lt;/td&gt;
&lt;td&gt;Manual inclusion in each request&lt;/td&gt;
&lt;td&gt;Automatic inclusion with cookies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scalability&lt;/td&gt;
&lt;td&gt;Requires custom scaling and management&lt;/td&gt;
&lt;td&gt;Simplified scalability and management&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ecosystem&lt;/td&gt;
&lt;td&gt;Works with any server-side framework or technology&lt;/td&gt;
&lt;td&gt;Built for Next.js ecosystem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complexity&lt;/td&gt;
&lt;td&gt;Moderate complexity, requires understanding of JWT handling&lt;/td&gt;
&lt;td&gt;Simplifies authentication implementation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Flexibility&lt;/td&gt;
&lt;td&gt;Provides more control over token management and customisation&lt;/td&gt;
&lt;td&gt;Offers predefined solutions and flexibility within NextAuth&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Development Time&lt;/td&gt;
&lt;td&gt;Requires additional development time for implementation&lt;/td&gt;
&lt;td&gt;Can save development time with built-in functionality&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Community Support&lt;/td&gt;
&lt;td&gt;Wider community support and resources available&lt;/td&gt;
&lt;td&gt;Active community and support for NextAuth&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cookies with JWT&lt;/strong&gt;: Provides manual handling of JWT and cookies, requires custom implementation for token management and session handling, offers security benefits with the ability to set HttpOnly flag, and allows for more control and flexibility at the cost of moderate complexity and additional development time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NextAuth&lt;/strong&gt;: Simplifies authentication implementation with built-in solutions for token validation, session management, and security, offers automatic token inclusion with cookies, provides scalability and ecosystem compatibility within the Next.js framework, and saves development time with predefined functionality and active community support.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Through my exploration, I discovered that the &lt;strong&gt;choice between cookies and NextAuth is more a matter of preference rather than one being inherently better than the other.&lt;/strong&gt; It’ll depend on individual preferences, project requirements, familiarity with the technologies involved, and the desired level of control, customisation, and simplicity in authentication implementation.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>cookies</category>
      <category>jwt</category>
      <category>nextauth</category>
    </item>
  </channel>
</rss>
