The Lifecycle of React Hooks Component

Full-Stack Developer, Entrepreneur and Co-Founder of Coderplex.
Building https://coderplex.in
Checkout my portfolio at https://bhanuteja.dev
Search for a command to run...

Full-Stack Developer, Entrepreneur and Co-Founder of Coderplex.
Building https://coderplex.in
Checkout my portfolio at https://bhanuteja.dev
Thanks Aaron 😀. Glad it was helpful.
Thanks Tapas 😀
In this series, I will write different blog posts based on workshop content from [Kent C. Dodds](https://kentcdodds.com)'s [EpicReact.Dev](https://epicreact.dev) as I go through it.
Hello World 👋 Welcome to the new series that I am starting on React called My Review of Kent C. Dodds's EpicReact.Dev. This is the first article in this series and I will start it off with the introduction to what this series will be about. This ser...
Authored in connection with the Write With Fauna program. Table of Contents Authentication Setting up Fauna in Next.js Installing Fauna Setting up Migrations Tool Authentication and Authorization in Fauna Next.js Serverless Function Setup for Fa...

I wanted to start blogging in Aug of 2020. I decided to use @hashnode for my blog. It was the best decision that I made. I wrote 27 technical articles so far. Most of them are about frontend web development. A thread 🧵 about each of these articles i...

Next.js has become my go-to framework for almost every project that I make. So, I made a starter template that I can just use and get started easily. In this article, I will show you how to use the starter template that I made and deploy it with Verc...

In this article, I will list out all the git commands that I use very frequently. This is not in any way a complete list, just the commands that I use very often. This is intended to be used as a quick reference to perform an action that you want. ...

Bhanu Teja Pachipulusu's blog
29 posts
Developer, Indie Maker and Blogger. Currently building MDX.one
Checkout my portfolio
In this article, we will see the order in which different useEffect callbacks and cleanups happen. We will also see how it differs when the app mounts, unmounts, updates.

This image is taken from https://github.com/donavon/hook-flow.
I took the example shown in this article from
React Hooksworkshop in EpicReact.Dev by Kent C. Dodds.I have added relevant links at the end of this article. Check those out for more detailed video explanations given by Kent C. Dodds.
Every component has three phases:
This stage is when the component initially mounts on a page. In this stage, the flow of hooks is as follows:
useState and useReducer. Those functions will be run only in this mount stage.useState hooks and other things are present.This stage is when the component updates. An update can happen for all the following reasons:
In this stage, the flow of hooks is as follows:
useEffect) useLayoutEffect also has a cleanup phase.As you can see, this is similar to what we saw for the mount stage, except that this also has Cleanup Layout Effects and Cleanup Effects.
This stage is when the component unmounts from a page.
In this stage, the flow of hooks is as follows:
Only cleanups will be run in this stage.
Before we see an example, let's take a look at 3 different types of useEffect callbacks.
useEffect(() => {
console.log('useEffect(() => {})') // Line 1
return () => {
console.log('useEffect(() => {}) cleanup') // Line 2
}
})
This useEffect callback has no dependencies.
useEffect(() => {
console.log('useEffect(() => {}, [])') // Line 1
return () => {
console.log('useEffect(() => {}, []) cleanup') // Line 2
}
}, [])
This useEffect callback has empty dependencies.
Note: This useEffect callback will not be executed when the component updates because of the empty dependency array.
useEffect(() => {
console.log('useEffect(() => {}, [count])') // Line 1
return () => {
console.log('useEffect(() => {}, [count]) cleanup') // Line 2
}
}, [count])
This useEffect callback has one or more dependencies.
Consider the below example
import React from "react";
function App() {
console.log("App: render start");
const [showChild, setShowChild] = React.useState(() => {
console.log("App: useState(() => false)");
return false;
});
console.log(`App: showChild = ${showChild}`);
React.useEffect(() => {
console.log("App: useEffect(() => {})");
return () => {
console.log("App: useEffect(() => {}) cleanup");
};
});
React.useEffect(() => {
console.log("App: useEffect(() => {}, [])");
return () => {
console.log("App: useEffect(() => {}, []) cleanup");
};
}, []);
React.useEffect(() => {
console.log("App: useEffect(() => {}, [showChild])");
return () => {
console.log("App: useEffect(() => {}, [showChild]) cleanup");
};
}, [showChild]);
const element = (
<>
<label>
<input
type="checkbox"
checked={showChild}
onChange={(e) => setShowChild(e.target.checked)}
/>{" "}
show child
</label>
<div>
{showChild ? <Child /> : null}
</div>
</>
);
console.log("App: render end");
return element;
}
import React from "react";
function Child() {
console.log(" Child: render start");
const [count, setCount] = React.useState(() => {
console.log(" Child: useState(() => 0)");
return 0;
});
console.log(` Child: count = ${count}`);
React.useEffect(() => {
console.log(" Child: useEffect(() => {})");
return () => {
console.log(" Child: useEffect(() => {}) cleanup");
};
});
React.useEffect(() => {
console.log(" Child: useEffect(() => {}, [])");
return () => {
console.log(" Child: useEffect(() => {}, []) cleanup");
};
}, []);
React.useEffect(() => {
console.log(" Child: useEffect(() => {}, [count])");
return () => {
console.log(" Child: useEffect(() => {}, [count]) cleanup");
};
}, [count]);
const element = (
<button onClick={() => setCount((previousCount) => previousCount + 1)}>
{count}
</button>
);
console.log(" Child: render end");
return element;
}
App component and Child component.App component has a state which decides whether to show the Child component or not.Child component has a count state.Child has a button to update the count.App and Child has three types of useEffect callbacksuseEffect with no dependenciesuseEffect with empty dependenciesuseEffect with one or more dependencies.We will see how the flow looks like for each of the following steps:
Here the App is in mount phase, so from the diagram, the order should be
When the App is mounted, we see the following console logs.
useEffect with no dependecies is being executed.useEffect with empty dependecies is being executed.App component, and in mount phase all the useEffect callbacks will be called.useEffect with showChild as dependecy is being executed.App component, and in mount phase all the useEffect callbacks will be called.Notes:
useEffect callbacks will get executed on the initial mount of the componentuseEffect callbacks will be run in the order in which they appear.Let's click on show child checkbox. This will mount the Child component.
Here Child will be in the mount phase and App will be in the update phase.
As per diagram, the order for Child will be
And for App,
We will see the following console logs.
showChild dependencies cleanup.showChild is getting updated here.Child component, and in mount phase all the useEffect callbacks will be called.count as dependency is being executed.Child component, and in mount phase all the useEffect callbacks will be called.showChild dependencies is being executed.showChild has updated.Notes:
App component, we have <Child /> in its markup. But you can see the Child render starts after the App render ends.<Child /> is not same as calling calling Child function. It's basically calling React.createElement(Child). Child when it's time for rendering it.Let's click on the count button to update the count present in Child.
Here Child will be in the update phase and App has no change.
As per diagram, the order for Child will be
We will see the following console logs
count as dependency cleanup.count has updated. count as dependency is being executed.count has updated.Let's click on the show child checkbox to unmount the Child component.
Here Child will be in unmount phase and App will be in update phase
As per diagram, the order for Child will be
And for App,
We will see the following console logs
count as dependency cleanupshowChild as dependency clean up.showChild has updated here.showChild as dependency is getting executedshowChild has updated here.And finally, when the App component also unmounts, the cleanup of all the App useEffects will be called.
EpicReact.Dev by Kent C. DoddsThe Beginners Guide To React by Kent C. DoddsIn the next article, we will look at what lifting state and colocating state mean in React. And also we will see when they will be useful.
If you liked this article, check out
If you have any comments, please leave them below or you can also @ me on Twitter (@pbteja1998), or feel free to follow me.