<?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: Sai Maheshwar</title>
    <description>The latest articles on Forem by Sai Maheshwar (@saimaheshwarreddy).</description>
    <link>https://forem.com/saimaheshwarreddy</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%2F1136444%2F7b4ca6e3-923e-41f2-84c2-51c33b1d4774.jpg</url>
      <title>Forem: Sai Maheshwar</title>
      <link>https://forem.com/saimaheshwarreddy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/saimaheshwarreddy"/>
    <language>en</language>
    <item>
      <title>React Hooks Flow: A Deep Dive</title>
      <dc:creator>Sai Maheshwar</dc:creator>
      <pubDate>Tue, 17 Dec 2024 04:59:35 +0000</pubDate>
      <link>https://forem.com/saimaheshwarreddy/react-hooks-flow-a-deep-dive-1f73</link>
      <guid>https://forem.com/saimaheshwarreddy/react-hooks-flow-a-deep-dive-1f73</guid>
      <description>&lt;p&gt;Many developers know React, but a lack of understanding can lead to assumptions about how things work. Having a clear understanding of the hooks flow in React can greatly improve your code quality. After working with React for over 4 years, I'd like to share some key insights about the flow of React hooks, a concept that is often overlooked.&lt;/p&gt;

&lt;p&gt;Before diving into hooks flow, I would like to revisit the rules of hooks&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Only call Hooks at the top level&lt;/strong&gt;&lt;br&gt;
     Don’t call Hooks inside loops, conditions, or nested &lt;br&gt;
     functions.Instead, always use Hooks at the top level of your &lt;br&gt;
     React function, before any early returns&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Only call Hooks from React functions(Component or Custom Hook)&lt;/strong&gt;&lt;br&gt;
     Don’t call Hooks from regular JavaScript functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React Hooks Flow&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This React hooks flow diagram helps visualize the order in which hooks are called during different phases of a component's lifecycle.&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%2Fmmfo4gmeqj3iyer7lybn.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%2Fmmfo4gmeqj3iyer7lybn.png" alt="React hook flow diagram" width="760" height="969"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's explore each phase of the component lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Initial Mount&lt;/strong&gt;&lt;br&gt;
This is when a component loads on the screen for the first time.&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%2Ftwhnicxighscu6wagqg1.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%2Ftwhnicxighscu6wagqg1.png" alt="React component initial mount" width="603" height="889"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run lazy initializers&lt;/strong&gt;&lt;br&gt;
In React, state initialization occurs only once throughout a component's lifecycle.&lt;/p&gt;

&lt;p&gt;By 'initializing,' I mean the initial value of &lt;code&gt;useState&lt;/code&gt; will be assigned to the variable only once.&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%2Fbav532xloezzevypwpf4.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%2Fbav532xloezzevypwpf4.png" alt="Image description" width="646" height="85"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;this is quite normal and usually not a concern. However, it becomes more important when initialization involves complex computations, such as setting state from local storage.&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;userList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUserList&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&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;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;usersList&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;currently, localStorage.getItem("usersList") is executed on every re-render, but its result isn't assigned to the state variable. This repeated evaluation can lead to performance issues. To avoid this, you can pass a callback function to &lt;code&gt;useState&lt;/code&gt;, which will only be executed once, during the mounting phase.&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;userList&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUserList&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&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;localStorage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;usersList&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;This is called lazy initialization of state, read more &lt;a href="https://react.dev/reference/react/useState#avoiding-recreating-the-initial-state" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lazy initialization helps prevent unnecessary initialization logic from running on every re-render.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Only use the initializer function if there is a computation involved in state initialization.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Render&lt;/strong&gt;&lt;br&gt;
In this step React calls the function(component) to generate the virtual DOM representation of that component. This process does not directly mean that the component is visible to the user on the screen. Instead, it involves React creating a virtual DOM object that describes what the UI should look like.&lt;/p&gt;

&lt;p&gt;Here's an example of a virtual DOM (VDOM) object:&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ComponentA&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="k"&gt;return&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;Hello World!&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;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;VDOM object&lt;br&gt;
&lt;code&gt;{ &lt;br&gt;
            $$typeof: Symbol(react.element),&lt;br&gt;
             key: null,&lt;br&gt;
             props: {children: 'Hello World!'},&lt;br&gt;
             ref: null,&lt;br&gt;
             type: "h1" ,&lt;br&gt;
             .....&lt;br&gt;
             };&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React updates DOM&lt;/strong&gt;&lt;br&gt;
After generating the virtual DOM and reconciling it to get the final virtual DOM, React updates the actual DOM accordingly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run LayoutEffects&lt;/strong&gt;&lt;br&gt;
This step involves running the &lt;code&gt;useLayoutEffect&lt;/code&gt; hook, which is similar to &lt;code&gt;useEffect&lt;/code&gt; but executes before the browser performs any painting, see more &lt;a href="https://react.dev/reference/react/useLayoutEffect" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Browser paints screen&lt;/strong&gt;&lt;br&gt;
Browser paints the DOM onto the screen&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Run Effects&lt;/strong&gt;&lt;br&gt;
This is the step where the &lt;code&gt;useEffect&lt;/code&gt; hook runs, i.e after rendering the UI on the browser&lt;/p&gt;

&lt;p&gt;If there is a state update in a &lt;code&gt;useEffect&lt;/code&gt;, it trigger the update lifecycle of the component&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;br&gt;
When any state update is triggered, it triggers update lifecycle of the component&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%2Fgtrnoqx6wn0z9gf9unsb.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%2Fgtrnoqx6wn0z9gf9unsb.png" alt="Image description" width="603" height="889"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Render&lt;/strong&gt;, &lt;strong&gt;React updates DOM&lt;/strong&gt;&lt;br&gt;
These steps are similar to the mounting phase&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cleanup LayoutEffects&lt;/strong&gt;&lt;br&gt;
After creating the virtual DOM (VDOM) and updating the actual DOM, React runs the cleanup functions for &lt;code&gt;useLayoutEffect&lt;/code&gt; hooks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If the dependencies of a &lt;code&gt;useLayoutEffect&lt;/code&gt; haven’t changed, its cleanup function will not run.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Run LayoutEffects&lt;/strong&gt;&lt;br&gt;
If any dependency comparison returns false, indicating a change, the &lt;code&gt;useLayoutEffect&lt;/code&gt; will run again; otherwise, it will not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Browser paints screen&lt;/strong&gt;&lt;br&gt;
At this point, the browser renders the updated DOM onto the screen.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cleanup Effects&lt;/strong&gt;, &lt;strong&gt;Run Effects&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;useEffect&lt;/code&gt; cleanup and execution happen only if there is a change in dependencies compared to the previous render. If no dependencies have changed, neither the cleanup function nor the effect will run.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unmount&lt;/strong&gt;&lt;br&gt;
This is the final phase of a component's lifecycle, where the component is removed from the DOM and is no longer visible to the user.&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%2Fm10sielrl6e7ew1x3fhw.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%2Fm10sielrl6e7ew1x3fhw.png" alt="Image description" width="603" height="889"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cleanup LayoutEffects&lt;/strong&gt;, &lt;strong&gt;Cleanup Effects&lt;/strong&gt;&lt;br&gt;
Before removing a component from the DOM, React runs all cleanup functions for both &lt;code&gt;useLayoutEffect&lt;/code&gt; and &lt;code&gt;useEffect&lt;/code&gt; hooks.&lt;/p&gt;

&lt;p&gt;Next time you encounter a bug in the UI, visualize the hooks flow to pinpoint the right spot in your code to investigate and resolve the issue. I hope this helps you improve your coding skills. Happy coding!&lt;/p&gt;

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