life-cycle methods into functional components and hooks. If you started using React with the class components and are not comfortable with this transformation, this tutorial is just for you.
For sake of organization, here are the topics I will be going over.
Life-cycle Methods
Life-cycle methods might look a little bit trickier compared to what I’ve explained so far. We use the useEffect hook for imitating all three life-cycle methods I will be discussing here.
componentDidMount
We use this life-cycle method for the side effects of our component, such as calling an API, etc. when the component is initially rendered. Everything inside this method is called once the initial rendering of the component is completed.


Here is an important note: whether we leave the dependency array empty, pass values in it, or don’t even pass the array itself to useEffect; either way React executes the function in useEffect in the initial rendering, which brings me to the next life-cycle method.
componentDidUpdate (prevProps, prevState)
This life-cycle method is invoked after an update in props or state object occurs. It takes two parameters prevProps and prevState so we can check if the current props or state has changed in the last component update.
Now Unity is configured and ready


If you remove the dependency array completely, the function in useEffect will run in every update of the component.
componentWillUnmount
This life-cycle method is invoked right before the component is unmounted. If you have ongoing side-effects that you started earlier such as a network request or a timer, this is the place you clean them up.

To do the same thing in functional components, we make use of the return keyword inside our function in useEffect. Let’s create the same thing in a functional component.

Another note: Here we passed a function into setCounter method: setCounter(counter => counter + 1). This is to avoid stale closures. Dmitri Pavlutin explain what a stale closure is here very well in case you haven’t heard of it.








