<?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: Sajak Shrestha</title>
    <description>The latest articles on DEV Community by Sajak Shrestha (@sajakshrestha8).</description>
    <link>https://dev.to/sajakshrestha8</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%2F2742572%2F1b8ab7ef-733a-4c83-8e17-dcead3b23d49.jpeg</url>
      <title>DEV Community: Sajak Shrestha</title>
      <link>https://dev.to/sajakshrestha8</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sajakshrestha8"/>
    <language>en</language>
    <item>
      <title>DOM and DOM Tree</title>
      <dc:creator>Sajak Shrestha</dc:creator>
      <pubDate>Fri, 24 Jan 2025 16:31:32 +0000</pubDate>
      <link>https://dev.to/sajakshrestha8/dom-and-dom-tree-jlo</link>
      <guid>https://dev.to/sajakshrestha8/dom-and-dom-tree-jlo</guid>
      <description>&lt;p&gt;Hey, you know what DOM is? Let's begin from the full form of the DOM. Actually DOM stand for Document Object Model. Where Document is the HTML content of our webpage. Wait, firstly lets know that where is DOM actually used. DOM is used to manipulate the Document or the HTML. Did you just asked why? Let's understand Why.&lt;/p&gt;

&lt;p&gt;We know that HTML is the static application. But how is the data being updated in website if HTML is static? Here comes the DOM which connects the static HTML application to the Javascript in order to make the application dynamic. Now let's break the DOM in three different pieces.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Document &lt;br&gt;
Document is the HTML content of our application which contains the element and their attributes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Object&lt;br&gt;
Object is the Javascript object that contains the value and the attribute of the element&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Model&lt;br&gt;
Model is the hierarchical tree structure of the HTML document.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  HTML
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;head&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;h1&amp;gt;Learning DOM&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;We are learning DOM and DOM tree&amp;lt;/p&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  DOM
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    document: {
        body: {
            h1: "Learning DOM"
            p: "We are learrning DOM and DOM tree"
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we can see on the above image, DOM is the programmatic representation of the HTML document structured as a Javascript Object So, DOM is exactly identical to the HTML page.&lt;/p&gt;

&lt;p&gt;Then, What is the DOM tree? Let's discuss about it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvy4bxf30mwp7ks6nsbj2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvy4bxf30mwp7ks6nsbj2.png" alt="Image description" width="800" height="655"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As we can see the above Image is representing the HTML elements on the tree like structure using root and node. This is the actual DOM tree. So, According to the Document Object Model every HTML element is the object and the hierarchical structure of the nodes, where each HTML element is represented as node is the DOM Tree. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>useMemo in ReactJs</title>
      <dc:creator>Sajak Shrestha</dc:creator>
      <pubDate>Tue, 21 Jan 2025 14:37:01 +0000</pubDate>
      <link>https://dev.to/sajakshrestha8/usememo-in-reactjs-449</link>
      <guid>https://dev.to/sajakshrestha8/usememo-in-reactjs-449</guid>
      <description>&lt;p&gt;At the very starting after listening useMemo what come in mind at first is, what is useMemo? So, useMemo is the hook in react that helps on memoizating the computed value or the return result of the function. It is used for the optimization of the react component. Ok optimization, let's discuss how useMemo is helping in optimization. As we know react rerenders the component after the change in state. Because of which many unnecessary function or other operations that are heavy which are not linked to the state may execute too.&lt;br&gt;
&lt;/p&gt;

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

const App = () =&amp;gt; {
  const [count, setCount] = useState(0);
  const [item, setItem] = useState(1);

  let changingCount = count + 1;
  console.log("UpdatedCount:", changingCount);

  return (
    &amp;lt;&amp;gt;
      &amp;lt;div&amp;gt;State: {count}&amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;Item: {item}&amp;lt;/div&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setItem(item + 1)}&amp;gt;Increase Item&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Increase Count&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

export default App;

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

&lt;/div&gt;



&lt;p&gt;As we can see in the snippet above, my application is being rerender after I am clicking the button. Let's focus on the point console.log in application. My console is executing every time I am clicking any button. Wait, but my console is depending on my state "count". What can I do so that I can execute my console only when my "count" state is updating. Yeah, exactly we can use useMemo hook for that. Let's use it now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useMemo, useState } from "react";

const App = () =&amp;gt; {
  const [count, setCount] = useState(0);
  const [item, setItem] = useState(1);

  useMemo(() =&amp;gt; {
    let changingCount = count + 1;
    console.log("UpdatedCount:", changingCount);
  }, [count]);



  return (
    &amp;lt;&amp;gt;
      &amp;lt;div&amp;gt;State: {count}&amp;lt;/div&amp;gt;
      &amp;lt;div&amp;gt;Item: {item}&amp;lt;/div&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setItem(item + 1)}&amp;gt;Increase Item&amp;lt;/button&amp;gt;
      &amp;lt;button onClick={() =&amp;gt; setCount(count + 1)}&amp;gt;Increase Count&amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  );
};

export default App;


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

&lt;/div&gt;



&lt;p&gt;What benefit did we get by using useMemo hook. So, as you can see here useMemo has two dependencies first one is a callback function and the second one is the dependency array. Now my function or the console is only executing when my count state is being updated which means the function is now dependent on the state. Wow, is my application being optimized? Yes of course we tackle the unnecessary computation of our application. &lt;/p&gt;

&lt;p&gt;Callback function and dependency array as parameter. It should be right? Yeah it sounds similar to the useEffect but understand both have their own usage where useEffect is used for handling sideEffects in reacts. Wait wait, what is sideEffect? SideEffect is the interaction of the application with the external substance like api fetching and using events. Where useMemo is used to tackle the unnecessary computations.&lt;/p&gt;

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