<?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>DEV Community: Fernando Belotto</title>
    <description>The latest articles on DEV Community by Fernando Belotto (@fernandobelotto).</description>
    <link>https://dev.to/fernandobelotto</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%2F386157%2Fa8845ced-8c2c-400c-8c21-4c3392579531.png</url>
      <title>DEV Community: Fernando Belotto</title>
      <link>https://dev.to/fernandobelotto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/fernandobelotto"/>
    <language>en</language>
    <item>
      <title>Understanding lifecycle in React</title>
      <dc:creator>Fernando Belotto</dc:creator>
      <pubDate>Mon, 31 Jan 2022 20:42:46 +0000</pubDate>
      <link>https://dev.to/fernandobelotto/what-is-lifecycle-in-react-bmd</link>
      <guid>https://dev.to/fernandobelotto/what-is-lifecycle-in-react-bmd</guid>
      <description>&lt;p&gt;Simply and directly, lifecycle refers to the 3 stages that a React component goes through during its existence.&lt;br&gt;
Every component in React goes through three stages which are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Mounting&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Update&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unmounting&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;It is important that you, as a React developer, know these 3 phases and what methods are associated with each of them. These methods can be overwritten in class components to perform side effects and manage the state of the component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take a look at these methods in each of the phases:&lt;/p&gt;
&lt;h3&gt;
  
  
  Mouting
&lt;/h3&gt;

&lt;p&gt;This is the initial phase of any component. It corresponds to the moment when the component will be inserted in the DOM. That is, when it starts to exist on your browser's screen.&lt;br&gt;
In this phase there are 4 methods that are executed in the following order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;contructor()&lt;/li&gt;
&lt;li&gt;getDerivedStateFromProps()&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;li&gt;componentDidMount()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of these methods, only the third (render) is mandatory when creating a class component. The others are left to the developer to implement or not in their component.&lt;/p&gt;

&lt;p&gt;The first, &lt;code&gt;constructor&lt;/code&gt;, is used in React components to set the initial state value.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;getDerivedStateFromProps&lt;/code&gt; method is used for one purpose only: It allows a component to update its state through a change of props. See two examples of its use &lt;a href="https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#updating-state-based-on-props"&gt;here&lt;/a&gt; and &lt;a href="https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html#fetching-external-data-when-props-change"&gt;here&lt;/a&gt;. In the following example I set the state of the counter with the value passed in the prop _start_value.&lt;/p&gt;

&lt;p&gt;The third and only mandatory is &lt;code&gt;render&lt;/code&gt;. When called, it must examine this.props and this.state and return one of the following types: a React element, a Portal, a string, a number, a boolean, or the value null. The &lt;code&gt;render&lt;/code&gt; method &lt;strong&gt;should remain pure&lt;/strong&gt;, that is, do not create side effects (such as API calls) in it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

class MyComponent extends React.Component {

    constructor(props){
        super(props);
        this.state = { counter: 0 }
    }

    static getDerivedStateFromProps(props, state) {
        return { counter: props.initialValue }
    }

    render() {
        return &amp;lt;h1&amp;gt;Hello Lifecycle&amp;lt;/h1&amp;gt;
    }

    componentDidMount() {
        console.log('Here my component has rendered 😅')
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Today it is no longer necessary for you to call the constructor to initialize a state, which eliminates the need for it. The getDerivedStateFromProps method adds some complexity to our component and overall you won't need to use it. &lt;a href="https://reactjs.org/blog/2018/06/07/you-probably-dont-need-derived-state.html"&gt;This post on the React blog indicates why not to use derived state&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Updating
&lt;/h3&gt;

&lt;p&gt;When the component overwrites some props change (i.e. its parent component passes new props) or when the internal state overwrites a change (by this.setState({}) for example) the component enters the updating phase.&lt;/p&gt;

&lt;p&gt;Just like in the assembly phase, a defined sequence of methods will be called. They are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;getDerivedStateFromProps()&lt;/li&gt;
&lt;li&gt;shouldComponentUpdate()&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;li&gt;getSnapshotBeforeUpdate()&lt;/li&gt;
&lt;li&gt;componentDidUpdate()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The first method, &lt;code&gt;getDerivedStateFromProps&lt;/code&gt; we already know and covered in the assembly phase. Its behavior is the same here in the update phase.&lt;/p&gt;

&lt;p&gt;The second method, &lt;code&gt;shouldComponentUpdate&lt;/code&gt; will determine whether the methods in the sequence are executed or not. That is, it will determine whether the component should be rendered again or not. This method exists only as a way for us to avoid unnecessary updating, and thus optimize the performance of our applications.&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;this.forceUpdate()&lt;/code&gt; method has been called the update will not call shouldComponentUpdate, and the component will be forcefully updated as the name implies.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getSnapshotBeforeUpdate&lt;/code&gt; is then called just before the most recently rendered output is committed. It allows your component to grab some information from the DOM (for example, the scroll position) before it is potentially changed. Any value returned by this life cycle will be passed as a third parameter, called snapshot, to the componentDidUpdate method.&lt;/p&gt;

&lt;p&gt;With the DOM updated, the &lt;code&gt;componentDidUpdate&lt;/code&gt; method is finally called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

class MyComponent extends React.Component {

    static getDerivedStateFromProps(props, state) {
        return null
    }

    shouldComponentUpdate() {
        return true
    }

    render() {
        return &amp;lt;h1&amp;gt;Hello Lifecycle!&amp;lt;/h1&amp;gt;
    }

    getSnapshotBeforeUpdate(prevProps, prevState){
        return 'this is the snapshot'
    }

    componentDidUpdate(prevProps, prevState, snapshot) {
        console.log('Here my component has updated ♥')
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Mouting
&lt;/h3&gt;

&lt;p&gt;When the component is going to be removed from the DOM, by changing state or props, we are in the disassembly.&lt;br&gt;
Here we have only one life cycle method, which is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;componentWillMount()&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This method is executed just before the component is removed. It is used to remove entries and listeners. An example is with the use of setInterval, that even if the component is removed, it will continue to run regardless of the existence of the component that invoked it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";

class MyComponent extends React.Component {

    componentWillUnmount() {
        console.log('Here my component will disappear 😢')
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Legacy methods
&lt;/h3&gt;

&lt;p&gt;For creating confusion and some hard-to-solve bugs, some React lifecycle methods are being deprecated. In version 17 they can still be used with the UNSAFE_ prefix in front of their names. To understand more about why they are being deprecated, &lt;a href="https://reactjs.org/blog/2018/03/27/update-on-async-rendering.html"&gt;see this post on the React blog&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;UNSAFE_componentWillMount()&lt;/li&gt;
&lt;li&gt;UNSAFE_componentWillReceiveProps()&lt;/li&gt;
&lt;li&gt;UNSAFE_componentWillUpdate()&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;I hope that now you have a better sense of what is the lifecycle! this is definetly an concept that will help you build better and predictable ui in the future. With you have any question about React or other related topics, feel free to reach me at my site &lt;a href="https://fernandobelotto.dev"&gt;https://fernandobelotto.dev&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>web</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
