<?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>Forem: Dinesh Balaji</title>
    <description>The latest articles on Forem by Dinesh Balaji (@sidthesloth92).</description>
    <link>https://forem.com/sidthesloth92</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%2F88059%2F24c7ae48-062b-4dfa-bf73-ca69a22b6962.png</url>
      <title>Forem: Dinesh Balaji</title>
      <link>https://forem.com/sidthesloth92</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/sidthesloth92"/>
    <language>en</language>
    <item>
      <title>React - Server Components - Introduction and Initial Thoughts</title>
      <dc:creator>Dinesh Balaji</dc:creator>
      <pubDate>Sun, 27 Dec 2020 14:06:39 +0000</pubDate>
      <link>https://forem.com/sidthesloth92/react-server-components-initial-thoughts-3lml</link>
      <guid>https://forem.com/sidthesloth92/react-server-components-initial-thoughts-3lml</guid>
      <description>&lt;p&gt;Just before Christmas, the React team gave an early Christmas present, Server Components a.k.a the &lt;strong&gt;zero bundle size&lt;/strong&gt; components. Let's have a look at what they are, what they bring to the table and my thoughts.&lt;/p&gt;

&lt;p&gt;Before we start, just want to let you know that the best resource for a deeper understanding would obviously be the &lt;a href="https://github.com/josephsavona/rfcs/blob/server-components/text/0000-server-components.md" rel="noopener noreferrer"&gt;RFC&lt;/a&gt; and the &lt;a href="https://www.youtube.com/watch?v=TQQPAU21ZUw&amp;amp;t=2196s" rel="noopener noreferrer"&gt;introduction video&lt;/a&gt; from the React team. I put this together for people who are light on time and to share my thoughts and understanding.&lt;/p&gt;

&lt;p&gt;You can find the entire source for this post &lt;a href="https://github.com/sidthesloth92/server-components-demo" rel="noopener noreferrer"&gt;here&lt;/a&gt;. It's a fork of the actual demo repo from the React team. I just simplified the components for easier understanding. All kudos go to the React team.&lt;/p&gt;

&lt;p&gt;With the introduction of the Server Components, the existing components have been renamed as Client components. In fact, we have three types now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Server Components&lt;/li&gt;
&lt;li&gt;Client Components&lt;/li&gt;
&lt;li&gt;Shared Components&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Server Components
&lt;/h1&gt;

&lt;p&gt;Let's look at some of the important features of the Server components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Zero Bundle Size
&lt;/h2&gt;

&lt;p&gt;They are zero bundle size because they are rendered on the server and only the rendered content is sent to the client. This means they do not add to your client JS bundle size. Let's look at an example,&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="c1"&gt;// BlogPost.server.js - A Server component.&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;renderMarkDown&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;...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Server only dependency.&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;getBlogPost&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;./blog/blog-api&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="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;blog&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;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getBlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&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="c1"&gt;// Get blog post from database directly.&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;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&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;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&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;/h1&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;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nf"&gt;renderMarkdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;markdown&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;/p&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;/&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;p&gt;Things to note here,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All server components are suffixed with &lt;code&gt;server.{js,jsx,ts,tsx)&lt;/code&gt; (At least for now). &lt;/li&gt;
&lt;li&gt;Since they are not sent to the client, we can have code that accesses server resources like database, internal API's etc.&lt;/li&gt;
&lt;li&gt;Since all this happens in the server, the package you imported for rendering the markdown is not sent to the client, only the rendered content is sent. This is a significant reduction in the Client JS bundle size.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The component itself is straight forward, it fetches the data from the database and renders the content.&lt;/p&gt;

&lt;h2&gt;
  
  
  Render Format
&lt;/h2&gt;

&lt;p&gt;If you have noticed, I have said that the &lt;em&gt;content&lt;/em&gt; is rendered and not &lt;code&gt;HTML&lt;/code&gt;. This is because Server components are not rendered to HTML but rather to an intermediate format. &lt;/p&gt;

&lt;p&gt;If the above component was the only component in your app, this is what would be returned from the server.&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="nx"&gt;J0&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;h1&lt;/span&gt;&lt;span class="dl"&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="p"&gt;{&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&lt;/span&gt;&lt;span class="dl"&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;Blog 1&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="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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;p&lt;/span&gt;&lt;span class="dl"&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="p"&gt;{&lt;/span&gt;
        &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;children&lt;/span&gt;&lt;span class="dl"&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;unt aut...&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;As you can see, only the rendered markdown is sent to the client and not the library itself.&lt;/p&gt;

&lt;p&gt;Now you might be wondering why not HTML and this format? (I don't know the format's name.. 🙃). Let's see why in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  State and difference from SSR
&lt;/h2&gt;

&lt;p&gt;Let's look at a primary difference between Server components and SSR. SSR generates the HTML on the server which is then sent to the client for rendering by the browser. This means the content itself is static and you cannot have interactive markup.&lt;/p&gt;

&lt;p&gt;However, since Server components use this intermediate format instead of HTML it allows them to have Client components that have interactive behaviour. Make no mistake, Server components themselves, &lt;strong&gt;cannot have state or event handlers&lt;/strong&gt;, in other words, they cannot make use of &lt;code&gt;useState&lt;/code&gt;, &lt;code&gt;useEffect&lt;/code&gt; etc. However, they can have Client Components which in turn can have state. &lt;/p&gt;

&lt;p&gt;Let's add a like button to the &lt;code&gt;BlogPost&lt;/code&gt; component that when clicked increments the number of likes for the blog post.&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="c1"&gt;// BlogPost.server.js - A Server component.&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;getBlogPost&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;./blog/blog-api&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;LikeButton&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;./LikeButton.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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BlogPost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;blog&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;blog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getBlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&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;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&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;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&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;/h1&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;p&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;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;markdown&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;/p&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;LikeButton&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="nx"&gt;component&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;/&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// LikeButton.client.js - A Client component.&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;likeBlogPost&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;./blog/blog-api&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;React&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&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="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;LikeButton&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;blog&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;likesCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLikesCount&lt;/span&gt;&lt;span class="p"&gt;]&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;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;likes&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;handleClick&lt;/span&gt; &lt;span class="o"&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="nf"&gt;setLikesCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prev&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Likes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;likes&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;/span&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;BlogPost&lt;/code&gt; Server component has a child component &lt;code&gt;LikeButton&lt;/code&gt;, which is a Client component that handles user interaction. The &lt;code&gt;LikeButton&lt;/code&gt; component is free to make use of &lt;code&gt;useState&lt;/code&gt; as it is a Client component and it also updates the local state on click.&lt;/p&gt;

&lt;p&gt;Thus a Server component cannot have state itself, but it can make use of a Client component to maintain state and handle user interactions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that the &lt;code&gt;LikeButton&lt;/code&gt; component is only updating the local like count and not the count in the server. The point of this example was to show that Client components can have state and user interaction.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  State Tree
&lt;/h2&gt;

&lt;p&gt;To understand this, let's expand our example to have a &lt;code&gt;BlogPostList&lt;/code&gt; Server component that renders a list of blogs using our &lt;code&gt;BlogPost&lt;/code&gt; Server component.&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="c1"&gt;// BlogPost.server.js - A Server component.&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;getBlogPosts&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;./blog/blog-api&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;BlogPost&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;./BlogPost.server&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="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BlogPostsList&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;blogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getBlogPosts&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;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;blog&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;BlogPost&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Uses&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="nx"&gt;component&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;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&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;p&gt;Let's also update the &lt;code&gt;LikeButton&lt;/code&gt; component to replace the state variable for &lt;code&gt;likes&lt;/code&gt; with the &lt;code&gt;likes&lt;/code&gt; from the props. Let's also add a callback function that hits the server to update the &lt;code&gt;likes&lt;/code&gt; count of the particular blog post.&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="c1"&gt;// LikeButton.client.js - A Client component.&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;likeBlogPost&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;./blog/blog-api&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;React&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&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;useLocation&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;./LocationContext.client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Experimental API for POC.&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;LikeButton&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;blog&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="p"&gt;[,&lt;/span&gt; &lt;span class="nx"&gt;setLocation&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useLocation&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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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;await&lt;/span&gt; &lt;span class="nf"&gt;likeBlogPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blog&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="nf"&gt;setLocation&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;loc&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="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;loc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;likes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;likes&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;likeBlogPost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Fetch call to update the blog post in the server.&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Likes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;likes&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;/span&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you click on the like button, a call is made to the server to update the like count and then &lt;code&gt;setLocation&lt;/code&gt; is called. This is an experimental API provided by the React team to mimic a call to the server to fetch a unit of the UI. In this case, we are fetching the component tree for the current route. You can see in the network tab that a call was indeed made and all the components in the current route beginning from the root are returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1lg73805zhw3o3hv6yub.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F1lg73805zhw3o3hv6yub.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The entire tree is rendered from the root and the parts that are updated are rendered, in this case, wherever &lt;code&gt;likes&lt;/code&gt; is displayed on the screen. Note that the call to update was made from the &lt;code&gt;LikeButton&lt;/code&gt; component however since the entire tree is updated the &lt;code&gt;likes&lt;/code&gt; count passed as a &lt;code&gt;prop&lt;/code&gt; to the &lt;code&gt;LikeButton&lt;/code&gt; is updated.&lt;/p&gt;

&lt;h2&gt;
  
  
  State of the Client components are maintained
&lt;/h2&gt;

&lt;p&gt;Let's create a new &lt;code&gt;Comment&lt;/code&gt; component, that renders an input text field bound to a state variable. For simplicity, we will not implement the comment functionality.&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="c1"&gt;// Comment.client.js - A Client component.&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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&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="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Comment&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setComment&lt;/span&gt;&lt;span class="p"&gt;]&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;useState&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;input&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;comment&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="na"&gt;target&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setComment&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="sr"&gt;/&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;p&gt;Type something in the comment text field of one of the blog posts. Now, click on any of the like buttons. You can see that even though the entire tree was rendered as a result of the like count update, the state of the Client components are preserved during such updates. As a result, whatever you typed in the comment box is intact and is not cleared. This is one of the biggest advantages of the Server components and a primary difference from traditional SSR.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flbnreshdq1gx5mracyfk.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flbnreshdq1gx5mracyfk.gif" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Client Components
&lt;/h1&gt;

&lt;p&gt;Client components are the components that we having been using all this while. But with server components in the mix, you need to remember one thing, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A Client component cannot import a server component however, it can render a server component passed in as children from a server component. Note that these props should be serializable, JSX is serializable and hence it can be passed in.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Not Possible
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// FancyBlogPost.client.js - A Client component.&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&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&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;BlogPost&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;./BlogPost.server&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="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;FancyBlogPost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;blog&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;fancyEffects&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;BlogPost&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Not&lt;/span&gt; &lt;span class="nx"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Cannot&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;inside&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="nx"&gt;component&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;/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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BlogPostList.server.js - A Server component.&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;getBlogPosts&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;./blog/blog-api&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;BlogPost&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;./BlogPost.server&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="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BlogPostsList&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;blogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getBlogPosts&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;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;blog&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;FancyBlogPost&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blog&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="sr"&gt;/&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;p&gt;The reasoning is quite simple, Client components are sent to the client. If it were to contain a Server component accessing some internal API, that would fail in the client since it will not have access. This is just one reason among many.&lt;/p&gt;

&lt;p&gt;Instead, we can do the following.&lt;/p&gt;

&lt;h3&gt;
  
  
  Possible
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// FancyBlogPost.client.js - A Client component.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;FancyBlogPost&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&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;fancyEffects&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="nx"&gt;children&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;/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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// BlogPostList.server.js - A Server component.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;BlogPostsList&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;blogs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getBlogPosts&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;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blogs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;blog&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;FancyBlogPost&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;BlogPost&lt;/span&gt; &lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt; /&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;Fine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nx"&gt;Server&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt; &lt;span class="nx"&gt;passed&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;childredn&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;Client&lt;/span&gt; &lt;span class="nx"&gt;component&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;/FancyBlogPost&lt;/span&gt;&lt;span class="err"&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="sr"&gt;/&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;p&gt;This is fine because from the perspective of the Client component the content is already rendered in the server as part of the parent Server component and only the rendered content is passed as a &lt;code&gt;prop&lt;/code&gt; to the Client component. &lt;/p&gt;

&lt;p&gt;Other things to remember with respect to Client components,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They end with the extension &lt;code&gt;*.client.{js,jsx,ts,tsx}&lt;/code&gt; (At least for now)&lt;/li&gt;
&lt;li&gt;They will be part of the client bundle and as such, you shouldn't be doing anything that you wouldn't want to be public. Eg: DB operations etc.&lt;/li&gt;
&lt;li&gt;They are free to use state and effect hooks.&lt;/li&gt;
&lt;li&gt;Use browser only API's.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Shared Components
&lt;/h1&gt;

&lt;p&gt;Shared components can be rendered either as a Server component or as a Client component. This is determined by what component imports it. Since it can be used either as a Server or a Client component it has the most limitations.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They don't have a specific suffix.&lt;/li&gt;
&lt;li&gt;They cannot have &lt;code&gt;state&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;They cannot make use of &lt;code&gt;useEffect&lt;/code&gt; etc.&lt;/li&gt;
&lt;li&gt;They cannot render Server components.&lt;/li&gt;
&lt;li&gt;They cannot use browser specific API's.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With all these limitations, these components can only be used to display content that is passed as a prop to it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Thoughts and Conclusion
&lt;/h1&gt;

&lt;p&gt;After reading this, if you are thinking that Server components are doing exactly what NextJS/SSR is doing. No. In the case of NextJS, the components are rendered in the server, yes, but eventually, the components are part of the client bundle and used for hydration. In addition, Server components allow for,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maintaining Client component state.&lt;/li&gt;
&lt;li&gt;A much granular integration of Client and Server components. For example, in NextJS, you are limited by pages to choose between client and server components.&lt;/li&gt;
&lt;li&gt;Code splitting is done based on file names and is now not an extra step to be done by the developers as an import.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Of course, there are parts that are being worked on like routing and stuff but I am genuinely excited by what Server components bring to the table. They provide the developers with the flexibility to choose between Client and Server components based on the requirements and get the best of both worlds. &lt;/p&gt;

&lt;p&gt;Hope, I was able to explain some of the concepts in a way that was easy to grasp. Happy coding and see you in the next one.. :)&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/sidthesloth92" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or check out my &lt;a href="https://dineshbalaji.in" rel="noopener noreferrer"&gt;website&lt;/a&gt; to know more about me..✨&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Typescript Quirks - String or string? Which one should I use?</title>
      <dc:creator>Dinesh Balaji</dc:creator>
      <pubDate>Thu, 17 Dec 2020 14:36:38 +0000</pubDate>
      <link>https://forem.com/sidthesloth92/typescript-quirks-string-or-string-which-one-should-i-use-13n6</link>
      <guid>https://forem.com/sidthesloth92/typescript-quirks-string-or-string-which-one-should-i-use-13n6</guid>
      <description>&lt;p&gt;If you've been using Typescript, you would have definitely come across &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt;. Have you ever wondered which one you should be using? You might have used them interchangeably and they do work. In fact, there are other types like this, &lt;code&gt;Number&lt;/code&gt; and &lt;code&gt;Boolean&lt;/code&gt;. So what are the differences between the two string types and which one should you be using in your projects? Let's find out.&lt;/p&gt;

&lt;p&gt;Let's have a look at a simple example of how you create strings using &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;String&lt;/code&gt; in Typescript.&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;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is my string&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;strUsingString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is my string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;The first and most basic difference between the two is that &lt;code&gt;string&lt;/code&gt; is a primitive type whereas &lt;code&gt;String&lt;/code&gt; is an &lt;code&gt;Object&lt;/code&gt; type. Okay, so what are the implications of this tiny difference?&lt;/p&gt;

&lt;h2&gt;
  
  
  Your &lt;code&gt;typeof&lt;/code&gt; checks will fail
&lt;/h2&gt;

&lt;p&gt;Let's look at an example.&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;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is my string&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;strUsingString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is my string&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints "string".&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;strUsingString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints "object"!&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;As you can see, the &lt;code&gt;typeof&lt;/code&gt; operator when used on a string created using &lt;code&gt;String&lt;/code&gt; returns &lt;code&gt;object&lt;/code&gt;! This means that you no longer will be able to rely on &lt;code&gt;typeof&lt;/code&gt; checks to differentiate between objects and strings. If you have checks based on this assumption, they will now fail!&lt;/p&gt;

&lt;h2&gt;
  
  
  Your equality checks will fail
&lt;/h2&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;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&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;strUsingString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints true. Good.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints true. Good.&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strUsingString&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints true. Good.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strUsingString&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints false. Wait, what?&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;strUsingString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints true.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;strUsingString&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints false. Are you kidding!!!&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;Here, the equality checks using the &lt;code&gt;===&lt;/code&gt; operators fail because &lt;code&gt;===&lt;/code&gt; not only checks for equality in value but also for equality in type as well. Therefore, even though &lt;code&gt;str&lt;/code&gt; and &lt;code&gt;strUsingString&lt;/code&gt; have the same value "Hi", &lt;code&gt;str&lt;/code&gt; is of type &lt;code&gt;string&lt;/code&gt; whereas &lt;code&gt;strUsingString&lt;/code&gt; is of type &lt;code&gt;object&lt;/code&gt; and hence the check fails. This will lead to bugs which are very hard to debug.&lt;/p&gt;

&lt;h2&gt;
  
  
  Value vs Reference
&lt;/h2&gt;

&lt;p&gt;When you create a string as a literal, no matter how many of them you create, they will all point to the same literal in storage. However, when you create a string using &lt;code&gt;String&lt;/code&gt;, each one creates a brand new object. &lt;/p&gt;

&lt;p&gt;Let's look at how the following code is represented in memory. &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;str1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&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;str2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&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;strO1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&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;strO2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hi&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0fhlypn816u90fptejzl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0fhlypn816u90fptejzl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, consider the following snippet,&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;str2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints true. Good.&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints true. Good.&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strO1&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nx"&gt;strO2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false. Worse!&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;strO1&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;strO2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// false. Even Worse!!&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This time both the &lt;code&gt;==&lt;/code&gt; and &lt;code&gt;===&lt;/code&gt; checks fail. This is because what is being actually compared is the address of the memory locations and not the actual value themselves.&lt;/p&gt;

&lt;p&gt;This is a pretty big source of bugs! Just imagine trying to compare two strings and you get &lt;code&gt;false&lt;/code&gt; in spite of them having the same value.&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;Always use the lowercase versions, &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt; and &lt;code&gt;boolean&lt;/code&gt;. This will ensure that you do not run into the bugs mentioned above and since the bugs happen at run time, they are very hard to debug. &lt;/p&gt;

&lt;p&gt;Hope you learnt something new. Happy coding and see you in the next one.. :)&lt;/p&gt;

&lt;p&gt;Follow me on &lt;a href="https://twitter.com/sidthesloth92" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt; or check out my &lt;a href="https://dineshbalaji.in" rel="noopener noreferrer"&gt;website&lt;/a&gt; to know more about me..✨&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>typescript</category>
      <category>nuggets</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Story of how I built my Portfolio and Blog using DEV.to and NextJS</title>
      <dc:creator>Dinesh Balaji</dc:creator>
      <pubDate>Fri, 13 Nov 2020 16:07:47 +0000</pubDate>
      <link>https://forem.com/sidthesloth92/story-of-how-i-built-my-portfolio-and-blog-using-dev-to-and-nextjs-3j85</link>
      <guid>https://forem.com/sidthesloth92/story-of-how-i-built-my-portfolio-and-blog-using-dev-to-and-nextjs-3j85</guid>
      <description>&lt;p&gt;I've always wanted to build my very own portfolio website and blog. After many years of laziness..😋, finally, I mean, FINALLY I've done it..🎉 I have built my own website &lt;a href="https://dineshbalaji.in" rel="noopener noreferrer"&gt;dineshbalaji.in&lt;/a&gt;..✨ Please do check out the website and let me know your thoughts in the comments..🙏&lt;/p&gt;

&lt;p&gt;The idea behind this post is to tell the story of how I did it, the tech involved, what I learned along the way and most importantly to &lt;strong&gt;inspire many more people&lt;/strong&gt; (lazy like me may be..:D) to &lt;strong&gt;build their own website&lt;/strong&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Ground Rules
&lt;/h1&gt;

&lt;p&gt;These are some of the ground rules that I laid down for myself for developing the website.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It had to coded out by me from scratch without using WYSWYG editors.&lt;/li&gt;
&lt;li&gt;A good logo.&lt;/li&gt;
&lt;li&gt;I have to learn new stuff as I build it.&lt;/li&gt;
&lt;li&gt;Minimize usage of third-party libraries.&lt;/li&gt;
&lt;li&gt;It has to showcase my coding skills.&lt;/li&gt;
&lt;li&gt;Good Lighthouse Score - Performance and Accessiblity.&lt;/li&gt;
&lt;li&gt;Has to integrate with a CMS for blogging.&lt;/li&gt;
&lt;li&gt;Good SEO.&lt;/li&gt;
&lt;li&gt;Be open sourced for others to use/learn.&lt;/li&gt;
&lt;li&gt;A full-page canvas landing page..😍 I love them..❤️&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  The Start
&lt;/h1&gt;

&lt;p&gt;With the ground rules set, I began on October 3rd, 2020. One thing I wanted to absolutely make sure was to track my progress. I've tried building my site a couple of times earlier but I got sidetracked. I was also inspired by &lt;a href="https://github.com/TypeStrong/typedoc/issues/1364" rel="noopener noreferrer"&gt;this&lt;/a&gt;. The way he wrote down whatever little progress he made was the way to go for me. So I did a &lt;a href="https://github.com/sidthesloth92/db-portfolio/blob/master/CHANGELOG.md" rel="noopener noreferrer"&gt;CHANGELOG&lt;/a&gt;. Easy to generate and maintain.&lt;/p&gt;

&lt;h1&gt;
  
  
  Design
&lt;/h1&gt;

&lt;p&gt;I'm a coder by profession but I do love to design and I'm quite crafty with &lt;a href="https://www.sketch.com/" rel="noopener noreferrer"&gt;Sketch&lt;/a&gt;. But sticking to my ground rules, &lt;a href="https://www.figma.com/" rel="noopener noreferrer"&gt;Figma&lt;/a&gt;, was the first thing I learned. I spent a weekend going through tutorials learning the tool and then went about creating a design system and the website. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Logo
&lt;/h3&gt;

&lt;p&gt;I had to do a logo for branding. I wanted it to be simple and animatable. I'm happy with what I ended up with.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgv2jpooj2982s5ryk5fc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fgv2jpooj2982s5ryk5fc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Being a developer staring endlessly at the screens, I love dark themes and decided to go with it. Chose a couple of popping &lt;a href="https://coolors.co/222831-20252c-f20a72-e6db74-ffffff" rel="noopener noreferrer"&gt;colors&lt;/a&gt; and some shades using &lt;a href="https://www.crispedge.com/color-shades-generator/" rel="noopener noreferrer"&gt;crispedge&lt;/a&gt; and voila I had my recipe.. 🦄&lt;/p&gt;

&lt;h3&gt;
  
  
  Fonts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://fonts.google.com/specimen/Nunito+Sans" rel="noopener noreferrer"&gt;Nunito Sans&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.dafontfree.io/adinda-melia-script-font/" rel="noopener noreferrer"&gt;Adina Melia&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I created the screens over a week. I made sure all the screens were complete before starting development to ensure there were no stoppages. &lt;/p&gt;

&lt;h1&gt;
  
  
  Technology
&lt;/h1&gt;

&lt;p&gt;Since I wanted a blog along with the portfolio, SEO was of paramount importance and the best way to do that is to build static sites. I wanted to learn React along the way and there were two choices, NextJS and Gatsby. Gatsby was purely static and NextJS provided the flexibility to switch between static and dynamic based on our need. So I ended up with Next JS. &lt;/p&gt;

&lt;h3&gt;
  
  
  Hosting
&lt;/h3&gt;

&lt;p&gt;Choosing NextJS came with a benefit. The amazing hosting platform from the team that developed NextJS, &lt;a href="https://vercel.com/" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt;. It has first-class tooling to host your website in minutes and has seamless integration with Github for development, preview and production deployments.&lt;/p&gt;

&lt;p&gt;I don't want to bore you guys with a long explanation of each and every detail. I'll just list the tech I used on the website with one-liners.&lt;/p&gt;

&lt;h3&gt;
  
  
  Base Tech
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="http://nextjs.org/" rel="noopener noreferrer"&gt;NextJS&lt;/a&gt; - The base React framework on which the website is built. It allows you to choose between SSR, SSG and CSR.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://docs.dev.to/api/"&gt;DEV.to API&lt;/a&gt; - My CMS.. 😋 I love DEV.to as a platform for developer blogging and I didn't want to leave it. But at the same time, I wanted a single source of truth for my articles. So I made use of the dev.to &lt;a href="https://docs.dev.to/api/"&gt;API&lt;/a&gt;. It's sweet and I got the best of both worlds.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://tailwindcss.com/" rel="noopener noreferrer"&gt;TailwindCSS&lt;/a&gt; with &lt;a href="https://sass-lang.com/" rel="noopener noreferrer"&gt;SCSS&lt;/a&gt; - I was so happy to have come across TailwindCSS. I strongly recommend this to anyone. Integrating it with SCSS was a pain but if you just use CSS, please try it out.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.typescriptlang.org/" rel="noopener noreferrer"&gt;Typescript&lt;/a&gt; - Must have for anyone doing frontend development in 2020.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tooling
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://stylelint.io/" rel="noopener noreferrer"&gt;stylelint&lt;/a&gt; - For linting CSS, sorting and ordering CSS rules. Must have.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://eslint.org/" rel="noopener noreferrer"&gt;eslint&lt;/a&gt; - For linting Typescript code. Must have.&lt;/li&gt;
&lt;li&gt;
&lt;a href="http://postcss.org/" rel="noopener noreferrer"&gt;postcss&lt;/a&gt; - For CSS browser vendor pre-fixes, purging of unused TailwindCSS rules.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/typicode/husky" rel="noopener noreferrer"&gt;husky&lt;/a&gt; - For running linting, commit rules before committing code.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/commitizen/cz-cli" rel="noopener noreferrer"&gt;commitzen&lt;/a&gt; - Provides a format to your git commit messages. The beautiful change log you saw earlier was possible because of this. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://prettier.io/" rel="noopener noreferrer"&gt;prettier&lt;/a&gt; - Standard for 2020. Does what it says, formats and makes your code look beautiful..😋&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  NPM Packages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://www.framer.com/api/motion/" rel="noopener noreferrer"&gt;framer-motion&lt;/a&gt; - All the beautiful animations on the site are powered by this amazing library. You just have to come up with the animations and leave the rest to framer. Highly recommend this one.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/react-copy-to-clipboard" rel="noopener noreferrer"&gt;react-copy-to-clipboard&lt;/a&gt; - For copying code snippets to your clipboard.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/react-infinite-scroll-component" rel="noopener noreferrer"&gt;react-infinite-scroll-component&lt;/a&gt; - For loading content on scroll in posts and nuggets page.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/researchgate/react-intersection-observer" rel="noopener noreferrer"&gt;react-intersection-observer&lt;/a&gt; - For detecting if elements are scrolled into view. &lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/remarkjs/react-markdown#readme" rel="noopener noreferrer"&gt;react-markdown&lt;/a&gt; - For parsing the markdown from DEV.to API's and rendering as HTML.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/react-syntax-highlighter/react-syntax-highlighter" rel="noopener noreferrer"&gt;react-syntax-highlighter&lt;/a&gt; - Plugin for &lt;code&gt;react-markdown&lt;/code&gt; that highlights code within the markdown.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/nygardk/react-share#readme" rel="noopener noreferrer"&gt;react-share&lt;/a&gt; - Provides social sharing icons and functionality for posts and nuggets.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/reading-time" rel="noopener noreferrer"&gt;reading-time&lt;/a&gt; - Estimates the reading time of a given text.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://www.npmjs.com/package/sitemap" rel="noopener noreferrer"&gt;sitemap&lt;/a&gt; - For generating the site's &lt;code&gt;sitemap.xml&lt;/code&gt; for SEO.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/sindresorhus/camelcase" rel="noopener noreferrer"&gt;camelcase&lt;/a&gt; - For converting hyphenated strings to camelcase strings.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://react-svgr.com/" rel="noopener noreferrer"&gt;@svgr/cli&lt;/a&gt; - For optimizing and converting SVG's into React components.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Analytics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://analytics.google.com/" rel="noopener noreferrer"&gt;Google Analytics&lt;/a&gt; - I'm using it as my data store for posts view count and also for user demographics and content relevancy.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://clarity.microsoft.com/" rel="noopener noreferrer"&gt;MS Clarity&lt;/a&gt; - This just came out and I really like the heat maps and session playback features they offer. &lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Development
&lt;/h1&gt;

&lt;p&gt;I am employed full-time so I had to find the time to develop. Most of the website was built over weekends, late nights and early mornings. I used libraries whenever I felt like I was re-inventing the wheel and stuck to coding stuff out when I wanted to showcase my skills.&lt;/p&gt;

&lt;p&gt;Particularly, I wanted to ensure that all the canvas-based animations were coded out entirely by me without the use of third party libraries. As a result, I learnt a lot of math and ended up creating mini-libraries &lt;a href="https://github.com/sidthesloth92/db-portfolio/blob/master/src/lib/vector.ts" rel="noopener noreferrer"&gt;Vector.js&lt;/a&gt;, &lt;a href="https://github.com/sidthesloth92/db-portfolio/blob/master/src/lib/particle.ts" rel="noopener noreferrer"&gt;Particle.js&lt;/a&gt; for the animations on the canvas..👻&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;After a month of development, I've done it. I'm really happy with the outcome. This is a lighthouse snapshot of my website.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg02wnq19dvvcwiig6lf2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fg02wnq19dvvcwiig6lf2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, it's green across the board..🍾&lt;/p&gt;

&lt;p&gt;I intend to add features to the website as I go along and more importantly I hope that this inspires me to blog more. &lt;/p&gt;

&lt;p&gt;I also wanna thank &lt;a href="https://twitter.com/seshadrisowmya" rel="noopener noreferrer"&gt;Sowmya&lt;/a&gt; and &lt;a href="https://twitter.com/nayakrashmi" rel="noopener noreferrer"&gt;Rashmi&lt;/a&gt; for their kind reviews and feedback and the Open Source Community in general for all the amazing libraries and tools..🙏&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/sidthesloth92/db-portfolio" rel="noopener noreferrer"&gt;source&lt;/a&gt; for the website is completely Open Sourced.&lt;/p&gt;

&lt;p&gt;I sincerely hope that someone reads this and starts their own journey towards building their very own website.&lt;/p&gt;

&lt;p&gt;See you in the next one. Peace.. :)&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>webdev</category>
      <category>blog</category>
      <category>react</category>
    </item>
    <item>
      <title>Understanding HTML Form Encoding: URL Encoded and Multipart Forms</title>
      <dc:creator>Dinesh Balaji</dc:creator>
      <pubDate>Sat, 11 Aug 2018 14:14:03 +0000</pubDate>
      <link>https://forem.com/sidthesloth92/understanding-html-form-encoding-url-encoded-and-multipart-forms-3lpa</link>
      <guid>https://forem.com/sidthesloth92/understanding-html-form-encoding-url-encoded-and-multipart-forms-3lpa</guid>
      <description>&lt;p&gt;The other day I was trying to write a REST endpoint in &lt;a href="https://golang.org/" rel="noopener noreferrer"&gt;Go&lt;/a&gt;, which uploads the contents of a form submitted in a browser to another REST endpoint, in other words,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Form in Browser ----&amp;gt; My GO Rest API ----&amp;gt; Another REST API&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;While doing that I ended up learning some fundamentals of how HTML forms work. So thought it might be a good thing to share what I learned and hence the post.. :)&lt;/p&gt;

&lt;p&gt;The encoding type of a form is determined by the attribute &lt;code&gt;enctype&lt;/code&gt;. It can have three values,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt; - Represents a URL encoded form. This is the default value if &lt;code&gt;enctype&lt;/code&gt; attribute is not set to anything.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;multipart/form-data&lt;/code&gt; - Represents a Multipart form. This type of form is used when the user wants to upload files&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;text/plain&lt;/code&gt; - A new form type introduced in HTML5, that as the name suggests, simply sends the data without any encoding&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let us look at each form type with an example to understand them better.&lt;/p&gt;

&lt;h1&gt;
  
  
  URL Encoded Form
&lt;/h1&gt;

&lt;p&gt;As the name suggests, the data that is submitted using this type of form is URL endcoded. Take the following form,&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="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/urlencoded?firstname=sid&amp;amp;lastname=sloth"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt; &lt;span class="na"&gt;enctype=&lt;/span&gt;&lt;span class="s"&gt;"application/x-www-form-urlencoded"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"sidthesloth"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"slothsecret"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Submit"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, you can see that the form is submitted to the server using a POST request, this means that it has a body. But how is the body formatted? It is URL encoded. Basically, a long string of &lt;code&gt;(name, value)&lt;/code&gt; pairs are created. Each &lt;code&gt;(name, value)&lt;/code&gt; pair is separated from one another by a &lt;code&gt;&amp;amp; (ampersand)&lt;/code&gt; sign, and for each &lt;code&gt;(name, value)&lt;/code&gt; pair, the &lt;code&gt;name&lt;/code&gt; is separated from the &lt;code&gt;value&lt;/code&gt; by an &lt;code&gt;= (equals)&lt;/code&gt; sign, like say,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;key1=value1&amp;amp;key2=value2&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For the above form, it would be,&lt;br&gt;
&lt;code&gt;username=sidthesloth&amp;amp;password=slothsecret&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also, notice that we have some query parameters passed in the action URL, &lt;code&gt;/urlencoded?firstname=sid&amp;amp;lastname=sloth&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Don't the URL encoded body and the query parameters passed in the action URL look awfully similar? It's because they are similar. They share the same format discussed above.&lt;/p&gt;

&lt;p&gt;Try creating an HTML file with the above code and see how it's submitted in the dev tools. Here is a snap,&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%2Fpsti396bkzet0vgsmifi.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%2Fpsti396bkzet0vgsmifi.png" alt="URL Encoded Snapshot" width="800" height="510"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The things to notice here are the &lt;code&gt;Content-Type&lt;/code&gt; header which says &lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt;, the query string and the form fields are transferred to the server in the format as discussed above. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Don't get confused by the term Form Data in the screen shot. It's just how Google Chrome represents form fields.  &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All is fine, but there is a little more to the encoding process. Let's introduce some spaces in the submitted values, take the below form which is the same as the previous one but has the &lt;code&gt;firstname&lt;/code&gt; value changed from &lt;code&gt;sid&lt;/code&gt; to  &lt;code&gt;sid slayer&lt;/code&gt; and &lt;code&gt;username&lt;/code&gt; value changed from &lt;code&gt;sidthesloth&lt;/code&gt; to &lt;code&gt;sid the sloth&lt;/code&gt;.&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="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/urlencoded?firstname=sid slayer&amp;amp;lastname=sloth"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt; &lt;span class="na"&gt;enctype=&lt;/span&gt;&lt;span class="s"&gt;"application/x-www-form-urlencoded"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"sid the sloth"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"slothsecret"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Submit"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now try to submit the form and see how the form fields are transferred in the dev tools. Here is a dev tools snap in Chrome.&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%2Fq496466tm2dqzu79i1gj.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%2Fq496466tm2dqzu79i1gj.png" alt="URL Encoded snapshot with space" width="800" height="385"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Clearly, you can see that the spaces are replaced by either '%20' or '+'. This is done for both the query parameters and the form body.&lt;/p&gt;

&lt;p&gt;Read &lt;a href="https://stackoverflow.com/questions/2678551/when-to-encode-space-to-plus-or-20" rel="noopener noreferrer"&gt;this&lt;/a&gt; to understand when + and %20 can be used. This encompasses the URL encoding process.&lt;/p&gt;

&lt;h1&gt;
  
  
  Multipart Forms
&lt;/h1&gt;

&lt;p&gt;Multipart forms are generally used in contexts where the user needs files to be uploaded to the server. However, we'll just focus on simple text field based forms, as is it enough to understand how they work.&lt;/p&gt;

&lt;p&gt;To convert the above form into a multipart form all you have to do is to change the &lt;code&gt;enctype&lt;/code&gt; attribute of the form tag from &lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt; to &lt;code&gt;multipart/form-data&lt;/code&gt;.&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="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/multipart?firstname=sid slayer&amp;amp;lastname=sloth"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt; &lt;span class="na"&gt;enctype=&lt;/span&gt;&lt;span class="s"&gt;"multipart/form-data"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"sid the sloth"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"slothsecret"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Submit"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's go ahead and submit it and see how it appears in the dev tools.&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%2Fdr3ds5s7luc3tu1sfo5w.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%2Fdr3ds5s7luc3tu1sfo5w.png" alt="URL Encoded snapshot with space" width="800" height="552"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are the two things to notice here, the &lt;code&gt;Content-Type&lt;/code&gt; header and the payload of the form request. Let's go through them one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Content-Type Header
&lt;/h2&gt;

&lt;p&gt;The value of the &lt;code&gt;Content-Type&lt;/code&gt; header is obviously &lt;code&gt;multipart/form-data&lt;/code&gt;. But it also has another value, &lt;code&gt;boundary&lt;/code&gt;. The value for this in the example above is generated by the browser, but the user can very well define it as well, say for example, &lt;code&gt;boundary=sidtheslothboundary&lt;/code&gt;. We'll get to see how it's useful in the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  Request Body
&lt;/h2&gt;

&lt;p&gt;The request payload contains the form fields themselves. Each &lt;code&gt;(name, value)&lt;/code&gt; pair is converted into a MIME message part of the following format, &lt;/p&gt;

&lt;p&gt;&lt;code&gt;--&amp;lt;&amp;lt;boundary_value&amp;gt;&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Content-Disposition: form-data; name="&amp;lt;&amp;lt;field_name&amp;gt;&amp;gt;"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;&amp;lt;field_value&amp;gt;&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above format is repeated for each &lt;code&gt;(name, value)&lt;/code&gt; pair. &lt;/p&gt;

&lt;p&gt;Finally, the entire payload is terminated by the &lt;code&gt;boundary&lt;/code&gt; value suffixed with a &lt;code&gt;--&lt;/code&gt;. So the entire request looks like,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--&amp;lt;&amp;lt;boundary_value&amp;gt;&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Content-Disposition: form-data; name="&amp;lt;&amp;lt;field_name&amp;gt;&amp;gt;"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;&amp;lt;field_value&amp;gt;&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;--&amp;lt;&amp;lt;boundary_value&amp;gt;&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Content-Disposition: form-data; name="&amp;lt;&amp;lt;field_name&amp;gt;&amp;gt;"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;&amp;lt;field_value&amp;gt;&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;code&gt;--&amp;lt;&amp;lt;boundary_value&amp;gt;&amp;gt;--&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, we see how the boundary value is used. &lt;/p&gt;

&lt;p&gt;In the case of an &lt;code&gt;application/x-www-form-urlencoded&lt;/code&gt; form, the &lt;code&gt;&amp;amp;&lt;/code&gt; ampersand kind of acts as a delimiter between each &lt;code&gt;(name, value)&lt;/code&gt; pair, enabling the server to understand when and where a parameter value starts and ends. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;username=sidthelsloth&amp;amp;password=slothsecret&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the case of a &lt;code&gt;multipart/form-data&lt;/code&gt; form, the boundary value serves this purpose. Say if the boundary value was &lt;code&gt;XXX&lt;/code&gt;, the request payload would look like,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--XXX&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Content-Disposition: form-data; name="username"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sidthesloth&lt;/code&gt;&lt;br&gt;
&lt;code&gt;--XXX&lt;/code&gt;&lt;br&gt;
&lt;code&gt;Content-Disposition: form-data; name="password"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;slothsecret&lt;/code&gt;&lt;br&gt;
&lt;code&gt;--XXX--&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The hyphens themselves are not part of the boundary value but rather needed as part of the request format. The &lt;code&gt;Content-Type&lt;/code&gt; header for the above request would be,&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Content-Type: multipart/form-data; boundary=XXX&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This allows the browser to understand, when and where each field starts and ends.&lt;/p&gt;

&lt;h1&gt;
  
  
  Text/plain Forms
&lt;/h1&gt;

&lt;p&gt;These forms are pretty much the same as the URL encoded forms, except that the form fields are not URL encoded when sent to the server. These are not used widely in general, but they have been introduced as a part of the HTML 5 specification.&lt;/p&gt;

&lt;p&gt;Avoid using them as they meant for human understanding and not for machines.&lt;/p&gt;

&lt;p&gt;As quoted from the &lt;a href="https://www.w3.org/TR/html5/sec-forms.html#text-plain-encoding-algorithm" rel="noopener noreferrer"&gt;spec&lt;/a&gt;,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Payloads using the text/plain format are intended to be human readable. They are not reliably interpretable by computer, as the format is ambiguous (for example, there is no way to distinguish a literal newline in a value from the newline at &lt;br&gt;
the end of the value).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hope, I was clear in explaining what I learnt..See you in the next one guys..Peace.. :)&lt;/p&gt;

&lt;p&gt;Get to know more about me on my &lt;a href="https://dineshbalaji.in" rel="noopener noreferrer"&gt;website&lt;/a&gt;..✨&lt;/p&gt;

</description>
      <category>html</category>
      <category>web</category>
      <category>forms</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
