<?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: gursidak_singh</title>
    <description>The latest articles on DEV Community by gursidak_singh (@sidak).</description>
    <link>https://dev.to/sidak</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%2F532817%2F5c402082-82e0-4b09-ba6a-4d45630335ef.jpeg</url>
      <title>DEV Community: gursidak_singh</title>
      <link>https://dev.to/sidak</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sidak"/>
    <language>en</language>
    <item>
      <title>Mastering the Art of Pain-Free Remote Work: Your Ultimate Guide to Avoiding Neck and Back Pain in the IT Industry</title>
      <dc:creator>gursidak_singh</dc:creator>
      <pubDate>Sun, 16 Jul 2023 10:40:05 +0000</pubDate>
      <link>https://dev.to/sidak/mastering-the-art-of-pain-free-remote-work-your-ultimate-guide-to-avoiding-neck-and-back-pain-in-the-it-industry-5gpd</link>
      <guid>https://dev.to/sidak/mastering-the-art-of-pain-free-remote-work-your-ultimate-guide-to-avoiding-neck-and-back-pain-in-the-it-industry-5gpd</guid>
      <description>&lt;p&gt;Welcome to the comprehensive guide on how to prevent neck and back pains caused by remote work in the IT industry. As technology continues to revolutionize the way we work, more and more professionals find themselves spending long hours in front of screens, leading to increased risks of discomfort and musculoskeletal issues. In this blog, we will explore practical tips and expert advice to help you maintain a healthy posture, minimize strain, and achieve a pain-free remote work experience. Let's dive in!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Ergonomic Setup: Creating an Optimal Workstation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Invest in a supportive chair with adjustable features such as lumbar support and armrests. Ensure proper height adjustment to align your knees, hips, and elbows at 90-degree angles.&lt;/li&gt;
&lt;li&gt;Position your monitor at eye level to avoid straining your neck. Use a monitor stand or stack of books if necessary.&lt;/li&gt;
&lt;li&gt;Use an external keyboard and mouse for comfortable arm and wrist alignment. Place them at a height that allows your forearms to rest parallel to the floor.&lt;/li&gt;
&lt;li&gt;Arrange your desk setup to promote good posture and reduce strain. Keep frequently used items within easy reach to minimize excessive reaching or twisting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. The Power of Posture: Maintaining a Healthy Alignment
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Sit up straight with your back supported by the chair's backrest. Avoid slouching or hunching forward.&lt;/li&gt;
&lt;li&gt;Keep your feet flat on the floor or use a footrest if they don't reach. This helps stabilize your lower body and reduces strain on your back.&lt;/li&gt;
&lt;li&gt;Relax your shoulders and let them naturally fall downward. Avoid shrugging or tensing them up.&lt;/li&gt;
&lt;li&gt;Take regular breaks to stretch and walk around. Set reminders to encourage movement and change positions throughout the day.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Stretching and Strengthening Exercises: Relieving Tension and Building Resilience
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Incorporate stretching exercises into your daily routine to loosen tight muscles. Focus on neck, shoulder, and back stretches to alleviate tension.&lt;/li&gt;
&lt;li&gt;Perform strengthening exercises to support your core muscles, which play a vital role in maintaining proper posture.&lt;/li&gt;
&lt;li&gt;Explore yoga or Pilates routines specifically designed for desk workers. These exercises can improve flexibility, strengthen muscles, and promote relaxation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Mindful Work Habits: Prioritizing Health and Well-being
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Practice regular mindfulness and relaxation techniques to reduce stress and tension in your body. Deep breathing exercises can help release muscle tension and promote relaxation.&lt;/li&gt;
&lt;li&gt;Avoid multitasking and focus on one task at a time. This allows you to maintain proper posture and minimize strain on your neck and back.&lt;/li&gt;
&lt;li&gt;Optimize your work environment by adjusting lighting and minimizing glare on your screen. Position your desk and monitor to avoid reflections that strain your eyes.&lt;/li&gt;
&lt;li&gt;Stay hydrated and maintain a balanced diet to support your overall well-being. Proper hydration helps keep your muscles and joints lubricated.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Technology Usage Awareness: Optimizing Your Digital Experience
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use software tools and apps that remind you to take breaks and maintain good posture. Set timers or use productivity apps that encourage regular movement.&lt;/li&gt;
&lt;li&gt;Adjust your screen settings to reduce eye strain and minimize blue light exposure. Consider using a blue light filter or wearing blue light-blocking glasses.&lt;/li&gt;
&lt;li&gt;Utilize a document holder to keep reference materials at eye level, reducing the need for constant neck movement.&lt;/li&gt;
&lt;li&gt;Experiment with voice recognition software to reduce typing and mouse usage. This can alleviate strain on your wrists and fingers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Conclusion:&lt;/p&gt;

&lt;p&gt;In today's remote work landscape, taking proactive steps to prevent neck and back pains is crucial for IT professionals. By implementing ergonomic principles, maintaining good posture, incorporating stretching exercises, and practicing mindful work habits, you can significantly reduce the risk of discomfort and enhance your&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Magic!, useEffect running twice 🤔</title>
      <dc:creator>gursidak_singh</dc:creator>
      <pubDate>Sun, 12 Feb 2023 17:54:04 +0000</pubDate>
      <link>https://dev.to/sidak/magic-useeffect-running-twice-2c58</link>
      <guid>https://dev.to/sidak/magic-useeffect-running-twice-2c58</guid>
      <description>&lt;p&gt;React's useEffect hook is an essential tool for managing side effects in your components. However, it's not uncommon to run into issues where the useEffect hook appears to be running twice in development mode, causing unexpected behavior in your application.&lt;/p&gt;

&lt;p&gt;This issue occurs because React updates its components in a specific way during development mode. When a component re-renders, React first calls the component with the updated state and props, and then calls the useEffect hook with the same values. This ensures that the component and its effects are always in sync.&lt;/p&gt;

&lt;p&gt;However, if you're not careful, this can lead to unexpected behavior. For example, consider the following code snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useState, useEffect } from 'react';

const Example = () =&amp;gt; {
  const [count, setCount] = useState(0);

  useEffect(() =&amp;gt; {
    console.log('The count is:', count);
  }, [count]);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;The count is: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;
        Increment
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

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

&lt;/div&gt;



&lt;p&gt;In this example, we have a simple component that displays a count and increments it when a button is clicked. The useEffect hook logs the current count to the console whenever it changes.&lt;/p&gt;

&lt;p&gt;If you run this code in development mode, you'll notice that the useEffect hook is called twice for each update: once with the old count and once with the new count. This is because React is first calling the component with the old values, and then calling the component and its effects with the updated values.&lt;/p&gt;

&lt;p&gt;To avoid this issue, it's important to understand how React updates its components in development mode. You can also use the useDebugValue hook to display a custom label for your hook in the React DevTools, making it easier to distinguish between the initial render and subsequent re-renders.&lt;/p&gt;

&lt;p&gt;Here's an updated example that uses useDebugValue:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
import React, { useState, useEffect, useDebugValue } from 'react';

const Example = () =&amp;gt; {
  const [count, setCount] = useState(0);

  useDebugValue(count, count =&amp;gt; `Count: ${count}`);

  useEffect(() =&amp;gt; {
    console.log('The count is:', count);
  }, [count]);

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;The count is: {count}&amp;lt;/p&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;
        Increment
      &amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};


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

&lt;/div&gt;



&lt;p&gt;With this change, you can see the custom label in the React DevTools and easily distinguish between the initial render and subsequent re-renders.&lt;/p&gt;

&lt;p&gt;In conclusion, understanding why the useEffect hook may run twice in development mode is essential for avoiding unexpected behavior in your application. By using the useDebugValue hook, you can easily identify and debug issues related to double rendering in your application.&lt;/p&gt;

</description>
      <category>react</category>
      <category>nextjs</category>
      <category>useeffect</category>
    </item>
    <item>
      <title>How Suspense works in React18</title>
      <dc:creator>gursidak_singh</dc:creator>
      <pubDate>Fri, 01 Jul 2022 08:44:01 +0000</pubDate>
      <link>https://dev.to/sidak/how-suspense-works-in-react18-3nm</link>
      <guid>https://dev.to/sidak/how-suspense-works-in-react18-3nm</guid>
      <description>&lt;p&gt;React is one of the trending and most popular client-side libraries for developing painless interactive UIs. Already it has its roots deep in the market, but obviously, the goal is to achieve the best UI experience but not better. So React comes up with another major feature in its Version 18 that makes the User Experience faster and more interactive, which can be termed as "Non-Blocking User Experience". The term itself defines its meaning enough. That there's something that's gonna remove all the obstacles that make it slow while loading and interacting with any react app.&lt;/p&gt;

&lt;p&gt;So Let's Dig Deep into the underlying concept to understand well the working of these features that makes it lighting fast.&lt;/p&gt;

&lt;p&gt;Majorly there are two ways to use React &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Client-Side Rendered App&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Server Side Rendered App&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's Start our Discussion on Problems with These two, that react 18 solves&lt;/p&gt;

&lt;h2&gt;
  
  
  Client-Side Rendering
&lt;/h2&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%2Fuploads%2Farticles%2Fwrph7exbpzx1aejbj69c.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%2Fuploads%2Farticles%2Fwrph7exbpzx1aejbj69c.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above image all the stages of providing a view to the user, when he requests a page on react app is shown&lt;/p&gt;

&lt;p&gt;It can be seen clearly that after the user request a page, the very first step that is performed is to LoadJs, then it fetches the data required for the views, and finally, it renders the components by the process of hydration &lt;em&gt;(the process of making webpage interactive by connecting each DOM components with its respective logic)&lt;/em&gt;.&lt;br&gt;
Now the problem here can be seen clearly that until the process of hydration is not completed the user sees a blank white screen, which is a very bad user experience, and if there is some component that has large bulky Js with it to compile and render view, It makes it weirder. Also in case of a slow internet connection, it takes a lot of time to load the script and render an interactive view and until the script is loaded what the user sees is a blank screen.&lt;/p&gt;

&lt;p&gt;Now the question that arises in mind is that we already have a solution for this problem and that is to render pages from the server-side&lt;/p&gt;

&lt;p&gt;So, Let's discuss that case as well&lt;/p&gt;

&lt;h2&gt;
  
  
  Server-Side Rendering
&lt;/h2&gt;

&lt;p&gt;Now what will happen in server-side rendering is, It will not resolve the problem of large javascript bundle or script also not of low internet speed, but it acts as an additional layer of optimisation over a client-side rendered app by providing pre-rendered HTML/CSS pages that prevent the browser from showing the blank screen, although these pages are not interactive until the process of hydration is done. But still, a better user experience than Client-sider rendered web app.&lt;/p&gt;

&lt;p&gt;A typical app uses server-side rendering works in this way.&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%2Fuploads%2Farticles%2Fejst6e9yxbesc1duptdd.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%2Fuploads%2Farticles%2Fejst6e9yxbesc1duptdd.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When a browser receives a request for a page, On the server it fetches data for the entire web page, also on the server, all components are rendered as HTML/CSS. Now this HTML is sent to the browser and browser hence rendered in the browser. &lt;/p&gt;

&lt;p&gt;Clearly, for large and heavy web apps server-side rendering is very useful, instead of rendering the blank page to the user, the browser shows HTML/CSS rendered page and till the time user is getting the HTML/CSS of the page browser hydrates it to make it interactive.&lt;/p&gt;

&lt;p&gt;Now the problem of a blank page is solved to much extent, but not completely as server rendering renders an HTML/CSS-based view to the user but it's not interactive.&lt;br&gt;
Consider the case you go to the browser and request "&lt;a href="https://youtube.com" rel="noopener noreferrer"&gt;https://youtube.com&lt;/a&gt;" and you get a server-side rendered HTML/CSS but until the hydration of large bulky JS is not completed or consider you have a slow internet connection, the page will not become interactive till then, buttons will not be clicked, videos will not be played, and that will make it useless even its showing view to the user. We need javascript to make it interactive&lt;/p&gt;

&lt;p&gt;Now, this is server-side rendering explained in four steps :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We fetch the data on the server for &lt;em&gt;&lt;strong&gt;entire application&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We Render the code into HTML for &lt;em&gt;&lt;strong&gt;entire application&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We Load the Js on the browser for &lt;em&gt;&lt;strong&gt;entire application&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;We hydrate the components to make the app interactive, for &lt;em&gt;&lt;strong&gt;entire application&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Yes, we do every step for &lt;strong&gt;entire application&lt;/strong&gt; and simultaneously in server-side rendering. At every stage whole application is processed at once(in one pass).&lt;/p&gt;

&lt;p&gt;Let's understand it well with an example - Suppose we have an app that has the following components as shown in the image below&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%2Fuploads%2Farticles%2F4ugcs4ivyqdmtacx3794.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%2Fuploads%2Farticles%2F4ugcs4ivyqdmtacx3794.png" alt="Demo Project Structure containing different components"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have Navbar, Sidebar, Posts, and Comments components.&lt;br&gt;
Now suppose we have component  that has heavy Js and large expensive API requests for a large amount of data. We can say it's a troublemaker part of the application. Now, this component will make trouble at every stage, Also we can't skip this component as without posts component application will be useless.&lt;/p&gt;

&lt;p&gt;The main problems can be &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Problem 1 - &lt;strong&gt;Fetch Everything, before you can Show Anything&lt;/strong&gt; &lt;br&gt;
Now as discussed above in server-side rendering how server fetches data of all application and renders it into HTML pages and provide a browser to show it to the user, but code can't be sent for rendering until all the data is fetched and remember Posts component involved heavy and large API requests. Now it will put us back into the same situation where the user will see a blank page until pre-rendering is done and the browser receives a static view to render.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Problem 2 - &lt;strong&gt;Load Everything, before you can Hydrate anything&lt;/strong&gt;&lt;br&gt;
This means we need to load the JS of the entire application before we can hydrate anything. Now again Posts is the component with heavy Js&lt;br&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%2Fuploads%2Farticles%2Ff70reyf78tkb916z9kx8.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%2Fuploads%2Farticles%2Ff70reyf78tkb916z9kx8.png"&gt;&lt;/a&gt;&lt;br&gt;
We can see every component has been Loaded except the  component, which is still loading its Js.&lt;br&gt;
Now again in pre-18 versions of reacting the app will wait for hydration until all the components have loaded their JS.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Problem 3 - &lt;strong&gt;Hydrate Everything, before you can Interact with anything&lt;/strong&gt;&lt;br&gt;
Again the user will not be able to interact with any component until the code gets hydrated, for example, if the user will click on the profile component it will not be opened as there is no event- listeners and logic attached to components(hydration not done yet, due to large javascript connected with Posts component, the whole process gets late).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hence In all 3 problems discussed above, there is something very common that's making delays in each step. So here the react 18 comes into the picture with its amazing feature "Suspense", which solves this problem. Rather than each stage of needs to happen for all the apps in one pass at one time, suspense allows us to break our work into multiple parts.&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%2Fuploads%2Farticles%2F3ddkbvjnowhivqisnkkh.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%2Fuploads%2Farticles%2F3ddkbvjnowhivqisnkkh.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above, wrapping a component in suspense allows us to show another component (passed in fallback), until the actual component is loading and hence resolves the problem of carrying out each step in one pass and that too without facing any delay.&lt;br&gt;
Now the react will keep processing that component in the background and shows another component for example a spinner as a placeholder for that component. &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%2Fuploads%2Farticles%2Ffurktm8khmwjqhvds9q2.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%2Fuploads%2Farticles%2Ffurktm8khmwjqhvds9q2.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hence the initial page render happens sooner, there is no blocking in any stage. The loader provides the user an indicator that something is here that is going to load soon, instead of a blank screen that makes a really bad user experience. Now once the posts are fetched, rendered, loaded, and hydrated they are rendered at that particular place&lt;/p&gt;

&lt;p&gt;Now with this, all the above problems are resolved, &lt;/p&gt;

&lt;p&gt;In the first step, there is no blocking in fetching data due to expensive API requests, once the data fetching of all components, other than those wrapped in  is completed the code is sent for the next stage, and the data fetching for suspense components  keep on happening asynchronously behind&lt;br&gt;
&lt;strong&gt;Hence we need not Fetch Everything before you can Show Anything.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Now after rendering the code as HTML, sent to the browser for loading Js, now again if no bulky Js are blocking the loading stage send the code to the next stage without any delay.&lt;br&gt;
&lt;strong&gt;Hence we need not Load Everything before you can Hydrate anything&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the last stage, all the other components get hydrated except the one with troublemaker behaviour, in place of it the placeholder component provided in the fallback property of suspense is loaded and hydrated. Now all the components are interactive and the user can interact with any components, instead of the fact that posts are still loading. For example, a user can click on a profile and open the profile page, though the posts are loading.&lt;br&gt;
&lt;strong&gt;Hence we need not Hydrate Everything before you can Interact with anything&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is all about React 18. So let's make our apps lightning fast with the great new feature.&lt;/p&gt;

</description>
      <category>react18</category>
      <category>suspense</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Pre-Rendering In NextJs</title>
      <dc:creator>gursidak_singh</dc:creator>
      <pubDate>Tue, 22 Feb 2022 06:22:23 +0000</pubDate>
      <link>https://dev.to/sidak/pre-rendering-in-nextjs-5h96</link>
      <guid>https://dev.to/sidak/pre-rendering-in-nextjs-5h96</guid>
      <description>&lt;h2&gt;
  
  
  What pre-rendering actually means in layman language
&lt;/h2&gt;

&lt;p&gt;Pre-rendering is nothing but process of generating HTML along with necessary data of the content of requested page by client in our application. &lt;/p&gt;

&lt;p&gt;yeah, It might be confusing a bit let's understand it with an example&lt;/p&gt;

&lt;p&gt;In the picture shown below, Two pictures showing the page source of a next and react app &lt;/p&gt;

&lt;p&gt;didn't you get the difference?&lt;/p&gt;

&lt;p&gt;yeah ! its the difference between the html content that both of these are showing, In the first picture, showing react page source shows a single empty div tag with id root&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while if look into the page source of next app running on another tab, we see the whole html code of view that is visible on browser&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"__next"&lt;/span&gt; &lt;span class="na"&gt;data-reactroot&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Home_container__bCOhY"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;main&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Home_main__nLjiQ"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Home_title__T09hD"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          Welcome to &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://nextjs.org"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Next.js!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;a&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;main&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    ----------------------------- 
    some more html code here 
    -----------------------------
  &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;body&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Yes! this is what is the very basic stage from which we can start understanding the concept of pre-rendering&lt;/p&gt;

&lt;p&gt;In react, the html for the page requested is not produced but, the client is provided with the javascript code which on compiling gives us the requested view and this process of initialising app components and rendering view is known as hydration&lt;/p&gt;

&lt;p&gt;Where as in NextJs, instead of generating html after hydration, it is generated pre-handedly and what client receives is the view, although this view is raw html code only without any interactions code and these interactions(Js logical code) get added after the client perform hydration and hence the what user see in browser is complete interactive view. &lt;/p&gt;

&lt;p&gt;As shown in this figure below&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%2Fuploads%2Farticles%2F0l8wlwkjq337f4m99cax.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%2Fuploads%2Farticles%2F0l8wlwkjq337f4m99cax.png" alt="Hydration in React Vs NextJs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's Check it out using examples also &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Image1 showing the data received in network tab on requesting index page in react app&lt;/li&gt;
&lt;/ol&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%2Fuploads%2Farticles%2Ftomwkvupdnjaww5eddfi.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%2Fuploads%2Farticles%2Ftomwkvupdnjaww5eddfi.png" alt="View Rendering in ReactJs"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Image2 showing the data received in network tab on requesting index page in NextJs app&lt;/li&gt;
&lt;/ol&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%2Fuploads%2Farticles%2Fuo0c5aoljgphiwzf8oml.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%2Fuploads%2Farticles%2Fuo0c5aoljgphiwzf8oml.png" alt="next app rendering"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As discussed above, we can clearly see the difference that makes a-lot of differences in working of both and hence on efficiency also.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now the question is how this pre-rendering is useful to the end users ??
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;In react we need to wait for the javascript to be executed also for the data to be fetched from external API's and then finally our UI is rendered. But with pre-rendering the html is already generated, thus the wait time gets decreased significantly, resulting in more efficient and fast loading of requested pages/views.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Another most important concept in which NextJs benefits a-lot is SEO(Search Engine Optimization). Especially if your building an e-commerce web application or blogging website , SEO is of biggest concern.&lt;br&gt;
With React App if Search Engine hits your page, it only sees a "div" tag with id equals to "root", how ever if it hits the pre-rendered page in which all the html source code is present which helps search engine in indexing that page in search results easily&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>React Component LifeCycle Stages</title>
      <dc:creator>gursidak_singh</dc:creator>
      <pubDate>Wed, 19 Jan 2022 06:46:21 +0000</pubDate>
      <link>https://dev.to/sidak/react-lifecycle-4e71</link>
      <guid>https://dev.to/sidak/react-lifecycle-4e71</guid>
      <description>&lt;p&gt;Hey Geeks,&lt;/p&gt;

&lt;p&gt;One of the major parts while learning any language, framework or library is to understand the internal working of its code. Though All the methods and ways of doing things are given in the documentation. And When we are stuck we have the life-saving great tool Stack-overflow to put us back onto the right path. &lt;/p&gt;

&lt;p&gt;Have you ever wondered, who answers your questions on Stack-overflow and how they know the answers to the errors that are created by us 🥸😳 !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ReJvGOPf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5jq2go22iszc3gr3x0k.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ReJvGOPf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l5jq2go22iszc3gr3x0k.jpeg" alt="Image description" width="720" height="589"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, Let's know the working lifecycle of a React Component, to Avoid the errors and bugs we make…&lt;/p&gt;

&lt;p&gt;According to the React Docs of version 16.4 or above, We may classify the Lifecycle of a React Component into 4 main stages through which they pass, Which are as follows&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--P4Q-yIqO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yz7dv62bu4cfqg80zab9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--P4Q-yIqO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yz7dv62bu4cfqg80zab9.png" alt="Different Stages Of React Component" width="880" height="355"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MOUNT&lt;/strong&gt; - The Very First Time when our page loads the components start mounting and we get our beautiful page layout. that stage is known as the mounting of a component. What internally happens is, as we request the webpage by entering the domain name (&lt;a href="https://loadmypage.com"&gt;https://loadmypage.com&lt;/a&gt;), all the components get mounted on the &lt;a href="https://www.w3.org/TR/WD-DOM/introduction.html"&gt;DOM&lt;/a&gt; (i.e., is created and inserted into the DOM).&lt;br&gt;
This phase comes onto the scene after the initialization phase is completed. In this phase, our component renders the first time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt; - Update, we can say is another mount(remounting), that happens whenever there is a change in either state or props of a component, which results in the re-mount of that component. &lt;br&gt;
This is the major thing in react that makes it fast and a robust library. Only the component on which some change happens got updated in the real DOM, instead of constructing the whole DOM tree again on even a single change in any small component of the app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UNMOUNT&lt;/strong&gt; - As the word suggests, When a component is removed from DOM Tree, that phase is known as Unmounting. &lt;br&gt;
For Example - When we log out of our account on some app, the navigation buttons got removed from the header, which can be achieved by Unmounting the component containing the Navbar(Navigation buttons)&lt;br&gt;
This stage may seem like some basic or less important stage, but it's an as crucial stage as mounting. Like in Mount Stage, if a component fails to mount properly, the page may face bugs and errors, in the same way, there are some operations that a user may want to perform before or just after the component unmounts&lt;/p&gt;

&lt;p&gt;Let’s Understand Well With an Example Why Unmount Stage needs to be handled carefully;&lt;br&gt;
Here there are two components&lt;br&gt;
&lt;strong&gt;Index.js&lt;/strong&gt; - Containing the MouseHook Component, and a button to mount and unmount the MouseHook Component by toggling the value of mount state variable;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;MouseHook&lt;/strong&gt; - In this Component Event Listener is Added just after the component is mounted to listen to the mouse movements(x,y), using UseEffect Hook.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nI9NHYrD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdi25h3s1n9lrdloa04q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nI9NHYrD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdi25h3s1n9lrdloa04q.png" alt="Index Component Snapshot" width="880" height="572"&gt;&lt;/a&gt;&lt;br&gt;
(Index Component Snapshot)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vSUsDRpy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwpbmge2hjbv14oox3ox.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vSUsDRpy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwpbmge2hjbv14oox3ox.png" alt="MouseHook Component Snapshot" width="880" height="572"&gt;&lt;/a&gt;&lt;br&gt;
(MouseHook Component Snapshot)&lt;/p&gt;

&lt;p&gt;Hope you got the bug that is there in this code...🧐🧐&lt;br&gt;
Not???? Let's See through the console&lt;/p&gt;

&lt;p&gt;Let's Consider Two Cases &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When MouseHook Component is Mounted(Added and Present in Dom Tree).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m1FpsKpQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29o5ctjt436y0vltuqnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m1FpsKpQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/29o5ctjt436y0vltuqnf.png" alt="Image description" width="880" height="572"&gt;&lt;/a&gt;&lt;br&gt;
Our MouseHook is Mounted and working Right!!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's See What Will happen as we click the button to unmount MouseHook Component&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--w8KRRlhi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h8mwry9vl52sxv34eab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--w8KRRlhi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6h8mwry9vl52sxv34eab.png" alt="Showing the bug " width="880" height="572"&gt;&lt;/a&gt;&lt;br&gt;
OOOOOOPS..... Error!&lt;br&gt;
Why this happened is because we added an Event Listener when the component mounted, but we haven't removed that Event Listener before unmounting the component, which may cause a serious memory leak problem(same that is mentioned in the Error also).....&lt;/p&gt;

&lt;p&gt;Hope now the importance of unmounting stage is more clear.&lt;/p&gt;

&lt;p&gt;Let's Remove the Event Listener before unmounting it, to fix this bug&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ENDhRppb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amutykor6417a6inl5jp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ENDhRppb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/amutykor6417a6inl5jp.png" alt="Cleanup function returned in useEffect to remove EventListner" width="880" height="572"&gt;&lt;/a&gt;&lt;br&gt;
Cleanup function returned in useEffect to remove EventListner.&lt;/p&gt;

&lt;p&gt;Let's See the Results now...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--emzxyIOk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eten0qvkbgw6qtno589y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--emzxyIOk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eten0qvkbgw6qtno589y.png" alt="Working Code" width="880" height="572"&gt;&lt;/a&gt;&lt;br&gt;
The Event Listener is removed before unmounting the component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ERROR&lt;/strong&gt; - This is another important stage whenever any component crashes, it needs to show suitable and proper error messages, with the help of suitable methods and functions, which may help users to understand and fix bugs. Moreover, the bugs/errors are the only ways through which one can learn🙃&lt;/p&gt;

&lt;p&gt;And here we come to the finish line _________________________;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Next Blog&lt;/strong&gt; - Different functions and Methods available to handle the components at different stages differently&lt;/p&gt;

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