<?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: Sean Saranga Amarasinghe</title>
    <description>The latest articles on DEV Community by Sean Saranga Amarasinghe (@szaranger).</description>
    <link>https://dev.to/szaranger</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%2F87011%2Fa5a51dd6-fc37-4e2a-8423-b058c28e3c4d.jpg</url>
      <title>DEV Community: Sean Saranga Amarasinghe</title>
      <link>https://dev.to/szaranger</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/szaranger"/>
    <language>en</language>
    <item>
      <title>NextJS Client-side, Server-side &amp; Static Rendering</title>
      <dc:creator>Sean Saranga Amarasinghe</dc:creator>
      <pubDate>Wed, 18 Aug 2021 01:16:49 +0000</pubDate>
      <link>https://dev.to/szaranger/nextjs-client-side-server-side-static-rendering-328a</link>
      <guid>https://dev.to/szaranger/nextjs-client-side-server-side-static-rendering-328a</guid>
      <description>&lt;p&gt;The future of SEO and search ranking algorithms are now in heavy favour of static or server-side rendering. This means building static or server-side rendered apps instantly gain advantages in rankings.&lt;/p&gt;

&lt;p&gt;In this article, we are going to discuss building static &amp;amp; server-side rendering with NextJS, a server-side rendering React framework.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Static Rendering &amp;amp; Server-Side Rendering
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Server-Rendering (SSR)
&lt;/h3&gt;

&lt;p&gt;In response to each request, renders the app on the Server , and then sends the rendered HTML and Javascript back to the client.&lt;/p&gt;

&lt;h3&gt;
  
  
  Client-Side Rendering (CSR)
&lt;/h3&gt;

&lt;p&gt;Renders the app in the browser on the client at run time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Static Rendering (SR)
&lt;/h3&gt;

&lt;p&gt;Usually generating a static HTML page for every URL. Pre-built app, or renders the app at build time (e.g. when you run the npm run build or yarn build command). This is the default approach of NextJS.&lt;/p&gt;

&lt;h2&gt;
  
  
  NextJS
&lt;/h2&gt;

&lt;p&gt;In NextJS, there are three main functions that are used for SSR and SR, and a hook that for client-side data fetching.&lt;/p&gt;

&lt;p&gt;Built time:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;getStaticProps&lt;/em&gt; — fetches data at build time getStaticPaths — pre-render dynamic routes at build time&lt;/p&gt;

&lt;p&gt;Run time:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;getServerSideProps&lt;/em&gt; — fetches data on each request SWR — fetches data on the client-side at run time&lt;/p&gt;

&lt;h3&gt;
  
  
  Server-Side Rendering (SSR)
&lt;/h3&gt;

&lt;p&gt;The HTML is generated on the server on each request for the page — the HTML is 'dynamic' rather than 'static' as it will depend on the data required.&lt;/p&gt;

&lt;p&gt;Each time a client requests the page, the server will fetch the data for that page, and generate the page's HTML using the data.&lt;/p&gt;

&lt;p&gt;Next.js offers two data fetching methods for SSR — getServerSideProps and getInitialProp&lt;/p&gt;

&lt;p&gt;&lt;a href="https://seanamarasinghe.com/developer/2021-02-02-nextjs-client-side-server-side-and-static-rendering"&gt;Read more here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>React Re-rendering Components</title>
      <dc:creator>Sean Saranga Amarasinghe</dc:creator>
      <pubDate>Fri, 01 May 2020 03:36:47 +0000</pubDate>
      <link>https://dev.to/szaranger/react-re-rendering-components-1gco</link>
      <guid>https://dev.to/szaranger/react-re-rendering-components-1gco</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k3IOFYl6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/static/70cf4ded27448bba0780efb003004289/14b42/feature.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k3IOFYl6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/static/70cf4ded27448bba0780efb003004289/14b42/feature.jpg" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What is components re-rendering?
&lt;/h2&gt;

&lt;p&gt;When looking into React's render performance, there are a few terms and concepts that can be hard to understand.&lt;/p&gt;

&lt;p&gt;Here, we will look at the most important concepts about rendering in React and how React decides to re-render a given component.&lt;/p&gt;

&lt;p&gt;And then we will find out what we can do to optimise the render performance of your React application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Document Object Model (DOM)
&lt;/h3&gt;

&lt;p&gt;To understand how React renders and re-renders work, we can peek into React codebase to see what happens behind the scenes of the library.&lt;/p&gt;

&lt;p&gt;The tree-like DOM represents the structure of a website, represented by HTML. &lt;br&gt;
JavaScript also has a DOM, which is represented as an object where the root element is the document.&lt;/p&gt;

&lt;p&gt;You can modify the DOM with JavaScript through the DOM API that contains functions like &lt;code&gt;document.write&lt;/code&gt;, &lt;code&gt;Node.appendChild&lt;/code&gt; or &lt;code&gt;Element.setAttribute&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  What is the Virtual document Object Model (VDOM)?
&lt;/h3&gt;

&lt;p&gt;Then there's the Virtual DOM (or VDOM) of React, which is another abstraction layer on top of that. It consists of your React application's elements.&lt;/p&gt;

&lt;p&gt;State changes in your application will be applied to the VDOM first. If the new state of the VDOM requires a UI change, &lt;br&gt;
the ReactDOM library will efficiently do this by trying to update only what needs to be updated.&lt;/p&gt;

&lt;p&gt;For example, if only the attribute of an element changes, React will only update the attribute of the HTML element by calling &lt;code&gt;document.setAttribute&lt;/code&gt; (or something similar).&lt;/p&gt;

&lt;p&gt;Real DOM updates are slow because they cause an actual re-draw of the UI. React makes this more efficient by updating the smallest amount possible in the real DOM.&lt;/p&gt;

&lt;p&gt;Therefore we have to be aware of the difference between native and virtual DOM updates.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0dhzdFmh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/images/2020-04-30-react-re-rendering-components/vdom.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0dhzdFmh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/images/2020-04-30-react-re-rendering-components/vdom.jpg" alt="Virtual DOM"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Performance
&lt;/h2&gt;

&lt;p&gt;When we talk about renders in React, we talk about the execution of the render function, which doesn't always imply an update of the UI.&lt;/p&gt;

&lt;p&gt;Let's see this in 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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;App&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMessage&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="nx"&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;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Info&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Info&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&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;In functional components, the execution of the whole function is the equivalent of the render function in class components.&lt;/p&gt;

&lt;p&gt;When the state changes in the higher-order component (HOC, in this case, App), the two Info components will re-render, even though the second one doesn't even receive any props.&lt;/p&gt;

&lt;p&gt;This translates to having the render function being called 3 times, but actual DOM modifications only happen once in the Info component that displays the message.&lt;/p&gt;

&lt;p&gt;React already optimises this for you, so you don't have to worry too much about performance bottlenecks of UI redraws.&lt;/p&gt;

&lt;p&gt;The execution of these render functions has two drawbacks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React has to run its diffing algorithm on each of those components to check whether it should update the UI.&lt;/li&gt;
&lt;li&gt;All your code in these render functions or function components will be executed again.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first point is arguably not that important since React manages to calculate the difference quite efficiently. &lt;br&gt;
The danger lies in the code that you wrote is being executed over and over on every React render.&lt;/p&gt;

&lt;p&gt;In the example above we have a really small component tree. But imagine what happens if each node has more children and these again might have child-components. &lt;br&gt;
We'll see how we can optimize this.&lt;/p&gt;

&lt;p&gt;To read further &lt;a href="http://localhost:8000/developer/2020-03-30-react-re-rendering-components"&gt;go here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>development</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>React Component Library with Material UI</title>
      <dc:creator>Sean Saranga Amarasinghe</dc:creator>
      <pubDate>Mon, 30 Mar 2020 05:26:09 +0000</pubDate>
      <link>https://dev.to/szaranger/react-component-library-with-material-ui-2nij</link>
      <guid>https://dev.to/szaranger/react-component-library-with-material-ui-2nij</guid>
      <description>&lt;h2&gt;
  
  
  Why Material UI?
&lt;/h2&gt;

&lt;p&gt;Creating a custom component library from scratch can be tedious and time-consuming. Instead, it’s much faster to use one of an existing component libraries well-architected components than to spin up a custom one.&lt;br&gt;
Instead of using Sass or JS in CSS, I prefer using Styled Components for the ability to pass functions and props, write regular CSS, inject custom properties like any other React component and just the overall component-driven nature of it.&lt;br&gt;
Well, the good news is, Material UI pretty much supports it out of the box.&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up Material UI
&lt;/h2&gt;

&lt;p&gt;Install Material-UI's source files via npm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @material-ui/core
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Or using yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;yarn add @material-ui/core
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Material-UI components work without any additional setup, and also don't pollute the global scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;styled-components&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;Button&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@material-ui/core/Button&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;StyledButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;`
  color: white;
  &amp;amp;&amp;amp; :hover {
    color: blue;
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Pretty simple isn't it? &lt;/p&gt;

&lt;h2&gt;
  
  
  Using ThemeProvider for avoiding use of &amp;amp;&amp;amp; everywhere
&lt;/h2&gt;

&lt;p&gt;If we want to be able to style our Material UI components without using &lt;strong&gt;&amp;amp;&amp;amp;&lt;/strong&gt; then we have to do the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&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;StylesProvider&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;@material-ui/styles&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;Button&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@material-ui/core/Button&lt;/span&gt;&lt;span class="dl"&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;StylesProvider&lt;/span&gt; &lt;span class="nx"&gt;injectFirst&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;Button&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Hey&lt;/span&gt; &lt;span class="nx"&gt;there&lt;/span&gt;&lt;span class="o"&gt;!&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Button&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;/StylesProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;All it does is ensure that Material UI’s styles get injected first so that Styled Components classes take priority.&lt;/p&gt;

&lt;p&gt;Read more &lt;a href="https://seanamarasinghe.com/developer/2020-03-16-react-component-library-with-material-ui"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>development</category>
      <category>javascript</category>
      <category>react</category>
      <category>materialui</category>
    </item>
    <item>
      <title>Node.js Memory Management</title>
      <dc:creator>Sean Saranga Amarasinghe</dc:creator>
      <pubDate>Fri, 21 Feb 2020 05:46:34 +0000</pubDate>
      <link>https://dev.to/szaranger/node-js-memory-management-1jlj</link>
      <guid>https://dev.to/szaranger/node-js-memory-management-1jlj</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--333cruMd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/static/57029020f82a0b232314d15633025743/14b42/feature.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--333cruMd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/static/57029020f82a0b232314d15633025743/14b42/feature.jpg" alt="Featured Image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Node.js memory management provides means to dynamically allocate memory chunks for programs when they request them and free it when they are no longer needed, so they can be reused. Application-level memory management can be manual or automatic.&lt;/p&gt;

&lt;h2&gt;
  
  
  Heap
&lt;/h2&gt;

&lt;p&gt;The memory space managed by Node is called Heap. Heap is divided into several different spaces called Generations.&lt;/p&gt;

&lt;h2&gt;
  
  
  New Space
&lt;/h2&gt;

&lt;p&gt;New Space is where objects are first created. It is designed to be small and fast to garbage collect.&lt;/p&gt;

&lt;p&gt;Memory in a new space is managed by a pointer. When a new space is created the pointer moves to the next free spot.&lt;/p&gt;

&lt;p&gt;When the pointer reaches the end of the space, it goes into a Scavenge operation to identify all of the objects in memory that no longer referenced.&lt;/p&gt;

&lt;p&gt;Then it removes these, unpacks the memory and re-allocate the pointer to the next available free spot.&lt;/p&gt;

&lt;h2&gt;
  
  
  Old Space
&lt;/h2&gt;

&lt;p&gt;Objects that survive ~two garbage collection operations~ are promoted into the old space.&lt;/p&gt;

&lt;p&gt;Old space is much larger than the new space, so it implements a different garbage collection algorithm - Mark-sweep&lt;/p&gt;

&lt;p&gt;The GC marks the objects being referenced. Anything not marked is unreachable, and the sweep process will remove them.&lt;/p&gt;

&lt;p&gt;To read more visit &lt;a href="https://seanamarasinghe.com/developer/nodejs-memory-management"&gt;Node.js Memory Management&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>React Concurrent Mode</title>
      <dc:creator>Sean Saranga Amarasinghe</dc:creator>
      <pubDate>Tue, 18 Feb 2020 03:27:50 +0000</pubDate>
      <link>https://dev.to/szaranger/react-concurrent-mode-574e</link>
      <guid>https://dev.to/szaranger/react-concurrent-mode-574e</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uBHPOVBt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/static/085dadaf056ee052570cfac51cb8a17d/76843/feature.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uBHPOVBt--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://seanamarasinghe.com/static/085dadaf056ee052570cfac51cb8a17d/76843/feature.jpg" alt="Post"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Previous workarounds
&lt;/h2&gt;

&lt;p&gt;Debounce and throttle are common workarounds to mitigate for example stutter in an input field while typing.&lt;/p&gt;

&lt;p&gt;Debounce – Only update the list after the user stops typing Throttle – Update the list with a certain maximum frequency&lt;/p&gt;

&lt;p&gt;Similar to git branches&lt;br&gt;
Concurrent Mode is like React working on git branches.&lt;/p&gt;

&lt;p&gt;Eg:- Navigating between two screens in an app&lt;/p&gt;

&lt;p&gt;In React Concurrent Mode, we can tell React to keep showing the old screen(like another git branch), fully interactive, with an inline loading indicator. And when the new screen is ready, React can take us to it.&lt;/p&gt;

&lt;p&gt;Read more about React Concurrent Mode &lt;a href="https://seanamarasinghe.com/developer/react-concurrent-mode"&gt;in this article&lt;/a&gt;&lt;/p&gt;

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