Diving into React useEffect
Tags: react, hooks, javascript, insight
Published: 2020-06-22 12:51:04

<h1>Introuduction</h1>
<p>
The first time you used useEffect, it was amazing the work it could do with just one inbuilt function. I began to love it more the the class high-order-components. I got to know more when I had a webinar with <a href="https://mobile.twitter.com/dan_abramov">Dan Abramov</a>. I then dive more into by follow up on <a href="https://overreacted.io/a-complete-guide-to-useeffect/">Dan's blog</a> because I wanted to understand.
</p>
<h1>The Notion</h1>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eddkh09wbu7t2vgfsreg.png)
<p>
Most developers including me back then, think that when the button is clicked our count state changes and update automatically. Well that might be a useful first intuition when you learn React but it’s not an accurate mental model.
</p>
<p>
Before you understand how this works, you need to unlearn the class base high-order-component hierarchy and start learning the useEffect ways. 
</p>
<h4>Look at this</h4>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3mpwc4il8kijg3asa3xz.png)
<p>In this example, count is just a number. The first time our component renders, the count variable we get from useState() is 0. When we call setCount(1), React calls our component again. This time, count will be 1. And so on.</p>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3mx0ub282vqz32m7rr1d.png)
<p>Whenever we update the state, React calls our component. Each render result “sees” its own counter state value which is a constant inside our function.</p>
<p>It only embeds the {count} number value into the render output. That number is provided by React. When we setCount, React calls our component again with a different count value. Then React updates the DOM to match our latest render output.

The key takeaway is that the count constant inside any particular render doesn’t change over time. It’s our component that’s called again and each render “sees” its own count value that’s isolated between renders.</p>
<h1>Keep In Mind</h1>
<p>So what I am saying is render has it own everything(props,state,handlers,functions). <strong>One thing to know that effects run after every render, are conceptually a part of the component output, and “see” the props and state from that particular render.</strong></p>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eddkh09wbu7t2vgfsreg.png)
<p>If I click several times with a small delay, what is the log going to look like?

Each one belonging to a particular render and thus with its own count value. You can try it yourself:</p>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/j9il5w0m1dgt5696m689.gif)
<h4>Lets look at the class implementation</h4>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z7u0wf7rhocext59c1y2.png)
<p>However, this.state.count always points at the latest count rather than the one belonging to a particular render. So you’ll see 5 logged each time instead:</p>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/odqg4mr5gmllc3tdzafy.gif)
I hope you get the difference.
<h1>Conclusion</h1>
<p>React only runs the effects after letting the browser paint. This makes your app faster as most effects don’t need to block screen updates. Effect cleanup is also delayed. The previous effect is cleaned up after the re-render with new props.</p>
<p>
Each render is has its own props and state, functions and even even handlers. The key takeaway is that it’s our component that’s called again and each render “sees” its own props and states, function and event handlers that are isolated between renders.
</p>

<h1>Dive Deeper</h1>
<p>To dive deeper, continue reading more <a href="https://overreacted.io/a-complete-guide-to-useeffect/">Dan's blog has it into details</a></p>
<h1>Connect With Me</h1>
<p>Connect with me and lets share ideas that will help developers learning new technologies. Hit me on <a href="https://twitter.com/FiifiDr">Twitter</a></p>