React | The ABC of Lifecycle Methods
Published: 2021-01-28 23:39:56


Hi Developers! In a couple of blogs ago we talked about the two ways to create a component. These are: functional components and class components. When we create a class component, we have the advantage that by extending it from <strong>React.Component</strong> it will provide us with more methods and properties, which will exponentially improve the component. Part of that methods is what we know as Lifecycle methods. Components in React goes through a lifecycle of events. An easy way to understand this is by thinking about the lifecycle of a plant.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z0732omibab5xlxfk5rp.jpg)

1. You sow the plant.
2. The plant grows and changes with time.
3. The plant dies.

To understand this better is necessary to know that React.Component has three life cycles:

a. Mounting: when a component is instantiated and inserted into the DOM
b. Updating: when the properties (props) or the state (state) of the component undergo any change.
c. Unmounting: when the component is removed from the DOM.


Each life cycle will provide us with different life cycle methods for the component, depending on which phase it is in, we will be able to use:

<strong><i>a. Mounting</i> -> render() ->  <strong>componentDidMount()</strong></strong>

 1. It is invoked immediately after the component is mounted. 
 2. This loads data from an API.
 3. <strong>componentDidMount()</strong> allows the use of setState(). Calling the setState() here will update state and cause another rendering but it will happen before the browser updates the UI. This is to ensure that the user will not see any UI updates with the double rendering.

<strong><i>b. Updating</i> -> render() -> <strong>componentDidUpdate()</strong></strong>

 1. Invoked immediately after the update occurs.
 2. This method is NOT called in the initial rendering.
 3. It is useful for making API requests, as long as the properties (this.props) are compared with the prevProps argument, so that if there are no changes, it does not affect.
 4. We can modify the state (setState), but, like the previous point, the current this.state must be compared with the previous prevState, since otherwise you can generate an infinite loop.

<strong><i>c. Unmounting</i> --> <strong>componentWillUnmount()</strong></strong>

 1. Invoked immediately before disassembling and destroying the component.
 2. It is useful for "cleaning tasks" suchs a timers, network requests, subscriptions, etc. 
 3. This component will never be re-rendered and because of that we cannot call setState() during this lifecycle method.

This diagram below is from the official React documentation. Here you can see the different React lifecycle methods and when they are invoked.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6xbc5vufrzalckwa6c29.png)

Lifecycles are not the easiest part to explain in React and it's much more than this blog. I hope at least this little preview helped you understand the basics so you can go deeper into these methods. To be honest, it took me a lot of reading and playing with my code and the console to understand how they work, but once you got it, you got it. To learn more about lifecycles I recommend this <a href="https://www.w3schools.com/react/react_lifecycle.asp">documantation</a> from w3schools. The way they explain lifecycles is really simple and easy to follow, so go and check their website now. You can thank me later. 

Thank you for reading! :)