<?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: Ahmad Jamaly Rabib</title>
    <description>The latest articles on DEV Community by Ahmad Jamaly Rabib (@rabibsust).</description>
    <link>https://dev.to/rabibsust</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%2F483811%2Fbf98596f-1906-4577-86ba-d02e5325af73.png</url>
      <title>DEV Community: Ahmad Jamaly Rabib</title>
      <link>https://dev.to/rabibsust</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rabibsust"/>
    <language>en</language>
    <item>
      <title>Adding dd() in Wordpress development</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Mon, 27 May 2024 18:52:07 +0000</pubDate>
      <link>https://dev.to/rabibsust/adding-dd-in-wordpress-development-124h</link>
      <guid>https://dev.to/rabibsust/adding-dd-in-wordpress-development-124h</guid>
      <description>&lt;p&gt;Hey folks! Hope you are all doing well. Today I just want to share a common topic, which is related to debugging in Wordpress. I do like default Wordpress debugging. It's just fine. But you know what I like most is the Laravel's &lt;code&gt;dd()&lt;/code&gt; function. 😍&lt;/p&gt;

&lt;p&gt;So let's just check where and how the functions works.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;function_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'dd'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * @return never
     */&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;dd&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nv"&gt;$vars&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;in_array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;\PHP_SAPI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'cli'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'phpdbg'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nb"&gt;headers_sent&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nb"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'HTTP/1.1 500 Internal Server Error'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$vars&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;VarDumper&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We found the &lt;code&gt;dd()&lt;/code&gt; function inside the Symfony component var-dumper. Oh! I love Symfony. 😍&lt;br&gt;
So let's check the function now. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.php.net/manual/en/reserved.constants.php#constant.php-sapi" rel="noopener noreferrer"&gt;\PHP_SAPI&lt;/a&gt; is a predefined constant in PHP. It tells us how the PHP script is being executed. So in the condition we found that &lt;code&gt;!in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)&lt;/code&gt; let's breakdown this:&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;\PHP_SAPI&lt;/code&gt; is being checked against an array containing 'cli' and 'phpdbg'. Here 'cli' indicates that the script is being run from the command line interface and 'phpdbg' indicates that the script is being run using PHP's interactive debugger. So this condition will be true if the script is not running in CLI or PHP Debugger mode.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.php.net/manual/en/function.headers-sent.php" rel="noopener noreferrer"&gt;headers_sent&lt;/a&gt; is a function that checks if HTTP headers have already been sent to the client. So if the header is also not sent then the condition will be true.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If both conditions are true then the server will send a 500 Internal Server Error response&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In the next we found this foreach function which is the main part of our dd function.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$vars&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;VarDumper&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dump&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;So here &lt;code&gt;$vars&lt;/code&gt; are actually items of collection. Then inside the foreach it calls the static &lt;code&gt;dump()&lt;/code&gt; method inside the &lt;code&gt;[VarDumper](https://github.com/symfony/var-dumper/blob/7.0/VarDumper.php)&lt;/code&gt; class. &lt;/p&gt;

&lt;p&gt;After this &lt;code&gt;foreach&lt;/code&gt; block there is a &lt;code&gt;exit()&lt;/code&gt; function call to exit the operation. Pretty straight forward right!&lt;/p&gt;
&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;To use this function in my Wordpress plugin I needed to install this component.&lt;/p&gt;

&lt;p&gt;From my plugin directory I ran this command:&lt;br&gt;
&lt;code&gt;composer require --dev symfony/var-dumper&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It was installed. But to use this in my plugin I had to add &lt;code&gt;autoload&lt;/code&gt; inside the &lt;code&gt;init&lt;/code&gt; action hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nf"&gt;add_action&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'init'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;file_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;__DIR__&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/vendor/autoload.php'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="k"&gt;require_once&lt;/span&gt; &lt;span class="k"&gt;__DIR__&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s1"&gt;'/vendor/autoload.php'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;logCatchException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now I can call the &lt;code&gt;dd()&lt;/code&gt; or &lt;code&gt;dump()&lt;/code&gt; method inside any places from my plugin. And it perfectly shows the collection or array data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$postId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$opts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nv"&gt;$timestamp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;convertLocalTimeToUtc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$timestamp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;dd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$opts&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the data:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;array&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;▼&lt;/span&gt;
  &lt;span class="s2"&gt;"expireType"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"change-status"&lt;/span&gt;
  &lt;span class="s2"&gt;"newStatus"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"draft"&lt;/span&gt;
  &lt;span class="s2"&gt;"category"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"0"&lt;/span&gt;
  &lt;span class="s2"&gt;"categoryTaxonomy"&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;"category"&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all for today. Thank you all for reading it.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>laravel</category>
      <category>wordpress</category>
      <category>debug</category>
    </item>
    <item>
      <title>React Event Handling</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Mon, 19 Jun 2023 10:02:45 +0000</pubDate>
      <link>https://dev.to/rabibsust/react-event-handling-5963</link>
      <guid>https://dev.to/rabibsust/react-event-handling-5963</guid>
      <description>&lt;p&gt;Hello there! 👋 We are going to learn about React Event handling today.&lt;/p&gt;

&lt;p&gt;We know about the html event handling with Html and JavaScript. React does this event handling in a similar but kind of different way. We are going to deep dive on it today. 😁&lt;/p&gt;

&lt;h2&gt;
  
  
  Difference between HTML and React Events
&lt;/h2&gt;

&lt;p&gt;First let's see how we do it in HTML and JavaScript. We created a simple button with a click event and once the button is clicked it will show in the browser console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onclick="handleClick()"&amp;gt;Click&amp;lt;/button&amp;gt;

&amp;lt;script&amp;gt;
  function handleClick() {
    console.log("You have clicked the event");
  }
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zNefHxfI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjzsa8o1n81vssk955zw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zNefHxfI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjzsa8o1n81vssk955zw.gif" alt="Button click event" width="506" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will do the same thing for React now. We will use our previous code for state and will remove the props &lt;code&gt;locale&lt;/code&gt; we passed from the &lt;code&gt;App&lt;/code&gt; to &lt;code&gt;Clock&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  return (
    &amp;lt;Clock /&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will change the &lt;code&gt;Clock&lt;/code&gt; locale based on the click event now. 💪🤗&lt;/p&gt;

&lt;p&gt;Now let's create a button in React. 🎉🎊😁 &lt;br&gt;
Let's open our &lt;code&gt;Clock&lt;/code&gt; component and update in the &lt;code&gt;render&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  render() {
    const { date } = this.state;
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="heading"&amp;gt;
          &amp;lt;span&amp;gt;Hello &amp;lt;span&amp;gt;{this.props.children}&amp;lt;/span&amp;gt; { date.toLocaleTimeString('en-US')}&amp;lt;/span&amp;gt;
        &amp;lt;/h1&amp;gt;
        &amp;lt;button&amp;gt;Click here&amp;lt;/button&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QXW5Qkk_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ril763ho84s5ml7mkf5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QXW5Qkk_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ril763ho84s5ml7mkf5.gif" alt="React Button" width="404" height="228"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that we have added a button. But the button isn't functional yet. So there is no event handling there yet. Let's work on it and find out the differences with html button click events. 😊&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={}&amp;gt;Click here&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are adding an &lt;code&gt;onClick&lt;/code&gt; event on the React button. Now let's see our html button &lt;code&gt;onclick&lt;/code&gt; event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onclick="handleClick()"&amp;gt;Click&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we can clearly see that we are calling a string &lt;code&gt;"handleClick()"&lt;/code&gt; not the function &lt;code&gt;handleClick()&lt;/code&gt; on the html button click.&lt;/p&gt;

&lt;p&gt;So why are calling a string?&lt;/p&gt;

&lt;p&gt;This is because of not calling the &lt;code&gt;handleClick()&lt;/code&gt; function when the page is loaded. But the &lt;code&gt;handleClick()&lt;/code&gt; function will only be called when we click the button. Because that time the &lt;code&gt;onclick&lt;/code&gt; event will trigger the &lt;code&gt;handleClick()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Note that, in case of &lt;code&gt;React&lt;/code&gt; events we need to write &lt;strong&gt;camelcase&lt;/strong&gt; event. Like we are using &lt;code&gt;onClick&lt;/code&gt; for &lt;code&gt;React&lt;/code&gt; not &lt;code&gt;onclick&lt;/code&gt; like we used for html events.&lt;/p&gt;

&lt;p&gt;Also we are not calling string in React. We will call functions inside the curly braces &lt;code&gt;onClick={}&lt;/code&gt;. Now let's create a &lt;code&gt;handleClick&lt;/code&gt; function inside our &lt;code&gt;Clock&lt;/code&gt; class component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handleClick() {
  console.log("The button is clicked");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in our onClick event let's call the &lt;code&gt;handleClick&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button onClick={this.handleClick}&amp;gt;Click here&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are using &lt;code&gt;this.handleClick&lt;/code&gt; because it is inside the &lt;code&gt;Clock&lt;/code&gt; class component. As the &lt;code&gt;handleClick&lt;/code&gt; method is inside the class component we have to use &lt;code&gt;this&lt;/code&gt; to call that method.&lt;/p&gt;

&lt;p&gt;But we found another difference with the html event call with React. Look below codes&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML&lt;/strong&gt; =&amp;gt; &lt;code&gt;onclick="handleClick()"&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;React&lt;/strong&gt; =&amp;gt; &lt;code&gt;onClick={this.handleClick}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In React we are not calling the handleClick function like HTML.&lt;/p&gt;

&lt;p&gt;The reason is in HTML we are actually calling string but in React we are not calling the string.&lt;/p&gt;

&lt;p&gt;So, in the case of React we directly call the &lt;code&gt;handleClick&lt;/code&gt; function. It will actually call during page load. Not during the click event. 🤔&lt;/p&gt;

&lt;p&gt;Let's go to the browser now and check.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ETwlZFYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8bg25chdxbb74km372d.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ETwlZFYb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o8bg25chdxbb74km372d.gif" alt="Updated Click Event" width="530" height="382"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we can see that the &lt;code&gt;handleClick&lt;/code&gt; function is working and the log message is showing.&lt;/p&gt;
&lt;h2&gt;
  
  
  Handling Default behavior
&lt;/h2&gt;

&lt;p&gt;Let's think about a html link. It has a default behavior to go to the link provided. Let's see an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="test.html"&amp;gt;Click this&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see here that this is a link and when we click it will go to the &lt;code&gt;test.html&lt;/code&gt; url. We can change this default behavior using &lt;code&gt;return false;&lt;/code&gt; with the &lt;code&gt;onclick&lt;/code&gt; event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a href="test.html" onclick="console.log('You have clicked the event'); return false;"&amp;gt;Click here&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After updating we can see that this is no longer going to the url and we can see the log message &lt;code&gt;Link is clicked&lt;/code&gt; in the console.&lt;/p&gt;

&lt;p&gt;But in React we can not use &lt;code&gt;return false;&lt;/code&gt; to prevent the default behavior inside DOM.&lt;/p&gt;

&lt;p&gt;We can also prevent the default behavior using the &lt;code&gt;preventDefault&lt;/code&gt; function when the event will be passed as parameter in JavaScript. And we can use this technique in React events also to prevent default behavior.&lt;/p&gt;

&lt;p&gt;Let's go to our React project now. We can see that we have called the &lt;code&gt;this.handleClick&lt;/code&gt; function. This &lt;code&gt;handleClick&lt;/code&gt; function will actually get the event as a parameter. So we can write this function as &lt;code&gt;handleClick(e)&lt;/code&gt; here &lt;code&gt;e&lt;/code&gt; is the event. And then we will call the &lt;code&gt;e.preventDefault()&lt;/code&gt;function to prevent the default behavior of that event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  handleClick(e) {
    e.preventDefault();
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now even if we change the button to link or submit button the default behavior will be prevented and we will only see what is inside the handleClick function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a type="button" href="test.html" onClick={this.handleClick}&amp;gt;Click here&amp;lt;/a&amp;gt;

&amp;lt;button type="submit" onClick={this.handleClick}&amp;gt;Click here&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Update locale using React state and click event
&lt;/h2&gt;

&lt;p&gt;Now, let's update the functionality of the handleClick function so that it can change the locale of the Clock when we click the button.&lt;/p&gt;

&lt;p&gt;Let's first create a React state &lt;code&gt;locale&lt;/code&gt; and set default to &lt;code&gt;bn-BD&lt;/code&gt;. And update in our render function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;state = {date: new Date(), locale: 'bn-BD'}

render() {
  const { date, locale } = this.state;
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1 className="heading"&amp;gt;
        &amp;lt;span&amp;gt;Hello &amp;lt;span&amp;gt;{this.props.children}&amp;lt;/span&amp;gt; { date.toLocaleTimeString(locale)}&amp;lt;/span&amp;gt;
      &amp;lt;/h1&amp;gt;
      &amp;lt;button type="submit" onClick={this.handleClick}&amp;gt;Click here&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have changed to &lt;code&gt;date.toLocaleTimeString(locale)&lt;/code&gt; from &lt;code&gt;date.toLocaleTimeString('en-US')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And now let's update in our &lt;code&gt;handleClick&lt;/code&gt; function and check in the browser.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  handleClick(e) {
    e.preventDefault();
    this.setState({
      locale: 'en-US'
    });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XFg6SQ5f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfukjtlw3jcvdouwn4pp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XFg6SQ5f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hfukjtlw3jcvdouwn4pp.png" alt="handleClick setState" width="653" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Oh no! We got an error! 😭😱 It is saying &lt;code&gt;Uncaught TypeError: Cannot read properties of undefined (reading 'setState')&lt;/code&gt;&lt;br&gt;
But this didn't happen in our tick method. Why is it happening here? Let's find out.&lt;/p&gt;

&lt;p&gt;So here the problem is we are getting &lt;code&gt;this&lt;/code&gt; as &lt;code&gt;undefined&lt;/code&gt;. Why is that?&lt;/p&gt;

&lt;p&gt;Actually, here we are using the &lt;code&gt;Clock&lt;/code&gt; component which is a class component. We might know that class is a blueprint of the object. So here we have &lt;code&gt;this&lt;/code&gt; which is the object of the &lt;code&gt;Clock&lt;/code&gt; class. By using &lt;code&gt;this&lt;/code&gt; we can access all the properties of the &lt;code&gt;Clock&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;So here in the &lt;code&gt;render&lt;/code&gt; method when we use &lt;code&gt;this.handleClick&lt;/code&gt; it is calling the handleClick method without any issues. But inside the &lt;code&gt;handleClick&lt;/code&gt; method this isn't passed, just the reference is passed.&lt;/p&gt;

&lt;p&gt;So what happens here is, After the click event this isn't instantly calling the &lt;code&gt;handleClick&lt;/code&gt; function. Here it is just a callback function.&lt;/p&gt;

&lt;p&gt;When the callback function is called, it can't find &lt;code&gt;this&lt;/code&gt; that time. That's why we are getting &lt;code&gt;this&lt;/code&gt; as &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Fixing &lt;code&gt;this&lt;/code&gt; issue
&lt;/h2&gt;

&lt;p&gt;There are several ways to fix the &lt;code&gt;this&lt;/code&gt; issue in the class component. Let's try to fix it now. 🎉🥳&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Making handleClick method as arrow function.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's update our handleClick method to an arrow function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; handleClick = (e) =&amp;gt; {
    e.preventDefault();
    this.setState({
      locale: 'en-US'
    });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can do a &lt;code&gt;console.log(this)&lt;/code&gt; inside the handleClick method to check &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9Y3cKQI_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pn96ih8o69h3sika3w88.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9Y3cKQI_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pn96ih8o69h3sika3w88.png" alt="Arrow function" width="755" height="544"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see that there is no error and we have successfully updated the state of &lt;code&gt;locale&lt;/code&gt; to "en-US".&lt;/p&gt;

&lt;p&gt;The reason why the arrow function works but the traditional function not working is because the arrow function doesn't use additional scope for it. So it didn't change &lt;code&gt;this&lt;/code&gt; inside the function. Where our normal function has its own scope, so we couldn't access &lt;code&gt;this&lt;/code&gt; inside our normal function.&lt;/p&gt;

&lt;p&gt;This is one of the main reasons we use arrow functions in such cases.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Using &lt;code&gt;bind&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If we don't want to use the arrow function (although that is recommended) we can use &lt;code&gt;bind&lt;/code&gt; to use &lt;code&gt;this&lt;/code&gt; inside the &lt;code&gt;handleClick&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;We need to create a constructor function and update it there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constructor(props) {
    super(props);
    this.state = {date: new Date(), locale: 'bn-BD'}
    this.handleClick = this.handleClick.bind(this);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And keep the onClick event function same as before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick={this.handleClick}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So that's it!&lt;/p&gt;

&lt;p&gt;We are doing the same thing inside the constructor. The bind method binds our object inside the &lt;code&gt;handleClick&lt;/code&gt; method so we can access it inside the &lt;code&gt;handleClick&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;So in the constructor we are telling that the &lt;code&gt;this.handleClick&lt;/code&gt; is not the full function. &lt;code&gt;this.handleClick&lt;/code&gt; means &lt;code&gt;this.handleClick.bind(this)&lt;/code&gt; function where we are binding this and passing to &lt;code&gt;handleClick&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Passing Parameters to handleClick function&lt;/strong&gt;&lt;br&gt;
In this case we actually need to update where we are calling the function in the onClick event. We can actually do this. Instead of adding a constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick={this.handleClick.bind(this)}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also we can pass parameters from this onClick event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick={this.handleClick.bind(this, 'en-US')}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using this bind we can easily pass the parameters.&lt;br&gt;
So the handleClick method can be updated to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handleClick(locale) {
    this.setState({
      locale
    });
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy right! 😁🤗&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x78iG5_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5c9uwqtw3e6dqh4lypeh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x78iG5_6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5c9uwqtw3e6dqh4lypeh.jpg" alt="React Event Handling" width="528" height="473"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also there is another way if we want to use arrow function on the onClick event.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onClick= {() =&amp;gt; this.handleClick('en-US')}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PPzSwohl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1erxwaywvg7vne9pu1sc.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PPzSwohl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1erxwaywvg7vne9pu1sc.jpg" alt="Arrow function on onclick event" width="500" height="546"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This one is more easier! yay! 😁😄 &lt;/p&gt;

&lt;p&gt;That's all about event handling in React today. Thank you for reading. 🙏😊 &lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/rabibsust/react/tree/master/handling_events"&gt;github repository&lt;/a&gt; for todays code.&lt;/p&gt;

&lt;p&gt;I might come out tomorrow with some new topics. Till then goodbye! 👋🙏🙌&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Learning React State and LifeCycle</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Sat, 10 Jun 2023 19:33:01 +0000</pubDate>
      <link>https://dev.to/rabibsust/learning-react-state-and-lifecycle-1c0b</link>
      <guid>https://dev.to/rabibsust/learning-react-state-and-lifecycle-1c0b</guid>
      <description>&lt;p&gt;Hello again! 👋😁&lt;/p&gt;

&lt;p&gt;Last time we created React Components both class and functional but these aren't fully functional yet. Today we will look at how React components work independently using State.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_WNOTEbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rj4mtgh1mpe9dus5bl7i.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_WNOTEbf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rj4mtgh1mpe9dus5bl7i.jpg" alt="React state" width="700" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the &lt;code&gt;Component&lt;/code&gt; section we found that &lt;code&gt;Class Component&lt;/code&gt; is stateful. But we don't know what is state yet 😅. Let's find out about it. 🥳💪&lt;/p&gt;

&lt;p&gt;So, let's first prepare our project to work with React State. Let's start with our Component files&lt;br&gt;
&lt;a href="https://github.com/rabibsust/react/tree/2a84d19fe1d07850561bf54bc26ff038bbe3c713/react_component"&gt;react_component&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Till now we haven't worked with data changes in React. React only changes two times.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When the data/state changes.&lt;/li&gt;
&lt;li&gt;When there is change in the props from outside the Component.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We know that if we pass a parameter to the React component and we change the value of the parameter , in this case React changes.&lt;/p&gt;

&lt;p&gt;This is called change by props. This can be controlled from outside of the Component. Like we did pass the &lt;code&gt;locale&lt;/code&gt; props data to the Component from outside here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function App() {
  return (
    &amp;lt;Clock locale="en-US"&amp;gt;&amp;lt;/Clock&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;In React, State is a memory of a component. It is like a database of the Component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;React works with virtual DOM first to compare the previous state with current state. If there is any change then it paints it in the browser DOM.&lt;/p&gt;

&lt;p&gt;But State change only happens inside the React component and the data change should happen by "React way".&lt;/p&gt;

&lt;p&gt;So, what is a "React way" data change?&lt;/p&gt;

&lt;p&gt;As we have extended the &lt;code&gt;React.Component&lt;/code&gt; class we can use the methods of the &lt;code&gt;Component&lt;/code&gt; class of React, like we are using the &lt;code&gt;render&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Let's create the &lt;code&gt;constructor&lt;/code&gt; method for the &lt;code&gt;Clock&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;constructor(props) {
  super(props);
  this.state = { date: new Date() }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the constructor function we will have the props as parameter. After that we are using &lt;code&gt;super&lt;/code&gt; which calls the base class &lt;code&gt;constructor&lt;/code&gt; method properties. &lt;/p&gt;

&lt;p&gt;In this way we can use the &lt;code&gt;Component&lt;/code&gt; class &lt;code&gt;constructor&lt;/code&gt; method's all properties staying inside the &lt;code&gt;Clock&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Then we created the property &lt;code&gt;this.state&lt;/code&gt; which is an object. In this &lt;code&gt;state&lt;/code&gt; object we can define any types of properties.&lt;/p&gt;

&lt;p&gt;Here, we have defined the &lt;code&gt;date&lt;/code&gt; property inside the &lt;code&gt;this.state&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Now, we can use &lt;code&gt;this.state.date&lt;/code&gt; instead of the &lt;code&gt;new Date()&lt;/code&gt; inside the &lt;code&gt;render&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="heading"&amp;gt;
          &amp;lt;span&amp;gt;Hello &amp;lt;span&amp;gt;{this.props.children}&amp;lt;/span&amp;gt; { this.state.date.toLocaleTimeString(this.props.locale) }&amp;lt;/span&amp;gt;
        &amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By this way we prepared the methods so that it will update in case of any &lt;code&gt;state&lt;/code&gt; change in the "React way".&lt;/p&gt;

&lt;p&gt;We can also skip creating a constructor for class if we only need to initialize the &lt;code&gt;state&lt;/code&gt;. We can use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Public_class_fields"&gt;public class field syntax&lt;/a&gt; to define the state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;state = {date: new Date()}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have to use the &lt;code&gt;setState&lt;/code&gt; method to change the data using the &lt;code&gt;React way&lt;/code&gt;. But to do that, we actually need to know when to change the state before changing the state. 😏🤔🥴&lt;/p&gt;

&lt;p&gt;This leads us to understanding the Component lifecycle. 🤗 Let's go through some of the methods of React lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. componentDidMount&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;This method is called immediately after a component is mounted in the real DOM. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This method isn't mounted when the rendering is done in React virtual DOM, it actually mounted when everything is compared, calculated and then when the final result is painted in the browser DOM.&lt;/p&gt;

&lt;p&gt;But if we implement this method we also need to find out and update other lifecycle methods to avoid bugs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. componentDidUpdate&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If we define this method, React will call it immediately after the component is re-rendered with updated props or state. This method isn't called for initial render.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We use it to manipulate the DOM after the update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. componentWillUnmount&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If we define this method. React called immediately before the component is destroyed.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It performs any necessary cleanup in this method, such as canceled network requests, or cleaning up any DOM elements created in &lt;code&gt;componentDidMount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's update our &lt;code&gt;Clock&lt;/code&gt; class using some &lt;code&gt;React LifeCycle&lt;/code&gt; methods. Let's first add the &lt;code&gt;componentDidMount&lt;/code&gt; method to make the clock functional. We will use the &lt;code&gt;setInterval&lt;/code&gt; method to make the change per second by calling the &lt;code&gt;setState&lt;/code&gt; method. Inside the setState we will pass the updated object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {
  setInterval(() =&amp;gt; {
    this.setState({
      date: new Date()
    });
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So in this way we have made our clock to self update.&lt;br&gt;
&lt;a href="https://gyazo.com/2401dd0feacfac7c225e4ea688cd7de3"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xF1Y8K2Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://i.gyazo.com/2401dd0feacfac7c225e4ea688cd7de3.gif" alt="Image from Gyazo" width="462" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But this timer is resource hungry right. And this will continue to run even if we go to another page or route.&lt;/p&gt;

&lt;p&gt;So, to make it stop when we move to another route we will use another lifecycle method &lt;code&gt;componentWillUnmount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But let's first make a separate function &lt;code&gt;tick&lt;/code&gt; for the &lt;code&gt;this.setState&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tick() {
  this.setState({
    date: new Date()
  });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now let's replace the codes inside &lt;code&gt;componentDidMount&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {
  setInterval(() =&amp;gt; this.tick());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's clear the timer. But to do this we will add another property &lt;code&gt;this.clockTimer&lt;/code&gt; in which we will define the current date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;componentDidMount() {
  this.clockTimer = setInterval(() =&amp;gt; this.tick());
}

componentWillUnmount() {
  clearInterval(this.clockTimer);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yes! We are done with the proper functional &lt;code&gt;Clock&lt;/code&gt; component. Now we can use this Clock anywhere! 🙌🙏🤗&lt;/p&gt;

&lt;p&gt;I still have a lot of things to learn on &lt;code&gt;State&lt;/code&gt;. I am still going through the &lt;a href="https://react.dev/learn/state-a-components-memory"&gt;React documentation for state.&lt;/a&gt;. Hope I will get a better understanding after going through it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/rabibsust/react/tree/master/react_state"&gt;Here is the code for today's task.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you for reading. If there is anything missing please comment. I will try to learn and update. Thank you! Bye! 👋🙏&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Creating React Components and using Props</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Mon, 05 Jun 2023 06:09:35 +0000</pubDate>
      <link>https://dev.to/rabibsust/creating-react-components-and-using-props-2nec</link>
      <guid>https://dev.to/rabibsust/creating-react-components-and-using-props-2nec</guid>
      <description>&lt;p&gt;Hi there! 👋 Today I am going to create Component using the created element which I created on &lt;a href="https://dev.to/rabibsust/learning-react-markup-with-jsx-and-rendering-elements-d00"&gt;last time&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Components are functional elements of React. In a component we can add different functionalities to prepare before the render.&lt;/p&gt;

&lt;p&gt;We can re-use the components to create beautiful UI in a short time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why should we use React Components?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have used &lt;code&gt;React elements&lt;/code&gt; before right? It is a valid JS object and we could use dynamic variables inside the &lt;code&gt;elements&lt;/code&gt; as we saw in &lt;a href="https://dev.to/rabibsust/learning-react-markup-with-jsx-and-rendering-elements-d00"&gt;previous post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So why do we need React Components?&lt;/p&gt;

&lt;p&gt;Let's change our previous element to a component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const root = ReactDOM.createRoot(document.getElementById('root'));

function Clock() {
    return (
        &amp;lt;h1 className="heading" tabIndex={index}&amp;gt;
            &amp;lt;span className="text"&amp;gt;Hello {new Date().toLocaleTimeString()}&amp;lt;/span&amp;gt;
            &amp;lt;img src="" /&amp;gt;
        &amp;lt;/h1&amp;gt;
    );
}

root.render(Clock());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We created the &lt;code&gt;Clock&lt;/code&gt; component in the above example. This component returns a React element.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can render the component using html syntax.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root.render(&amp;lt;Clock /&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Using React components we can pass the variable as a parameter using the attribute of that component. These parameters are passed as objects.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Clock({locale}) {
    return (
        &amp;lt;h1 className="heading" tabIndex={index}&amp;gt;
            &amp;lt;span className="text"&amp;gt;Hello {new Date().toLocaleTimeString(locale)}&amp;lt;/span&amp;gt;
            &amp;lt;img src="" /&amp;gt;
        &amp;lt;/h1&amp;gt;
    );
}
root.render(&amp;lt;Clock locale="en-US"/&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We can make other calculations inside the function and then return the results in Component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Amazing right! This is like dynamic elements. As React elements can not be dynamic. React introduces the Components. For these dynamic functionalities components are super useful.&lt;/p&gt;

&lt;p&gt;Till now we have discussed the functional components. Now, let's learn about the Class component.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But the question is why do we need to use the Class component?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Class component is a stateful component. It means this can be returned to it again and again.&lt;/p&gt;

&lt;p&gt;While using a functional component we couldn't find a way to make the component to change by itself, which can return the updated value each time the clock changes other than manually calling it again and again. This can be fixed using a stateful approach.&lt;/p&gt;

&lt;p&gt;Now, let's update our functional component to a class component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Clock {
  print() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="heading"&amp;gt;
          &amp;lt;span&amp;gt;Hello {new Date().toLocaleTimeString()}&amp;lt;/span&amp;gt;
        &amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we created a Clock class and in that class we created a print method, which returns the element.&lt;/p&gt;

&lt;p&gt;But we know that we can not call class. Class is a skeleton of the Object.&lt;/p&gt;

&lt;p&gt;So we need to create an object first. Then call the print method from that object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ClockComponent = new Clock();
root.render(ClockComponent.print());
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this will not work if we set &lt;code&gt;root.render(&amp;lt;ClockComponent /&amp;gt;)&lt;/code&gt; like we used for &lt;code&gt;Clock&lt;/code&gt; function before. The problem is React isn't understanding our &lt;code&gt;Clock&lt;/code&gt; class as a Component.&lt;/p&gt;

&lt;p&gt;So, what should be the workaround for this?&lt;br&gt;
By making the class as a React component.&lt;/p&gt;

&lt;p&gt;We can extend the Clock class by React Component class to make it a React component.&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 Clock extends React.Component {
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="heading"&amp;gt;
          &amp;lt;span&amp;gt;Hello {new Date().toLocaleTimeString()}&amp;lt;/span&amp;gt;
        &amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have access to all properties of the &lt;code&gt;Component&lt;/code&gt; class. Also it automatically creates an object inside the class.&lt;/p&gt;

&lt;p&gt;We can use the &lt;code&gt;render&lt;/code&gt; method now which is defined in the React &lt;code&gt;Component&lt;/code&gt; class. Also we don't need to define any object to use the &lt;code&gt;Clock&lt;/code&gt; class component.&lt;/p&gt;

&lt;p&gt;We can just simply render the Clock class component. 🤗&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root.render(&amp;lt;Clock /&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also pass parameters as attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root.render(&amp;lt;Clock locale="en-US"/&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now Clock class can access the attribute using its &lt;code&gt;props&lt;/code&gt; property. The React &lt;code&gt;Component&lt;/code&gt; class already has the &lt;code&gt;props&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;As our Clock class extends it, it also gets access to the &lt;code&gt;props&lt;/code&gt; property. 😁&lt;/p&gt;

&lt;p&gt;Now, let's access the locale attribute using props property.&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 Clock extends React.Component {
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="heading"&amp;gt;
          &amp;lt;span&amp;gt;Hello {new Date().toLocaleTimeString(this.props.locale)}&amp;lt;/span&amp;gt;
        &amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;this.props&lt;/code&gt; is the object. Here &lt;code&gt;this&lt;/code&gt; is the object of the React Component. If we want to get the property inside the class we can call &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;From the above example we get that we can pass props outside and update the component.&lt;/p&gt;

&lt;p&gt;We can separate these class components in different files and then we can export the class component.&lt;/p&gt;

&lt;p&gt;Let's create a file &lt;code&gt;Clock.jsx&lt;/code&gt;. Cut all the Clock component codes to &lt;code&gt;Clock.jsx&lt;/code&gt; then export the Clock component.&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 Clock extends React.Component {
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="heading"&amp;gt;
          &amp;lt;span&amp;gt;Hello {new Date().toLocaleTimeString(this.props.locale)}&amp;lt;/span&amp;gt;
        &amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}

export default Clock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then in the main.jsx file let's import the &lt;code&gt;Clock&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import ReactDOM from 'react-dom/client'
import Clock from './Clock';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(&amp;lt;Clock locale="en-US"/&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it! We are done with creating components and exporting them. We can import and use that component in different locations.&lt;/p&gt;

&lt;p&gt;From these &lt;code&gt;Component&lt;/code&gt; examples we found that components are reusable. We can pass attributes to it and it will return the results.&lt;/p&gt;

&lt;p&gt;This is similar to the html tags and attributes. Pretty easy right! 😍🥳&lt;/p&gt;

&lt;p&gt;We can also add components inside another component.&lt;/p&gt;

&lt;p&gt;Let's update the component tag with a separate closing tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Clock locale="en-US"&amp;gt;&amp;lt;/Clock&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can put anything inside the Clock tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;Clock locale="en-US"&amp;gt;Test children component&amp;lt;/Clock&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To show the children properties we have to use the props again. We can find the children properties in &lt;code&gt;this.props.children&lt;/code&gt;. Let's add this in our &lt;code&gt;Clock&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Clock extends React.Component {
  render() {
    return (
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="heading"&amp;gt;
          &amp;lt;span&amp;gt;Hello {this.props.children} {new Date().toLocaleTimeString(this.props.locale)}&amp;lt;/span&amp;gt;
        &amp;lt;/h1&amp;gt;
      &amp;lt;/div&amp;gt;
    );
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can see the children property in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kulGU8fg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le2xvp6cotl6aha01opy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kulGU8fg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/le2xvp6cotl6aha01opy.png" alt="Children property" width="655" height="174"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also put components inside the component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;root.render(&amp;lt;Clock locale="en-US"&amp;gt;&amp;lt;Clock locale="en-US"/&amp;gt;&amp;lt;Clock locale="en-US"/&amp;gt;&amp;lt;/Clock&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result will be&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ZwmCImZE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dh8m889is4mzc5rmm55t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ZwmCImZE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dh8m889is4mzc5rmm55t.png" alt="Component inside component" width="655" height="454"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We have to keep in mind that we should never change props inside the component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we change the attribute or child component of the React Component the render function is always called and the elements are reloaded. So if we change the prop that time it will again call the render function. As a result this will create an infinite loop and our application will crash. 😩&lt;/p&gt;

&lt;p&gt;Now as we understand Components we can create reusable components and use them multiple times in our application to make a beautiful app.&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/rabibsust/react/tree/master/react_component"&gt;github repo&lt;/a&gt; of today's exercise.&lt;/p&gt;

&lt;p&gt;Thank you for reading. 🙏 I will try to write about the state and lifecycle in the next post. Till then bye 👋🙌&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Learning React: Markup with JSX and Rendering Elements</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Thu, 01 Jun 2023 06:35:53 +0000</pubDate>
      <link>https://dev.to/rabibsust/learning-react-markup-with-jsx-and-rendering-elements-d00</link>
      <guid>https://dev.to/rabibsust/learning-react-markup-with-jsx-and-rendering-elements-d00</guid>
      <description>&lt;p&gt;Hello there! 👋&lt;br&gt;
Today I am writing about JSX and React elements.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feq8h5nag4jeu51otnq85.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feq8h5nag4jeu51otnq85.jpg" alt="JSX and React elements"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://dev.to/rabibsust/learning-react-day-2-li5"&gt;Learning React : JSX post&lt;/a&gt; I discussed my understanding of JSX. But today I will dive deeper with React Documentation on JSX and will check how we create React elements, use it with JSX and render using React DOM. 💪&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JSX is a syntax extension for JavaScript which lets us write HTML like markup inside a JS file. It is just syntactic sugar that makes our development easier.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's first prepare our project to work with it. In the &lt;a href="https://dev.to/rabibsust/installing-react-with-vite-801"&gt;Installing React with Vite post&lt;/a&gt; we learned about installing react to our local machine. I have removed all the folders and files from src folder except &lt;code&gt;App.jsx&lt;/code&gt; and &lt;code&gt;main.jsx&lt;/code&gt;. Here is the &lt;a href="https://github.com/rabibsust/react/tree/master/myreactapp" rel="noopener noreferrer"&gt;github link&lt;/a&gt; of the skeleton project.&lt;/p&gt;

&lt;p&gt;We know that React doesn't work with DOM and DOM elements. React has it's virtual DOM and it creates React elements.&lt;/p&gt;

&lt;p&gt;Now let's create a simple React element in the &lt;code&gt;main.jsx&lt;/code&gt; file.&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'
const element = React.createElement('h1', null, 'Hello, world!');
console.log(element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;main.jsx&lt;/code&gt; file is our main file in the React project.&lt;br&gt;
Let's see in the browser the &lt;code&gt;console.log&lt;/code&gt; result of the element.&lt;/p&gt;

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

&lt;p&gt;We can see the React element which is also a JS object unlike the HTML elements.&lt;/p&gt;

&lt;p&gt;We can also create the same element using JSX in React. This time while working on the React project we don't have to include babel manually because babel is already included in the React project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello World!&amp;lt;/h1&amp;gt;;
console.log(element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will provide the same result as the &lt;code&gt;React.createElement&lt;/code&gt;.&lt;br&gt;
We can show dynamic values to JSX using curly braces.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello {name}!&amp;lt;/h1&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also use any valid JS expressions in JSX&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello {firstname + lastname}!&amp;lt;/h1&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JSX itself is a valid JS expression. We can return JSX expression in a function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getGreeting(user) {
    if (user) {
        return (
            &amp;lt;h1 className="heading"&amp;gt;
                &amp;lt;span&amp;gt;Hello, {user}!&amp;lt;/span&amp;gt;
            &amp;lt;/h1&amp;gt;
        )
    } else {
        return &amp;lt;h1&amp;gt;Hello, user!&amp;lt;/h1&amp;gt;
    }
}


console.log(getGreeting('rabib'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also call the function inside the element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getGreeting(user) {
    if (user) {
        return (
            &amp;lt;span&amp;gt;{user}!&amp;lt;/span&amp;gt;
        )
    } else {
        return &amp;lt;span&amp;gt;user!&amp;lt;/span&amp;gt;;
    }
}

const element = &amp;lt;h1&amp;gt;Hello {getGreeting('rabib')}&amp;lt;/h1&amp;gt;
console.log(element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is called &lt;code&gt;Interpolation&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's get some idea about &lt;strong&gt;Attributes in JSX&lt;/strong&gt;&lt;br&gt;
JSX attributes are not the same as HTML attributes.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;JSX turns into JavaScript and attributes written in JSX become keys of JavaScript objects.&lt;br&gt;
JavaScript has limitations on variable names. For example, their names can't contain &lt;code&gt;dashes&lt;/code&gt; or be reserved words like &lt;code&gt;class&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is why in JSX attributes are mostly written in camelCase. For example we use &lt;code&gt;className&lt;/code&gt; instead of &lt;code&gt;class&lt;/code&gt; in JSX. Let's check the below element.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const element = (
    &amp;lt;h1 className="heading"&amp;gt;
        &amp;lt;span className="text"&amp;gt;Hello {new Date().toLocaleTimeString()}&amp;lt;/span&amp;gt;
        &amp;lt;img src="" className="img-fluid"/&amp;gt;
    &amp;lt;/h1&amp;gt;
);

console.log(element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;To create multiline element we use first bracket &lt;code&gt;()&lt;/code&gt; to wrap the element contents. Otherwise if there there is a semicolon &lt;code&gt;;&lt;/code&gt; inside the element before ending, then JS expression will alredy set before ending the element. Which could create issues.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here we are using &lt;code&gt;className&lt;/code&gt; attribute which is added to React element object as &lt;code&gt;props&lt;/code&gt; property element. Let's see in the console.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw5oobf2ez1xwzgp623w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqw5oobf2ez1xwzgp623w.png" alt="Added className Attribute"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also we can see from the console that by creating child elements inside the &lt;code&gt;h1&lt;/code&gt; tag we are creating child objects inside the React element object. &lt;/p&gt;

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

&lt;p&gt;There are two child object elements for &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;image&lt;/code&gt; and these are added as array elements inside the &lt;code&gt;props&lt;/code&gt; of the parent object element &lt;code&gt;h1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Preventing malicious attack in React&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To prevent any malicious attack React automatically escape the values inside the &lt;code&gt;{}&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So from now we might get the idea what React Element is. React element is the smallest building block of React.&lt;/p&gt;

&lt;p&gt;Now let's print our React element in the browser. We will use &lt;code&gt;ReactDOM&lt;/code&gt; library to render the React element in the browser.&lt;/p&gt;

&lt;p&gt;We can see in our React project there is a &lt;code&gt;index.html&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8" /&amp;gt;
    &amp;lt;link rel="icon" type="image/svg+xml" href="/vite.svg" /&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&amp;gt;
    &amp;lt;title&amp;gt;Vite + React&amp;lt;/title&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;script type="module" src="/src/main.jsx"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;We will render inside the &lt;code&gt;root&lt;/code&gt; div.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let index = 0;
const element = (
        &amp;lt;h1 className="heading" tabIndex={index}&amp;gt;
            &amp;lt;span className="text"&amp;gt;Hello {new Date().toLocaleTimeString()}&amp;lt;/span&amp;gt;
            &amp;lt;img src="" /&amp;gt;
        &amp;lt;/h1&amp;gt;
    );
ReactDOM.createRoot(document.getElementById('root')).render(element);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see in the browser that the created element is shown.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuoe0bpg79oalnbmsw1ak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuoe0bpg79oalnbmsw1ak.png" alt="React Element Render"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's update the time in each interval.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let index = 0;

const root = ReactDOM.createRoot(document.getElementById('root'));
setInterval(() =&amp;gt; {
    const element = (
        &amp;lt;h1 className="heading" tabIndex={index}&amp;gt;
            &amp;lt;span className="text"&amp;gt;Hello {new Date().toLocaleTimeString()}&amp;lt;/span&amp;gt;
            &amp;lt;img src="" /&amp;gt;
        &amp;lt;/h1&amp;gt;
    );

    root.render(element);
}, 1000);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv0zjosqlgbyzfq9aaei.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzv0zjosqlgbyzfq9aaei.gif" alt="Updating Element"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can see from the above example that, only &lt;code&gt;time&lt;/code&gt; is updating in the DOM. But inside the React virtual DOM the element is created each time. And react only updating the DOM after comparing the states inside the virtual DOM. Amazing! isn't it?&lt;/p&gt;

&lt;p&gt;Normally, we will never create the element multiple times but we will change the data and based on the component we will update the UI based on the date change.&lt;/p&gt;

&lt;p&gt;I will learn about how to create and use the component and share in my next post. 😊&lt;/p&gt;

&lt;p&gt;I found it really helpful going through &lt;a href="https://react.dev/learn/writing-markup-with-jsx" rel="noopener noreferrer"&gt;Writing Markup with JSX&lt;/a&gt; post from React official Documentation while learning JSX.&lt;/p&gt;

&lt;p&gt;I have started posting about my learning for the last few days. Here are the previous posts about React.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/rabibsust/installing-react-with-vite-801"&gt;Installing React with Vite&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/rabibsust/learning-react-about-virtual-dom-3obe"&gt;Learning React: Virtual DOM&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/rabibsust/learning-react-day-2-li5"&gt;Learning React : about JSX&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/rabibsust/at-last-i-am-learning-react-2bon"&gt;At last I am learning React: Day 1&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thank you for reading. Bye 👋 &lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Installing React with Vite</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Tue, 30 May 2023 05:45:04 +0000</pubDate>
      <link>https://dev.to/rabibsust/installing-react-with-vite-801</link>
      <guid>https://dev.to/rabibsust/installing-react-with-vite-801</guid>
      <description>&lt;p&gt;From the &lt;a href="https://react.dev/learn/start-a-new-react-project"&gt;React Official Documentation&lt;/a&gt; I came to know that we have to use React frameworks like Next.js, Remix, Gatsby etc.&lt;/p&gt;

&lt;p&gt;Also the &lt;code&gt;create-react-app&lt;/code&gt; is no longer valid if we want to create a React app. 😞&lt;/p&gt;

&lt;p&gt;From React's official documentation&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You can definitely use React without a framework—that’s how you’d &lt;a href="https://react.dev/learn/add-react-to-an-existing-project#using-react-for-a-part-of-your-existing-page"&gt;use React for a part of your page&lt;/a&gt;. However, if you’re building a new app or a site fully with React, we recommend using a framework.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But as a beginner I want to explore only the React library. That's why I need to install React in my local. To acheive this we can use bundler tools like &lt;a href="https://vitejs.dev"&gt;Vite&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So let's start with the installation. 🤗😁&lt;br&gt;
Let's first install &lt;a href="https://nodejs.org/en"&gt;Node.js&lt;/a&gt;. Then Let's scaffold our first Vite project for React.&lt;/p&gt;

&lt;p&gt;With NPM:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create vite@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Yarn:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn create vite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After enter there will be prompt to proceed. Next steps will be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Enter Project name: … &lt;code&gt;myreactapp&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Select a framework: › &lt;code&gt;React&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Select a variant: › &lt;code&gt;JavaScript + SWC&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There will be several options to select which Framework/library/Language to select. We can select by moving the arrow keys.&lt;/p&gt;

&lt;p&gt;After this the our react application will be created. In this case my React app name is &lt;code&gt;myreactapp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now after all this We just have to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  cd myreactapp
  npm install
  npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our react app will be ready and running.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YRARa85d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhokuj5s64lfljb455yd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YRARa85d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhokuj5s64lfljb455yd.png" alt="Running React App" width="786" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's open the URL &lt;code&gt;http://localhost:5174/&lt;/code&gt; in browser and see our created React app is running.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4nOcobM8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smludl6ciwaprhk3abe2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4nOcobM8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/smludl6ciwaprhk3abe2.png" alt="React App in browser" width="600" height="685"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is really great how Vite has made the steps really easy to install and scaffold any library. I have became a fan of Vite. I will try to go through the documentation of Vite later. 🙌&lt;/p&gt;

&lt;p&gt;I will dive into development Application from now on. 🤞&lt;br&gt;
Thank you for reading 🙏 will keep posting on my learning here. Till then bye 👋😄&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Learning React: Virtual DOM</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Mon, 29 May 2023 02:07:54 +0000</pubDate>
      <link>https://dev.to/rabibsust/learning-react-about-virtual-dom-3obe</link>
      <guid>https://dev.to/rabibsust/learning-react-about-virtual-dom-3obe</guid>
      <description>&lt;p&gt;Hello there! 👋 I have recently learned about React's Virtual DOM. Instead of manipulating DOM directly like jQuery, React creates its own separate DOM. Let's see how it works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--77xF4QG6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn4d9wt1ks2nfd0zqi7b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--77xF4QG6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jn4d9wt1ks2nfd0zqi7b.gif" alt="Search for Virtual DOM" width="260" height="144"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is said that DOM read/write is slow, that's why the DOM element add/remove is slow. But the reality is DOM is really fast and the DOM element update/delete is similar to an object property adding or removing in JS. But why it seems that the DOM elements are not updating quickly is that the Render process in the browser after the DOM change.&lt;/p&gt;

&lt;p&gt;Let's see the browser workflow first.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RVLv5Gv5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpre5oa1kh2l5wxeh2so.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RVLv5Gv5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tpre5oa1kh2l5wxeh2so.png" alt="DOM browser workflow" width="800" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the top image we are seeing that the browser follows several steps to show the contents.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At first the browser receives the HTML file and then parses it using its render engine and creates a DOM tree. In the DOM tree the created HTML elements stay as nodes.&lt;/li&gt;
&lt;li&gt;Like the HTML document, browser parse the CSS file contents using CSS parser. After parsing it is created similarly like the DOM for CSS. It is called CSSOM. Where the styles also stay as nodes.&lt;/li&gt;
&lt;li&gt;These Parsed CSS and HTML together makes Render Tree. This Render Tree goes between a phase called layout. In this phase the Render tree coordinates are calculated and the elements are attached. So in this phase the render tree has all the information like the HTML, Style and the coordinates where it will paint the elements.&lt;/li&gt;
&lt;li&gt;After that the painting phase is done based on the Render Tree nodes information.&lt;/li&gt;
&lt;li&gt;And in the last stage the page is displayed to the end users.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So when we update anything in the DOM, the browser re-run the process again. All the HTML elements creation and positioning, CSS style information re-calculation is done again and after all this the page is repainted and displayed.&lt;/p&gt;

&lt;p&gt;Although the modern browsers do these things in a very fast way, for better UX currently most of the web applications are made as SPA (Single Page Application). &lt;/p&gt;

&lt;p&gt;While using SPA we have to handle lot's of DOM operations. Usually we can see that the web applications make 100's of DOM manipulation for single actions. 🥹😭😩&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0BwIdALU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2f99dkr0s57qzwsni4zd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0BwIdALU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2f99dkr0s57qzwsni4zd.jpg" alt="DOM manipulation for dev" width="500" height="572"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When we will make these large amounts of DOM manipulation it is likely to be affected after some time in our application. But we have to do it anyway. 😞 So to minimize the effect in the performance we can try these two things.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Doing Batch Update 🤞&lt;/li&gt;
&lt;li&gt;Try to do as less DOM operation as possible 😅&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's see an example for the batch update here. Let's first create a HTML file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;DOM&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;

    &amp;lt;/div&amp;gt;
    &amp;lt;script src="./dom.js"&amp;gt;&amp;lt;/script&amp;gt;

&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create the &lt;code&gt;dom.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let increment = 0;

let container = document.querySelector('.container');

// slow
while (increment &amp;lt; 50000) {
    increment++;
    container.innerHTML += ' ' + increment;
}

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

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;dom.js&lt;/code&gt; file we can see that we are updating the &lt;code&gt;container&lt;/code&gt; html for each increment in the while loop. So here the DOM operation is happening 50000 times. Which is a lot! And for this the page loads after a long time of waiting.&lt;/p&gt;

&lt;p&gt;Now let's update the dom.js file. Let's create a blank array and push the numbers to the array for each increment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let array = [];
let increment = 0;

let container = document.querySelector('.container');
// fast
while (increment &amp;lt; 50000) {
    array.push(++increment);
}

container.innerHTML = array.join(' ');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So after all the elements are pushed to the array we can just put the array elements inside the &lt;code&gt;container&lt;/code&gt; html using &lt;code&gt;array.join(' ')&lt;/code&gt;. So this only takes one DOM operation and the page loads instantly.&lt;/p&gt;

&lt;p&gt;From this above example we can clearly see that due to too much DOM operation our page loads slowly. But with just simple modification we can achieve faster page load while getting the same output. This way is called &lt;code&gt;batch update&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can do &lt;code&gt;batch update&lt;/code&gt; without using virtual DOM or React. But if we want to update DOM only for the element which is updated we need to use Virtual DOM.&lt;/p&gt;

&lt;p&gt;To achieve this DOM should have two separate snapshots before updating the element and after updating the element. So that it can compare between these snapshots to check where to update.&lt;/p&gt;

&lt;p&gt;But using DOM to do this task is difficult and it creates problems also it would take a lots of resource which could bring more problems. So React created Virtual DOM to achieve this feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gllSg0Ys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2umvvyi2olpub2cvrex0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gllSg0Ys--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2umvvyi2olpub2cvrex0.jpg" alt="Virtual DOM" width="750" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;So what is Virtual DOM?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is a playground DOM for React, where React makes the changes.&lt;br&gt;
It works similar like the browser DOM but there are no repainting steps here.&lt;br&gt;
Only the JS elements are created/updated here. All the updates are done inside the virtual DOM, so there is no pressure on DOM to add/update the elements. 🤗&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does Virtual DOM work?&lt;/strong&gt;&lt;br&gt;
We can think of a Virtual DOM as a tree. Where the nodes are different components. If there is any change in the state of a node then it creates a new tree. In the new tree all the changed components and new components are newly created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xEeG2y-k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bnukoscjylezsvwlfcc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xEeG2y-k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8bnukoscjylezsvwlfcc.png" alt="DOM compare" width="800" height="285"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, in this case React can compare the previous state and new state in the Virtual DOM to check which element in which position is changed. There is an efficient algorithm to compare the previous and later states. This algorithm is called the &lt;code&gt;diffing&lt;/code&gt; or &lt;code&gt;reconciliation&lt;/code&gt; algorithm.&lt;/p&gt;

&lt;p&gt;Now let's see how normal DOM and virtual DOM works in case of updating elements with two examples using Reactive way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Working with normal DOM&lt;/strong&gt;&lt;br&gt;
let's first create the HTML file.&lt;br&gt;
&lt;code&gt;index.html&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;DOM Tutorial&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="./style.css"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div class="container"&amp;gt;
        &amp;lt;ul id="fruits"&amp;gt;&amp;lt;/ul&amp;gt;
        &amp;lt;br&amp;gt;
        &amp;lt;p&amp;gt;
            &amp;lt;input type="text" id="input"&amp;gt;
        &amp;lt;/p&amp;gt;
        &amp;lt;button id="button" onclick="addItem()"&amp;gt;Add Item&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;script src="./dom.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the HTML file we can see that there is a &lt;code&gt;ul&lt;/code&gt; list element area for elements.&lt;br&gt;
Also there is a text input area and button to add items.&lt;br&gt;
Now let's update in the dom.js file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Let's first declare the variables

const display = document.getElementById('fruits');
const button = document.querySelector('#button');

let fruits = ['mango', 'guava', 'apple', 'orange'];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the above code we can see that we have initialized the variable &lt;code&gt;display&lt;/code&gt; for &lt;code&gt;ul&lt;/code&gt; element and &lt;code&gt;button&lt;/code&gt;. Also created an array for the initial lists of &lt;code&gt;fruits&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's create a function &lt;code&gt;init&lt;/code&gt; and call to append and sort the &lt;code&gt;fruits&lt;/code&gt; elements inside the &lt;code&gt;display&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const init = function () {
    display.innerHTML = '';
    let liElements = '';
    fruits.sort().forEach(fruit =&amp;gt; {
        let item = document.createElement('li');
        item.textContent = fruit;
        liElements += item.outerHTML;
    });
    display.innerHTML = liElements;
}

// Using appendChild
const init = function () {
    display.innerHTML = '';
    fruits.sort().forEach(fruit =&amp;gt; {
        let item = document.createElement('li');
        item.textContent = fruit;
        display.appendChild(item);
    });
}

init();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;During button click we will call the addItem function. So now let's add this function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const addItem = function () {
    fruits.myPush(document.getElementById('input').value);
}

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

&lt;/div&gt;



&lt;p&gt;In the above code we are seeing that the new input value will be passed as a parameter in the &lt;code&gt;myPush&lt;/code&gt; function.&lt;br&gt;
We need to create the &lt;code&gt;myPush&lt;/code&gt; function as a custom array prototype function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.prototype.myPush = function (...a) {
    this.push(a[0]);
    init();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this Array.prototype.myPush function we are pushing the elements to the array and calling the &lt;code&gt;init&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So here the array is &lt;code&gt;fruits&lt;/code&gt; and if we call the function using &lt;code&gt;fruits.myPush('banana')&lt;/code&gt; then &lt;code&gt;banana&lt;/code&gt; will be passed as parameter and it will be pushed to the &lt;code&gt;fruits&lt;/code&gt; array. 🥳&lt;/p&gt;

&lt;p&gt;Now let's see how the things gets re-rendered when we add any element in the DOM.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mn5CYcKH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft774zqwalts2lccfo4q.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mn5CYcKH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft774zqwalts2lccfo4q.jpg" alt="Checking DOM Render" width="717" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's first open dev tools in Chrome. From the &lt;code&gt;options-&amp;gt;More tools&lt;/code&gt; select &lt;code&gt;Rendering&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vkaddczu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kcl4fh4reukexh9jsx0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vkaddczu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4kcl4fh4reukexh9jsx0.png" alt="Select Rendering" width="432" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then from the Rendering tab check &lt;code&gt;Paint Flashing&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1kNutt-6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p77aj8gcwsdxapqbofee.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1kNutt-6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p77aj8gcwsdxapqbofee.png" alt="Paint flashing" width="772" height="196"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then open the index.html file in the browser or use the live server plugin to open the page. Now let's see how the rendering is done.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8w6uiZk1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lc1k74w6d630dqjmbr39.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8w6uiZk1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lc1k74w6d630dqjmbr39.gif" alt="Adding element on top area" width="408" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NZZC7U72--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vco37j9dot1ac77ru6m1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NZZC7U72--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vco37j9dot1ac77ru6m1.gif" alt="Adding element in bottom area" width="338" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the rendering example we can see that all the elements are re-rendering and repainting when any element is added in any position. 😕&lt;/p&gt;

&lt;p&gt;Now let's create the same project using &lt;code&gt;React&lt;/code&gt; to take advantage of Virtual DOM. 😎&lt;br&gt;
First let's create the &lt;code&gt;index.html&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;DOM Tutorial&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="./style.css"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;

    &amp;lt;!-- Load React. --&amp;gt;
    &amp;lt;script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;!-- Load our React component. --&amp;gt;
    &amp;lt;script src="https://unpkg.com/@babel/standalone/babel.min.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script type="text/babel" src="./fruits.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create and modify the &lt;code&gt;fruits.js&lt;/code&gt; file. We will see the state changes by tracking using the &lt;code&gt;useState&lt;/code&gt; hook.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The React &lt;code&gt;useState&lt;/code&gt; Hook allows us to track state in a function component.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const domContainer = document.querySelector('#root');

const Fruits = () =&amp;gt; {
    const [fruit, setFruit] = React.useState('');
    const [fruits, setFruits] = React.useState(['mango', 'guava', 'apple', 'orange']);
};

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

&lt;/div&gt;



&lt;p&gt;Above we can see that we created a component &lt;code&gt;Fruits&lt;/code&gt; where we are destructuring the returned values from &lt;code&gt;useState&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We have two &lt;code&gt;const&lt;/code&gt; variables &lt;code&gt;[fruit, setFruit]&lt;/code&gt; and &lt;code&gt;[fruits, setFruits]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here &lt;code&gt;fruit&lt;/code&gt; and &lt;code&gt;fruits&lt;/code&gt; are the current state and &lt;code&gt;setFruit&lt;/code&gt; and &lt;code&gt;setFruits&lt;/code&gt; are the functions that are used to update the states.&lt;/p&gt;

&lt;p&gt;The initial state of &lt;code&gt;fruit&lt;/code&gt; is an empty string, &lt;code&gt;React.useState('')&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And the initial state of &lt;code&gt;fruits&lt;/code&gt; is an array of fruits, &lt;code&gt;React.useState(['mango', 'guava', 'apple', 'orange'])&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's update the states in the &lt;code&gt;Fruits&lt;/code&gt; component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const domContainer = document.querySelector('#root');

const Fruits = () =&amp;gt; {
    const [fruit, setFruit] = React.useState('');
    const [fruits, setFruits] = React.useState(['mango', 'guava', 'apple', 'orange']);

    return (
        &amp;lt;div className="container"&amp;gt;
            &amp;lt;ul id="fruits"&amp;gt;
                {fruits.sort().map((fruit, index) =&amp;gt; (
                    &amp;lt;li key={index}&amp;gt;{fruit}&amp;lt;/li&amp;gt;
                ))}
            &amp;lt;/ul&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;p&amp;gt;&amp;lt;input type="text" value={fruit} onChange={(e) =&amp;gt; setFruit(e.target.value)} /&amp;gt;&amp;lt;/p&amp;gt;
            &amp;lt;button onClick={() =&amp;gt; setFruits([...fruits, fruit])}&amp;gt;Add Item&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    );
};

ReactDOM.render(&amp;lt;Fruits /&amp;gt;, domContainer)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the return area of the component we can see that we are updating the states during key input and button click. Now let's run the project in the browser and see how the render is done while using React state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MTnHHgdh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obnod0p6zyhwspb9q28z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MTnHHgdh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/obnod0p6zyhwspb9q28z.gif" alt="Last Element" width="390" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2DD6PqGe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxl4kjn6v4pxkbuzihlr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2DD6PqGe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sxl4kjn6v4pxkbuzihlr.gif" alt="Top element" width="358" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;From the top result examples we can see that React efficiently updates the state and updates in that specific area in the DOM.&lt;/p&gt;

&lt;p&gt;So from the examples of React Virtual DOM and browser DOM we can say that both are fast but Virtual DOM works in a more efficient way that's why it looks like it is working faster. 🙌😍&lt;/p&gt;

&lt;p&gt;This is all from my today's learning. I will keep posting everyday from learning React. Thank you for reading. Bye 👋 &lt;/p&gt;

&lt;p&gt;I have started posting about my learning for the last few days. Here are the previous posts about React. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/rabibsust/learning-react-day-2-li5"&gt;Learning React : about JSX&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://dev.to/rabibsust/at-last-i-am-learning-react-2bon"&gt;At last I am learning React: Day 1&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Learning React : JSX</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Thu, 25 May 2023 10:33:01 +0000</pubDate>
      <link>https://dev.to/rabibsust/learning-react-day-2-li5</link>
      <guid>https://dev.to/rabibsust/learning-react-day-2-li5</guid>
      <description>&lt;p&gt;I am continuing to learn React. It is my second day learning React. If you are only reading this article then you can also visit &lt;a href="https://dev.to/rabibsust/at-last-i-am-learning-react-2bon"&gt;my experience of learning React on Day 1&lt;/a&gt;. 😄&lt;/p&gt;

&lt;p&gt;Let's hope I will get the knowledge to work with React very soon and just not learn then forget. 😁😅&lt;/p&gt;

&lt;p&gt;So, yesterday we stopped after creating a React element using the &lt;code&gt;React.createElement()&lt;/code&gt; method. 😎&lt;br&gt;
Today we will learn about &lt;code&gt;JSX-&amp;gt; JavaScript XML&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;JSX&lt;/code&gt; looks almost like HTML and it is really easy to use while using React.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r-BZl9QX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yihjivczdd1h9ha3rqio.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r-BZl9QX--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yihjivczdd1h9ha3rqio.jpg" alt="JSX and HTML similarity" width="556" height="500"&gt;&lt;/a&gt;&lt;br&gt;
So I am creating a similar React component for the increment button that we did in vanilla JS yesterday. Let’s update in the &lt;code&gt;increment.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const domContainer = document.querySelector('#root');
const reactIncrement = (
    &amp;lt;div&amp;gt;
        &amp;lt;h1 id="display"&amp;gt;0&amp;lt;/h1&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;button id="button"&amp;gt;Increment +&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
);

ReactDOM.render(reactIncrement, domContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here as we can see I have simply written JSX to render the element in the root div. Let me run the html now. But I got an error in the console. 😞&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Uncaught SyntaxError: Unexpected token '&amp;lt;' (at increment.js)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reason for this error is Javascript doesn't recognize the JSX tags we have used. Even though I renamed the &lt;code&gt;increment.js&lt;/code&gt; file to &lt;code&gt;increment.jsx (As I am now using JSX :wink:)&lt;/code&gt; it didn't work. 😟&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--k_eHpGqZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qntlo809dapcacyxykm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k_eHpGqZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7qntlo809dapcacyxykm.jpg" alt="Using JSX" width="500" height="696"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get rid of this situation at last we found a way. We need to use a &lt;strong&gt;transpiler&lt;/strong&gt;.&lt;br&gt;
&lt;strong&gt;Transpilers&lt;/strong&gt; are tools that convert one programming language into another language. 😲&lt;br&gt;
In this case of React we will use Babel as a Transpiler. Babel will convert our JSX codes to vanilla JS.&lt;br&gt;
There are also other use cases of Babel to help us coding without worring about the compatibility of different browsers. 😍&lt;/p&gt;

&lt;p&gt;Here I checked my &lt;a href="https://babeljs.io/repl#?browsers=defaults%2C%20not%20ie%2011%2C%20not%20ie_mob%2011&amp;amp;build=&amp;amp;builtIns=false&amp;amp;corejs=3.21&amp;amp;spec=false&amp;amp;loose=false&amp;amp;code_lz=DwEwlgbgfAUABAxSHABYEY5hAXgETgDOADgDYCGAnunlAAzAD0Gsybcokr7PwARgFcALkID2AOyy48gkRJpQAkuIDGAJwCmAWw3ihcANRNZY8dx4pG4aPHZNrUIA&amp;amp;debug=false&amp;amp;forceAllTransforms=false&amp;amp;modules=false&amp;amp;shippedProposals=false&amp;amp;circleciRepo=&amp;amp;evaluate=false&amp;amp;fileSize=false&amp;amp;timeTravel=false&amp;amp;sourceType=module&amp;amp;lineWrap=true&amp;amp;presets=env%2Creact%2Cstage-2&amp;amp;prettier=false&amp;amp;targets=&amp;amp;version=7.21.8&amp;amp;externalPlugins=&amp;amp;assumptions=%7B%7D"&gt;JSX code in babel.io&lt;/a&gt; website how it converted our JSX code to JS.&lt;/p&gt;

&lt;p&gt;So let's go to &lt;a href="https://babeljs.io/setup#installation"&gt;babel website -&amp;gt; setup -&amp;gt; In the browser link&lt;/a&gt; and copy the babel script to our html file. Also let's add the increment.js script type to &lt;code&gt;"text/babel"&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="https://unpkg.com/@babel/standalone/babel.min.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;script type="text/babel" src="./increment.jsx"&amp;gt;&amp;lt;/script&amp;gt;

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

&lt;/div&gt;



&lt;p&gt;Now when we run the html in the browser we can see that the counter button and display is appearing and there is no console error.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rOwMNXgF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rt4fuyyt01n0ib5ekwa3.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rOwMNXgF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rt4fuyyt01n0ib5ekwa3.jpg" alt="Issue Fixed" width="656" height="380"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Let's add the increment functionality in the JS file.
Let's first create an Increment method.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Increment = () =&amp;gt; {};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now let's copy the JSX codes we wrote before in the &lt;code&gt;increment.js&lt;/code&gt; file inside the function, so that this Increment method returns that JSX code.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Increment = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
        &amp;lt;h1 id="display"&amp;gt;0&amp;lt;/h1&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;button id="button"&amp;gt;Increment +&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}
// Render Increment
ReactDOM.render(&amp;lt;Increment /&amp;gt;, domContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are still viewing the same button and display with no functionality update till now. 😒&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hzCvDKW9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdic84hmwl7oove8vod8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hzCvDKW9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fdic84hmwl7oove8vod8.png" alt="Display Button" width="386" height="208"&gt;&lt;/a&gt;&lt;br&gt;
Now let's copy the increment functionality from the vanilla JS code that we wrote yesterday in our &lt;code&gt;increment.js&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const domContainer = document.querySelector('#root');
const Increment = () =&amp;gt; {
  return (
    &amp;lt;div&amp;gt;
        &amp;lt;h1 id="display"&amp;gt;0&amp;lt;/h1&amp;gt;
        &amp;lt;div&amp;gt;
            &amp;lt;button id="button"&amp;gt;Increment +&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

let number = 0;

let button = document.querySelector('#button');
let display = document.querySelector('#display');

button.addEventListener('click', () =&amp;gt; {
    number++;
    display.textContent = number;
});

ReactDOM.render(&amp;lt;Increment /&amp;gt;, domContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Did we get what we wanted? No! 😞&lt;br&gt;
I got a console error after copying this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qQRxuFoL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvw5b246tl9enyy6ldg9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qQRxuFoL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gvw5b246tl9enyy6ldg9.png" alt="Error in Console" width="512" height="120"&gt;&lt;/a&gt;&lt;br&gt;
Let's fix this issue now.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_suT-3_q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/236ck0q2vnac8mojxmdi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_suT-3_q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/236ck0q2vnac8mojxmdi.png" alt="Button null" width="423" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So from the console error we found that the button is null. That means the button is not defined. So it seems we are calling the button event even before the button is initialized and rendered by calling the &lt;code&gt;Increment&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;So now let's render the elements first before calling the button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(&amp;lt;Increment /&amp;gt;, domContainer);

let number = 0;

let button = document.querySelector('#button');
let display = document.querySelector('#display');

button.addEventListener('click', () =&amp;gt; {
    number++;
    display.textContent = number;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It works now!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_0mr9S9G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4qicjsn77x1wa1f087b.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_0mr9S9G--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d4qicjsn77x1wa1f087b.gif" alt="Incrementing number" width="454" height="234"&gt;&lt;/a&gt;&lt;br&gt;
But does it solve the problem? Can we use multiple components this way? Let's say we want two counters. Will it work? This is the reason we are working with React right?&lt;br&gt;
The answer is no. 😭 We still haven't achieved what we want.&lt;/p&gt;

&lt;p&gt;I tried adding the Increment component multiple times in the render.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render(&amp;lt;&amp;gt;&amp;lt;Increment /&amp;gt;&amp;lt;Increment /&amp;gt;&amp;lt;Increment /&amp;gt;&amp;lt;/&amp;gt;, domContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The functionality should work independently, but it didn't.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nnTWqJwQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6t4vnkb5qlr08jhcfla.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nnTWqJwQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6t4vnkb5qlr08jhcfla.gif" alt="All component not working" width="800" height="188"&gt;&lt;/a&gt;&lt;br&gt;
In the above image we saw that only the first component is working but the others aren't.&lt;/p&gt;

&lt;p&gt;The main reason for the above issue is that we aren't doing things in React way. Now let's do our coding in the React way. 💪&lt;/p&gt;

&lt;p&gt;React tells us that we shouldn't manipulate DOM. But in our code we are still selecting DOM components, creating &lt;code&gt;counter&lt;/code&gt; variable then updating &lt;code&gt;counter&lt;/code&gt; using DOM manipulation. Which is actually prohibited in React.&lt;/p&gt;

&lt;p&gt;So let's first remove our button and display counter functionality code.&lt;/p&gt;

&lt;p&gt;To achieve this functionality using React we need to use &lt;strong&gt;State&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;State is a built-in React object which is used to contain data or information about the component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So in our counter functionality what is the state? This is our &lt;code&gt;number&lt;/code&gt; variable. Because only this data is changing and based on this our components' content is changing.&lt;/p&gt;

&lt;p&gt;Now let's use this knowledge to create a State inside the Increment function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Increment = () =&amp;gt; {
    const counter = React.useState(0);
    ...
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what did I do there? I am using &lt;code&gt;useState&lt;/code&gt; and setting an initial value which is &lt;code&gt;0&lt;/code&gt; in this case. Let's do a &lt;code&gt;console.log(counter)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XmWUN8AI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zt8dn7gyd8jfo52iu09s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XmWUN8AI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zt8dn7gyd8jfo52iu09s.png" alt="React State" width="477" height="213"&gt;&lt;/a&gt;&lt;br&gt;
So, it returns an array which has two elements. &lt;br&gt;
The first one is the value we set for the state and the second one is a function.&lt;/p&gt;

&lt;p&gt;This function actually helps us by returning an updated value based on the parameter we will pass to the function.&lt;/p&gt;

&lt;p&gt;So, this function updates the state and triggers a re-render of our component.&lt;br&gt;
This can be used like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [state, setState] = React.useState(initialValue);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's update our &lt;code&gt;Increment&lt;/code&gt; method now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [counter, setCounter] = React.useState(0);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we created the &lt;code&gt;setCounter&lt;/code&gt; function which we will call during the button click event. So our updated button will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button id="button" onClick={ () =&amp;gt; setCounter(counter + 1) }&amp;gt;Increment +&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am using JSX. The &lt;code&gt;onClick&lt;/code&gt; function isn't the same as normal HTML. Couldn't find the issue the first time. Also I am using curly braces &lt;code&gt;{}&lt;/code&gt; not quotation &lt;code&gt;"&lt;/code&gt; like we used to use in HTML. Found these differences in JSX for now. I need to get used to it 😆&lt;/p&gt;

&lt;p&gt;To show that counter in the display we will update our display area where we will bind the &lt;code&gt;counter&lt;/code&gt; variable inside &lt;code&gt;h1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1 id="display"&amp;gt;{ counter }&amp;lt;/h1&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So after the updates I have now successfully created a React component, which is reusable and can be used several times. And most interestingly I can call this multiple times and can have multiple independent components without writing more codes. 😅&lt;/p&gt;

&lt;p&gt;This is my JS code now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Increment = () =&amp;gt; {
    const [counter, setCounter] = React.useState(0);
    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h1 id="display"&amp;gt;{ counter }&amp;lt;/h1&amp;gt;
            &amp;lt;div&amp;gt;
                &amp;lt;button id="button" onClick={ () =&amp;gt; setCounter(counter + 1) }&amp;gt;Increment +&amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
    );
};

ReactDOM.render(&amp;lt;div className="container"&amp;gt;&amp;lt;Increment /&amp;gt;&amp;lt;Increment /&amp;gt;&amp;lt;Increment /&amp;gt;&amp;lt;/div&amp;gt;, domContainer);

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

&lt;/div&gt;



&lt;p&gt;I have called the Increment method 3 times and it is now working fine!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cVBl_u8F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqfniyb7qi5rm3hqloet.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cVBl_u8F--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqfniyb7qi5rm3hqloet.jpg" alt="Success!" width="620" height="413"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/rabibsust/react/tree/master/Day1"&gt;github link&lt;/a&gt; for the two projects I have worked till now.&lt;/p&gt;

&lt;p&gt;This is all for today. I will try to learn about React DOM tomorrow. Hopefully write something from that. Till then bye! 👋&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>At last I am learning React: Day 1</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Wed, 24 May 2023 10:21:44 +0000</pubDate>
      <link>https://dev.to/rabibsust/at-last-i-am-learning-react-2bon</link>
      <guid>https://dev.to/rabibsust/at-last-i-am-learning-react-2bon</guid>
      <description>&lt;p&gt;It is strange that after all these years I still haven't learned React. 😞 😓&lt;/p&gt;

&lt;p&gt;I was still using vanilla JS and jQuery in my projects. Until now I am determined to work on some projects where I need to use React. At last! 😅&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--l8RljQuR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxrbfq3ojx9fc8n8eqqz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--l8RljQuR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxrbfq3ojx9fc8n8eqqz.jpg" alt="Learning React at Last!" width="500" height="625"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have started learning React a few times before but then couldn't keep up. 😒 😭 But this time I will try to be consistent. I am learning from this &lt;a href="https://www.youtube.com/watch?v=5Xy-t8k_M4A&amp;amp;list=PLHiZ4m8vCp9M6HVQv7a36cp8LKzyHIePr&amp;amp;pp=iAQB"&gt;React JS Tutorial Bangla Series for Beginners | Think in a React way&lt;/a&gt; playlist in youtube. This is in Bangla tutorial from &lt;a href="https://www.youtube.com/@LearnwithSumit"&gt;&lt;br&gt;
Learn with Sumit - LWS - Bangladesh&lt;/a&gt;. I love the contents of this tutorial. This is better than paid courses. And whatever I will learn each day I will post here.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1EJkXaIh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6xfnglbuflokpi9g8o7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1EJkXaIh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6xfnglbuflokpi9g8o7.jpg" alt="Fingers Crossed" width="500" height="520"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  So, why React? 😒
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xNpTeYBz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwh34ux17opofp6z48nu.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xNpTeYBz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cwh34ux17opofp6z48nu.jpg" alt="React everywhere!" width="735" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;React is not a language like Javascript. It is a &lt;strong&gt;library&lt;/strong&gt; made with &lt;strong&gt;Javascript&lt;/strong&gt;. We create interactive UI using &lt;strong&gt;React&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;But we can create UI with Javascript or jQuery right? Like I have been doing this for years. Then why do we need React? 😒&lt;/p&gt;

&lt;p&gt;Actually React is introduced to make scalable larger applications with less hassle. Let's look at this JS code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let number1 = 0;
let number2 = 0;


let button1 = document.querySelector('#button1');
let display1 = document.querySelector('#display1');
let button2 = document.querySelector('#button2');
let display2 = document.querySelector('#display2');


button1.addEventListener('click', () =&amp;gt; {
    number1++;
    display1.textContent = number1;
});


button2.addEventListener('click', () =&amp;gt; {
    number2++;
    display2.textContent = number2;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this JS code I created two buttons and two display areas to implement counters. If I click the buttons the display counters will increment independently. So now imagine I want to create hundreds of elements and events using Vanilla JS. How can we manage this? It is possible but I am afraid how much time and effort it would take to make that possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--W2QEbOnk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pw4t0cksdtcrkg87rm2.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--W2QEbOnk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2pw4t0cksdtcrkg87rm2.jpg" alt="Exhausted" width="621" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So to make it possible and to make the lives of the developers easier in 2011 React was first introduced by Jordan Walke. It was named FaxJS first. It was named ReactJS in 2013 and made open source. 😌&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Lets see how React works to make and manage components.&lt;/strong&gt;&lt;br&gt;
Let's first create the html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta http-equiv="X-UA-Compatible" content="IE=edge"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;React Counter&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="./style.css"&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create a div inside body with the id &lt;strong&gt;root&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our React application will be generated inside this &lt;code&gt;root&lt;/code&gt; div. This means whatever components we create will be inside this &lt;code&gt;root&lt;/code&gt; div.&lt;/p&gt;

&lt;p&gt;Also let's not forget to include the react library scripts inside the body.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;!-- Load React. --&amp;gt;
    &amp;lt;script src="https://unpkg.com/react@17/umd/react.development.js" crossorigin&amp;gt;&amp;lt;/script&amp;gt;

    &amp;lt;script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js" crossorigin&amp;gt;&amp;lt;/script&amp;gt;


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

&lt;/div&gt;



&lt;p&gt;Here we can see that we have included two scripts. One is &lt;code&gt;react&lt;/code&gt; and another is &lt;code&gt;react-dom&lt;/code&gt;.&lt;br&gt;
Here the &lt;code&gt;react&lt;/code&gt; library generates &lt;code&gt;HTML&lt;/code&gt; inside the &lt;code&gt;root&lt;/code&gt; div.&lt;br&gt;
And the &lt;code&gt;react-dom&lt;/code&gt; library renders the HTML elements which are generated by the &lt;code&gt;react&lt;/code&gt; library inside the &lt;code&gt;root&lt;/code&gt; div in the HTML file.&lt;br&gt;
Now let's add our &lt;code&gt;script&lt;/code&gt; in the &lt;code&gt;html&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;script src="./increment.js"&amp;gt;&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this we have already set up our &lt;code&gt;html&lt;/code&gt; file. From now on we will not add anything extra in the &lt;code&gt;html&lt;/code&gt;. We will just write the JS code in our &lt;code&gt;increment.js&lt;/code&gt; file. 😌&lt;/p&gt;

&lt;p&gt;Let's now open 📂 the &lt;code&gt;increment.js&lt;/code&gt; file and check if we are loading the &lt;code&gt;react&lt;/code&gt; and &lt;code&gt;react-dom&lt;/code&gt; libraries in this file by using &lt;code&gt;console.log()&lt;/code&gt;. ⤵️&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(React);
console.log(ReactDOM);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Yj-vuQpH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpyfhpdwsz63sx05glyb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Yj-vuQpH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpyfhpdwsz63sx05glyb.png" alt="React and ReactDOM loaded" width="800" height="122"&gt;&lt;/a&gt;&lt;br&gt;
As you can see from the above image, the &lt;code&gt;React&lt;/code&gt; and &lt;code&gt;ReactDOM&lt;/code&gt; library are both loaded. If you are seeing the same in the &lt;code&gt;console&lt;/code&gt; area then we have successfully loaded the react libraries.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RsmqCoMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5aja5sq8k1dis8pq8m23.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RsmqCoMe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5aja5sq8k1dis8pq8m23.jpg" alt="Mission accomplished" width="500" height="500"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's jump into creating components in React. Let's update in the &lt;code&gt;increment.js&lt;/code&gt; file. First let's get the &lt;code&gt;root&lt;/code&gt; div element using &lt;code&gt;querySelector&lt;/code&gt; in the variable &lt;code&gt;domContainer&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const domContainer = document.querySelector('#root');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we will call the render method in ReactDOM. This render method receives two parameters. In the first parameter we pass what to print and in the second parameter we pass where to print. Let's look at the below code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ReactDOM.render('Hello world!', domContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this way text will be shown in the root div. We created an HTML element this way. Now let's add React elements to the root div using React.&lt;/p&gt;

&lt;p&gt;React uses React.createElement method to create elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let newElement = React.createElement('div', null, 'Hello World!');
// Render inside root div
ReactDOM.render(newElement, domContainer);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the createElement method we can see that it has 3 parameters.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We can define which type of element we want to create. We added &lt;code&gt;div&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Data to pass inside that element. We passed &lt;code&gt;null&lt;/code&gt;;&lt;/li&gt;
&lt;li&gt;Content inside that element. We passed &lt;code&gt;Hello World!&lt;/code&gt;
In the content parameter we can also pass another React element. Check the below code:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Passing React content in the 3rd parameter
let pElement = React.createElement('div', null, React.createElement('p', null, 'Hello World!'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can also pass multiple React Elements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Passing Multiple React contents
let multiElement = React.createElement('div', null, [
    React.createElement('p', null, 'Hello World!'),
    React.createElement('p', null, 'New World!'),
]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's good right? But yeah maybe creating normal html was a bit easy for us.&lt;br&gt;
But let's see how browser works in any webpage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--trUZOJKB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ler7r9s70u4qfbwgz5ns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--trUZOJKB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ler7r9s70u4qfbwgz5ns.png" alt="Browser creating DOM" width="500" height="518"&gt;&lt;/a&gt;&lt;br&gt;
We can see that Browser actually parses the Web page then by createElement creates DOM. 😲&lt;/p&gt;

&lt;p&gt;In the same way React also creates elements and creates a separate DOM for React. Which we call Virtual DOM. 🔥&lt;/p&gt;

&lt;p&gt;But to make markup like &lt;code&gt;HTML&lt;/code&gt; while using &lt;code&gt;React&lt;/code&gt; it gives us a markup syntax. Which is called &lt;strong&gt;JSX&lt;/strong&gt;.&lt;br&gt;
Here JSX means JavaScript XML. JSX allows us to write HTML in React. JSX makes it easier to write and add HTML in React. 😍&lt;/p&gt;

&lt;p&gt;I will try to learn about JSX tomorrow. See ya! 👋 Thanks for reading. I will try to keep my pace from now on. 🙇&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
    </item>
    <item>
      <title>Managing different scripts in separate pages while using barba.js</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Wed, 10 May 2023 10:58:48 +0000</pubDate>
      <link>https://dev.to/rabibsust/managing-different-scripts-in-separate-pages-while-using-barbajs-e4i</link>
      <guid>https://dev.to/rabibsust/managing-different-scripts-in-separate-pages-while-using-barbajs-e4i</guid>
      <description>&lt;p&gt;Those who don't know about &lt;a href="https://barba.js.org/docs/getstarted/intro/"&gt;barba.js&lt;/a&gt;, &lt;strong&gt;Barba.js&lt;/strong&gt; is a small (7kb minified and compressed) and easy-to-use library that helps you create fluid and smooth transitions between your website’s pages. It makes your website run like a SPA (Single Page Application) and help reduce the delay between your pages, minimize browser HTTP requests and enhance your user’s web experience.&lt;/p&gt;

&lt;p&gt;I am using barba.js for this website &lt;a href="https://www.visit-suruga.com/"&gt;Visit Suruga&lt;/a&gt; and it is performing really well. You can visit the website and check how the smooth transitions are working.&lt;/p&gt;

&lt;p&gt;But everything isn't perfect. Same goes for barba.js.&lt;/p&gt;

&lt;p&gt;Lets see how barba.js works:&lt;/p&gt;

&lt;p&gt;Barba.js follows a specific DOM structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body data-barba="wrapper"&amp;gt;
  &amp;lt;!-- put here content that will not change
  between your pages, like &amp;lt;header&amp;gt; or &amp;lt;nav&amp;gt; --&amp;gt;

  &amp;lt;main data-barba="container" data-barba-namespace="home"&amp;gt;
    &amp;lt;!-- put here the content you wish to change
    between your pages, like your main content &amp;lt;h1&amp;gt; or &amp;lt;p&amp;gt; --&amp;gt;
  &amp;lt;/main&amp;gt;

  &amp;lt;!-- put here content that will not change
  between your pages, like &amp;lt;footer&amp;gt; --&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Barba.js will only update the contents which are inside the &lt;code&gt;data-barba="container"&lt;/code&gt;. Other parts of this DOM will remain the same what we had during the page load.&lt;/p&gt;

&lt;p&gt;For this feature we have to load all the scripts, styles, libraries during the page load. Even if that page isn't using those style or script, which eventually make the initial page loading a bit slower.&lt;/p&gt;

&lt;p&gt;Normally almost all pages has the same style and scripts needed. But there are few pages which requires extra libraries or styles. If we don't load those libraries the functionality will not work on those pages.&lt;/p&gt;

&lt;p&gt;To fix this issue we took the advantage of the &lt;code&gt;data-barba-namespace&lt;/code&gt; attribute. Normally it is used for Transition rules and Views.&lt;/p&gt;

&lt;p&gt;So we set the page name &lt;code&gt;spot&lt;/code&gt; in the &lt;code&gt;data-barba-namespace&lt;/code&gt; value in that page. And we also did added the library scripts in the bottom of the page (If we load the page first time). And after that based on the namespace value we added the same script inside barba hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;barba.hooks.enter((data) =&amp;gt; {
   if (data.next.namespace === 'spot') {
     // Initialize library
   } 
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By doing this even if we go to the &lt;code&gt;spot&lt;/code&gt; page after transition from another page the library will be initialized in the page. So there will be no more error and the initial loading time of the index page will be reduced.&lt;/p&gt;

&lt;p&gt;That's all for today. Thank you for reading!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>barbajs</category>
    </item>
    <item>
      <title>How I am using composer to install Dolibarr modules</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Mon, 08 May 2023 18:12:20 +0000</pubDate>
      <link>https://dev.to/rabibsust/how-i-am-using-composer-to-install-dolibarr-modules-2poc</link>
      <guid>https://dev.to/rabibsust/how-i-am-using-composer-to-install-dolibarr-modules-2poc</guid>
      <description>&lt;p&gt;Those who don't know about Dolibarr, &lt;a href="https://www.dolibarr.org/"&gt;Dolibarr&lt;/a&gt; is a Open Source ERP and CRM system for businesses. It's codebase is written in PHP.&lt;/p&gt;

&lt;p&gt;Here is the &lt;a href="https://github.com/Dolibarr/dolibarr/"&gt;repository&lt;/a&gt; if you are interested. btw I don't think any developer will be comfortable working on this codebase first time 😅&lt;/p&gt;

&lt;p&gt;It had a composer file to manage some dependency before but in the current stable version it is disabled. You can check the commit message &lt;a href="https://github.com/Dolibarr/dolibarr/commit/857048c9bd1aa38e00ddbdc868a3936963805f1a"&gt;here&lt;/a&gt; 😜&lt;/p&gt;

&lt;p&gt;So anyway, to install any module we need to zip the module and upload via Dolibarr admin panel. Or we can copy the module to the htdocs/custom folder area. 😐&lt;/p&gt;

&lt;p&gt;But it causes some issues when we are continuously updating modules and managing version control system for modules.&lt;/p&gt;

&lt;p&gt;Also it causes some security risk when we open the upload functionality of the modules. 😱&lt;/p&gt;

&lt;p&gt;And honestly why we shouldn't use composer to install modules? It felt like I am in the age of Dinosaurs. 🙄&lt;/p&gt;

&lt;p&gt;So what we did was simple. Initialize the dolibarr project with composer. Created separate repositories for the modules.&lt;/p&gt;

&lt;p&gt;And when we need to install any modules we just need to add that in the composer.json file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "name": "dolibarr/dolibarr",
    "description": "Dolibarr ERP &amp;amp; CRM is a modern and easy to use web software to manage your business",
    "license": "GPL-3.0-or-later",
    "require": {
        "myproject/dashboard": "dev-master",
        "myproject/report": "dev-master"
    },
    "repositories": [
        {
            "url": "git@github.com:myproject/dashboard.git",
            "type": "git"
        },
        {
            "url": "git@github.com:myproject/report.git",
            "type": "git"
        }
    ],
    "config" : {
       "vendor-dir" : "../vendor"
    }
}

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

&lt;/div&gt;



&lt;p&gt;I did install the modules outside the project folder in the &lt;code&gt;vendor&lt;/code&gt; directory and created symlink to show this as the custom folder inside htdocs directory.&lt;/p&gt;

&lt;p&gt;And in the modules I added composer.json to make the module repositories as packages. Here is the composer.json file of the module&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "name": "myproject/dashboard",
    "description": "This module shows custom dashboard",
    "type": "dolibarr-module",
    "license": "MIT",
    "authors": [
        {
            "name": "Ahmad Jamaly Rabib",
            "email": "rabib@japantravel.com"
        }
    ],
    "require": {}
}

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

&lt;/div&gt;



&lt;p&gt;I have kept the require as empty as it is not needed now but we can modify based on our needs.&lt;/p&gt;

&lt;p&gt;So in this way whenever we add/update any module in our dolibarr main project we just need to run &lt;code&gt;composer install&lt;/code&gt; or &lt;code&gt;composer update&lt;/code&gt;. No need to copy the folders or making zip files to upload from dolibarr dashboard. 😎🥳&lt;/p&gt;

&lt;p&gt;We can do the same approach for any PHP projects to reuse codes and reduce complexity. 😇 Thank you for reading!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>php</category>
    </item>
    <item>
      <title>Wordpress: Getting out of meta queries</title>
      <dc:creator>Ahmad Jamaly Rabib</dc:creator>
      <pubDate>Fri, 03 Feb 2023 19:19:35 +0000</pubDate>
      <link>https://dev.to/rabibsust/wordpress-getting-out-of-meta-queries-851</link>
      <guid>https://dev.to/rabibsust/wordpress-getting-out-of-meta-queries-851</guid>
      <description>&lt;p&gt;I recently faced a really weird issue. I need to fetch data for event posts where the scenario is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There are two meta keys &lt;code&gt;start_date&lt;/code&gt; and &lt;code&gt;expire_date&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;I need to show all the events where these posts &lt;code&gt;expire_date&lt;/code&gt; value is greater than today's date and the events &lt;code&gt;start_date&lt;/code&gt; should be less than or equal today's date. Pretty straight forward right?&lt;/li&gt;
&lt;li&gt;But here is a catch. I also need to add a query where the start_date id greater than today's date. This can be a &lt;code&gt;OR&lt;/code&gt; condition where I need to show the events in both of these cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So normally what I did was to create a &lt;code&gt;meta_query&lt;/code&gt; where the relation will be &lt;code&gt;OR&lt;/code&gt; at first then for the &lt;code&gt;expire_date&lt;/code&gt; and &lt;code&gt;start_date&lt;/code&gt; condition an array with &lt;code&gt;AND&lt;/code&gt; condition. In another array other condition for the greater &lt;code&gt;start_date&lt;/code&gt;.&lt;br&gt;
It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'meta_query' =&amp;gt; [
            'relation' =&amp;gt; 'OR',
            [
                [
                    'key' =&amp;gt; 'start_date',
                    'value' =&amp;gt; date('Y-m-d'),
                    'compare' =&amp;gt; '&amp;lt;=',
                    'type' =&amp;gt; 'DATE'
                ],
                [
                    'key' =&amp;gt; 'expire_date',
                    'value' =&amp;gt; date('Y-m-d'),
                    'compare' =&amp;gt; '&amp;gt;=',
                    'type' =&amp;gt; 'DATE'
                ],
                'relation' =&amp;gt; 'AND'
            ],
            [
                'key' =&amp;gt; 'start_date',
                'value' =&amp;gt; date('Y-m-d'),
                'compare' =&amp;gt; '&amp;gt;',
                'type' =&amp;gt; 'DATE'
            ]
        ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This actually giving the correct result but it made the query complex. It made several inner joins and the condition was not efficient. So it was taking a lot of time to show the event results.&lt;br&gt;
These are the joins made for this meta query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
    INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id)
    INNER JOIN wp_postmeta AS mt2 ON (wp_posts.ID = mt2.post_id)
    INNER JOIN wp_postmeta AS mt3 ON (wp_posts.ID = mt3.post_id)
    INNER JOIN wp_postmeta AS mt4 ON (wp_posts.ID = mt4.post_id)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the where condition only for this meta_query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;wp_postmeta.meta_key = 'start_date'
        AND((mt1.meta_key = 'expire_date')
        AND(((mt2.meta_key = 'start_date'
            AND CAST(mt2.meta_value AS DATE) &amp;lt;= '2023-02-03')
        AND(mt3.meta_key = 'expire_date'
            AND mt3.meta_value &amp;gt;= '1675350000'))
    OR(mt4.meta_key = 'start_date'
        AND CAST(mt4.meta_value AS DATE) &amp;gt; '2023-02-03')))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It was taking almost 30 seconds to 1 minute to fetch all the event datas and showing.&lt;br&gt;
So I removed all the meta queries and replaced with the &lt;code&gt;wpdb&lt;/code&gt; query to write custom queries.&lt;/p&gt;

&lt;p&gt;From this above meta_query I replaced it. Queried to get the id's from two separate conditions for &lt;code&gt;start_date&lt;/code&gt; and &lt;code&gt;expire_date&lt;/code&gt; then merging these two arrays. Removed extra joins and too much complex meta_queries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ids1 = $wpdb-&amp;gt;get_col("`QUERY for the expire and start date`");
$ids2 = $wpdb-&amp;gt;get_col("`Query for start_date post ids`");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;$ids = array_merge($ids1, $ids2);&lt;br&gt;
in the wp args &lt;code&gt;'post__in' =&amp;gt; $ids&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Above wpdb queries made my query clear and I also understood what I was doing. It's performance improved. And I am now feeling relax! 😅&lt;br&gt;
See you again! 🫶&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>wordpress</category>
      <category>php</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
