<?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: Sachin</title>
    <description>The latest articles on DEV Community by Sachin (@sachinsainni).</description>
    <link>https://dev.to/sachinsainni</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%2F919004%2Ffcd456e7-d1e7-44fd-858c-ce7778a5a62f.jpg</url>
      <title>DEV Community: Sachin</title>
      <link>https://dev.to/sachinsainni</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sachinsainni"/>
    <language>en</language>
    <item>
      <title>React lifecycle methods</title>
      <dc:creator>Sachin</dc:creator>
      <pubDate>Fri, 02 Sep 2022 04:43:33 +0000</pubDate>
      <link>https://dev.to/sachinsainni/react-lifecycle-methods-4lon</link>
      <guid>https://dev.to/sachinsainni/react-lifecycle-methods-4lon</guid>
      <description>&lt;p&gt;Each component in react live a life - from birth to death.&lt;br&gt;
Main three stages of the lifecycle of a component are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;Mounting&lt;/code&gt; : Birth of the component&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Updating&lt;/code&gt; : Growth of the component&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Unmounting&lt;/code&gt; : Death of the component&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;we have many method in these lifecycle, but i'm talk about four mostly used methods. These methods can only used in class components.&lt;/p&gt;
&lt;h2&gt;
  
  
  render()
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {
    console.log('rendering')
    return (
        &amp;lt;div&amp;gt;
          &amp;lt;div&amp;gt;Parent Component&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;);

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

&lt;/div&gt;


&lt;p&gt;Other ways can be used, but this one must be declared. Our component is created at this stage and is added to the DOM. Rendering of the component to the DOM&lt;/p&gt;
&lt;h2&gt;
  
  
  componentDidMount()
&lt;/h2&gt;

&lt;p&gt;The componentDidMount() is called after the component is rendered.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
class... {
constructor(props) {
    super();

    this.state = {
        color : ''
    }
}

    componentDidMount() {
        this.setState({
            color: 'red'
        })
    }

    render() {
        &amp;lt;h1&amp;gt;I, a header element, is born! My color is {this.state.color}&amp;lt;/h1&amp;gt;
    }

}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  componentdidupdate()
&lt;/h2&gt;

&lt;p&gt;Once the update is complete, this method is called. It accepts as input the previous props, previous state. This is an ideal location for causing side effects and making network requests for the updated props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; componentDidUpdate(prevProps) {
        if(this.props.color !== prevProps.color) {
            console.log('component updating')
        }
    }

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  shouldComponentUpdate()
&lt;/h2&gt;

&lt;p&gt;When state or props change, you'll want a component to re-render. You do, however, have control over this behaviour.&lt;br&gt;
You can control whether the component is rerendered by returning a boolean (true or false)  within this lifecycle method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shouldComponentUpdate(){
    console.log('shouldComponentUpdate')
    return true
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Reference links
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://reactjs.org/blog/2018/03/29/react-v-16-3.html"&gt;react js&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.logrocket.com/react-lifecycle-methods-tutorial-examples/#shouldcomponentupdate"&gt;log Rocket&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Batch Update</title>
      <dc:creator>Sachin</dc:creator>
      <pubDate>Thu, 01 Sep 2022 06:33:03 +0000</pubDate>
      <link>https://dev.to/sachinsainni/batch-update-4kfg</link>
      <guid>https://dev.to/sachinsainni/batch-update-4kfg</guid>
      <description>&lt;p&gt;In react 17 version, when we set multiple state  in &lt;code&gt;this.state&lt;/code&gt; .&lt;br&gt;
&lt;code&gt;render&lt;/code&gt; can run for each state when update . so , its can effect performance while working.&lt;/p&gt;

&lt;p&gt;But after react 18 version, &lt;code&gt;render&lt;/code&gt; can run once after all state updated. &lt;br&gt;
below this code is example of render,&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";
import "./styles.css";

class batch extends React.Component {
  constructor() {
    super();
    this.state = {
      increment1: 0,
      increment2: 0
    };
  }

  bottonClick = () =&amp;gt; {
    this.setState({
      increment1: this.state.increment1 + 1,
      increment2: this.state.increment2 + 2
    });
    console.log("render");

  };

  render() {
    return (
      &amp;lt;&amp;gt;
        &amp;lt;h1&amp;gt; batch update&amp;lt;/h1&amp;gt;
        &amp;lt;div&amp;gt;increment1 : {this.state.increment1}&amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt; increment2 : {this.state.increment2}&amp;lt;/div&amp;gt;
        &amp;lt;button onClick={this.bottonClick}&amp;gt; increment&amp;lt;/button&amp;gt;
      &amp;lt;/&amp;gt;
    );
  }
}
export default batch;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advantage
&lt;/h2&gt;

&lt;p&gt;Separate changes to the state(&lt;code&gt;increment1&lt;/code&gt; and &lt;code&gt;increment2&lt;/code&gt;) within the event handler (&lt;code&gt;bottonClick&lt;/code&gt;) will result in single rendering&lt;/p&gt;

&lt;h2&gt;
  
  
  Disadvantage
&lt;/h2&gt;

&lt;p&gt;But it doesn't work in every situation.&lt;br&gt;
 when an event handler that uses asynchronous operation then separate state updates will not be batched.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgynjupgvvsgrvmyyyj1a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgynjupgvvsgrvmyyyj1a.png" alt="Image description" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Helping links : &lt;br&gt;
&lt;a href="https://www.markdownguide.org/basic-syntax/#links"&gt;medium&lt;/a&gt;&lt;br&gt;
and &lt;br&gt;
&lt;a href="https://blog.logrocket.com/simplifying-state-management-in-react-apps-with-batched-updates/"&gt;log rocket&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
