<?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: Bryan Almaraz</title>
    <description>The latest articles on DEV Community by Bryan Almaraz (@thee_divide).</description>
    <link>https://dev.to/thee_divide</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%2F223967%2F49c63640-0e01-4fbb-94b3-a67c01dff780.jpg</url>
      <title>DEV Community: Bryan Almaraz</title>
      <link>https://dev.to/thee_divide</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thee_divide"/>
    <language>en</language>
    <item>
      <title>Understanding Reconciliation: React Rendering Phases</title>
      <dc:creator>Bryan Almaraz</dc:creator>
      <pubDate>Mon, 16 Nov 2020 08:10:27 +0000</pubDate>
      <link>https://dev.to/thee_divide/reconciliation-react-rendering-phases-56g2</link>
      <guid>https://dev.to/thee_divide/reconciliation-react-rendering-phases-56g2</guid>
      <description>&lt;p&gt;Ever wonder how React takes your code and creates what you see on the screen? Or how React knows whether to update your component or not? &lt;/p&gt;

&lt;p&gt;Learning how rendering works in React will allow you to optimize your apps and make better decisions on how to structure your React application.&lt;/p&gt;

&lt;p&gt;Let's get started...&lt;/p&gt;

&lt;h2&gt;
  
  
  How React Works
&lt;/h2&gt;

&lt;p&gt;There are two phases to the React rendering cycle.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;render phase&lt;/strong&gt; and the &lt;strong&gt;commit phase&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here's the quick overview. The &lt;strong&gt;Render&lt;/strong&gt; phase takes your JSX and turns it into a javascript representation of what the HTML structure should look like. This is called the VirtualDOM. While the &lt;strong&gt;commit&lt;/strong&gt; phase is actually taking that representation and applying it to the real DOM. The whole process is called &lt;strong&gt;reconciliation&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rendering Phase
&lt;/h3&gt;

&lt;p&gt;There are also &lt;strong&gt;two&lt;/strong&gt; types of renders. An initial render and a re-render. Initial rendering is when your app first starts up. A re-render is when your state or props have updated.&lt;/p&gt;

&lt;p&gt;The initial rendering phase starts from your root component (usually App if using CRA) and works its way down the tree. React will take your JSX components and build out a javascript representation of what the actual DOM will look like with it. This is called the VirtualDOM and this is one part of the rendering phase.&lt;/p&gt;

&lt;p&gt;Once the Virtual DOM is created, React will compare what it has built out to what the actual DOM has using a fancy diff algorithm. However, once it has finished comparing it ends up with a list of what needs to be changed. This is still considered part of the rendering phase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side Note:&lt;/strong&gt; It's important to note that React goes through its virtualDOM and creates a list of those changes that need to be made to the actual DOM. Meaning that React will wait to commit all the updates so it does it in one swift process and not in parts. This is what it means when you read that React does batch updates. This is crucial to understand when working with state.&lt;/p&gt;

&lt;p&gt;Re-rendering is similar, but with one key difference. It does not go through every component to check for updates. Instead, when your component state or props update React uses a flag to mark that component. Basically saying that this component has been marked for an update.&lt;/p&gt;

&lt;h3&gt;
  
  
  Commit Phase
&lt;/h3&gt;

&lt;p&gt;Once React has done the comparison between its new and old components using that diff algorithm and has a list of changes. It will go ahead and &lt;em&gt;surgically&lt;/em&gt; apply those changes to the actual DOM. Meaning that it will only change the particular elements of the DOM that have changes, not every single element. This is called the commit phase.&lt;/p&gt;

&lt;p&gt;The commit phase is where React actually touches the DOM and makes changes.&lt;/p&gt;

&lt;p&gt;It's important to point out that React may go through the render phase but never commit. This can happen if the component returns the same result as the last time. Often happening if the parent component's state updates causing a render, but the child component(s) still create the same output anyways.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Fast Guide to Using React.memo</title>
      <dc:creator>Bryan Almaraz</dc:creator>
      <pubDate>Tue, 10 Nov 2020 16:49:39 +0000</pubDate>
      <link>https://dev.to/thee_divide/fast-guide-to-using-react-memo-315j</link>
      <guid>https://dev.to/thee_divide/fast-guide-to-using-react-memo-315j</guid>
      <description>&lt;h2&gt;
  
  
  What is React.memo?
&lt;/h2&gt;

&lt;p&gt;React.memo is a Higher Order Component that will wrap the component you want to memoize. It checks if the props have changed, if so it will continue with the re-render, if not then it will stop the re-render and all recursive renders of its child components. &lt;/p&gt;

&lt;p&gt;It is important to understand that it will only do a shallow comparison of your props. So functions and objects will be considered new props even if they haven't changed. This is due to reference equality for objects and functions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Side note:&lt;/strong&gt; React is pretty smart, if your state is an object. React does some magic behind the scenes and knows that your object in state has not changed. It does not do this with normal objects though.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// if you pass this state, react will not re-render your component unless state updated&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;someData&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="c1"&gt;// if you pass this as a prop, your memoized component will still re-render&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;someObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;someObj&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why do we need it?
&lt;/h2&gt;

&lt;p&gt;React.memo is useful when a parent component has re-rendered due to some state or prop change and a child component that lives inside that parent component does not make use of any of those changes. The child component should not have to re-render if it is going to return the same output.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use memo.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// UnrelatedComponent.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;UnrelatedComponent&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Footer&lt;/span&gt; &lt;span class="nx"&gt;Content&lt;/span&gt;&lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// before&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;UnrelatedComponent&lt;/span&gt;
&lt;span class="c1"&gt;// after, now the component will not re-render unless its props have changed.&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;memo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;UnrelatedComponent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// App.js&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setcount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h2&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h2&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;           &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UnrelatedComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
     &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If count updates and UnrelatedComponent is not using memo, then it will re-render even though it does not make use of the count state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Some Gotchas
&lt;/h3&gt;

&lt;p&gt;A few good to knows when using react.memo.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you are passing in a function, use the useCallback hook to memoize the function or else it will re-render due to reference type equalities.&lt;/li&gt;
&lt;li&gt;If you are passing in an object, use the useMemo hook to memoize the object or else it will re-render due to reference type equalities.&lt;/li&gt;
&lt;li&gt;React.memo takes in a function as a second parameter that has the previous and next props so you can have more fine grain control on what props should trigger a re-render.&lt;/li&gt;
&lt;li&gt;If your memoized component is using the children prop, it will always re-render due to the prop always being a reference.&lt;/li&gt;
&lt;li&gt;If you are using a function that should be updated every time a re-render happens, like for example a date function, or a random calculation, then you should not memoize the component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;More Resources: &lt;a href="https://dmitripavlutin.com/use-react-memo-wisely/"&gt;https://dmitripavlutin.com/use-react-memo-wisely/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding The Execution Context &amp; Stack</title>
      <dc:creator>Bryan Almaraz</dc:creator>
      <pubDate>Mon, 09 Nov 2020 09:36:24 +0000</pubDate>
      <link>https://dev.to/thee_divide/fast-guide-to-execution-context-execution-stack-in-javascript-3eij</link>
      <guid>https://dev.to/thee_divide/fast-guide-to-execution-context-execution-stack-in-javascript-3eij</guid>
      <description>&lt;p&gt;To become a good JavaScript developer you need to understand how JS is interpreted. Knowing how the execution context works will allow you to connect all the dots when it comes to learning about hoisting, scope, and closures. Let's get started.&lt;/p&gt;

&lt;p&gt;This guide is meant to be a quick break down and easy reference when you need it. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What is the Execution Context

&lt;ul&gt;
&lt;li&gt;Execution Context Phases&lt;/li&gt;
&lt;li&gt;Execution Context Types&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;What is the Execution Stack (Call Stack)&lt;/li&gt;
&lt;li&gt;What is hoisting&lt;/li&gt;
&lt;li&gt;What is scope&lt;/li&gt;
&lt;li&gt;What are closures&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is the Execution Context?
&lt;/h2&gt;

&lt;p&gt;Whenever JavaScript first runs your application code or invokes a function it creates an Execution Context. An Execution Context is a wrapper or container that helps manage the code currently executing in the program. It takes into account the environment (context) in which the code is currently executing.&lt;/p&gt;

&lt;p&gt;When JavaScript first interprets your code it creates a Global Execution Context. This is like a global environment for your application. This global environment is a giant object. It then creates a special keyword for you called "this". It sets "this" as a pointer to the giant global object. If you are running your code in the browser the giant global object is called the window object. It also creates a reference to the outer environment.&lt;/p&gt;

&lt;p&gt;In other words: The execution context is just a container that is currently running code and is taking into account the environment (context) in which it is running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There are two phases to the Execution Context.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Phases
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Creation&lt;/strong&gt; phase and the &lt;strong&gt;Execution&lt;/strong&gt; phase.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creation Phase
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Create a global object&lt;/li&gt;
&lt;li&gt;Create a "this" keyword and assign it to the global object&lt;/li&gt;
&lt;li&gt;Creates memory in its environment for the variables and functions inside the code&lt;/li&gt;
&lt;li&gt;Sets variables to &lt;em&gt;undefined&lt;/em&gt; &amp;amp; entire functions in memory&lt;/li&gt;
&lt;li&gt;Sets a reference to its outer environment (null in Global EC)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Execution Phase
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Starts executing your code line by line&lt;/li&gt;
&lt;li&gt;Sets values to your variables.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Execution Types
&lt;/h2&gt;

&lt;p&gt;The two important types of execution contexts in Javascript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Global EC&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Functional EC&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The global EC we just discussed. The functional EC is not very different from the global EC. It also has the same two phases only with slight alterations in the creation phase. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;del&gt;Create a global object&lt;/del&gt; | Create an arguments object&lt;/li&gt;
&lt;li&gt;Create a "this" keyword and assign it to the &lt;em&gt;existing&lt;/em&gt; global object&lt;/li&gt;
&lt;li&gt;Set aside memory in it's environment for the variables and functions inside the code&lt;/li&gt;
&lt;li&gt;Sets variables to undefined &amp;amp; functions in memory

&lt;ul&gt;
&lt;li&gt;Except arguments passed in become variables and are assigned to memory &lt;strong&gt;with their value&lt;/strong&gt;, not to undefined like regular variables.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Sets a reference to its outer environment&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Execution Stack (Call Stack)
&lt;/h3&gt;

&lt;p&gt;The execution stack or also known as the "call stack" is where execution contexts are stored. Keep in mind that a stack is a type of data structure that has one rule. "Last In, First Out".&lt;/p&gt;

&lt;p&gt;When Javascript runs your script it creates the global execution contexts and pushes it on to the Execution Stack. This becomes the base of your execution stack and will stay there until your program is finally done before it pops off. &lt;/p&gt;

&lt;p&gt;As mentioned before, the execution stack is just where execution contexts are stored. There are only two ways an execution context can be pushed onto the execution stack. The first is when your program starts up. The GEC is created and pushed onto the Execution Stack. The second is when JavaScript is executing your code and runs into a line that invokes a function. When a function is called a new execution context is created for that function. That Function Execution Context holds its "personal" variables and functions. Once that function is finished it pops off the Execution Stack and whatever was on the stack before that function was called is resumed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hoisting
&lt;/h3&gt;

&lt;p&gt;When JavaScript starts the creation phase of the execution context it creates your variables in memory and sets them to undefined. It's really that simple.&lt;/p&gt;

&lt;p&gt;So if you try using a variable before it was initialized it will be undefined (using the var keyword) instead of throwing an error. So it seems like your variable was "hoisted" to the top of the program because it knew that you had that variable. JavaScript just didn't know what the value was yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scope
&lt;/h3&gt;

&lt;p&gt;During the Execution Phase, JavaScript is creating new execution contexts for functions and already created the global execution context. So when referring to scope, it simply means what variables are in the current execution context. The variables and functions that are in that execution context are also in the scope.&lt;/p&gt;

&lt;p&gt;When using a variable that is not in a function, but outside the FEC then the scope extends outwards (and only outwards) to find that variable. &lt;/p&gt;

&lt;p&gt;Scope has a parent-child relationship where the child scope can ask the parent scope for values of variables but not vice-versa. &lt;strong&gt;This is called the scope chain.&lt;/strong&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  Closures
&lt;/h3&gt;

&lt;p&gt;When a function returns a function that references a variable in the outer function a closure is created.&lt;/p&gt;

&lt;p&gt;A closure encloses or locks-in its environment variables (scope) and has references to them even after that execution context has been popped off the stack. &lt;/p&gt;

&lt;h4&gt;
  
  
  How this is possible?
&lt;/h4&gt;

&lt;p&gt;When a function execution context is created remember that it creates it's own memory space for it's variables and functions. Once the function has finished and returns. The variables and functions that were created in that function are not deleted right away. They are just no longer accessible to the outer scope, but they are still somewhere in memory. &lt;/p&gt;

&lt;p&gt;It's like the global scope lost its address and can no longer locate it. But when a function returns a function that uses a variable from the function that returned it. Then that function that was returned will always have a reference to those variables even if the rest of the program does not. It encloses its variables and will remember where to find them. &lt;/p&gt;

&lt;p&gt;So a closure just makes sure its scope is always intact. A closure just knows where in memory its variables are, even if the execution context that created those variables is not on the call stack anymore.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
