{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/using-a-tool-vs-knowing-how-a-tool-works-internally","result":{"data":{"markdownRemark":{"excerpt":"My thoughts about using a tool vs. understanding how a tool works internally.  Before we start let me tell you that this post isn't about…","html":"<p>My thoughts about using a tool vs. understanding how a tool works internally. </p>\n<p>Before we start let me tell you that this post isn't about which tool is better vue or react, my intentions are to express my thoughts about \"Using a tool vs knowing how it works\" &#x26; \"If the tool is easier to work with, does that means it's easier to understand how it works?\"\nAlso note that i'm not an Vuejs expert.</p>\n<p>Here's an important thing, </p>\n<p>People say react is hard but it's not, they think it's hard because they don't know how it works. </p>\n<p>People say redux is hard but it's not, they think it's hard because they don't know how it works, </p>\n<p>People say Vue is amazing and easy to use (of course it is) but most people don't know how it works internally, but still they love it and find it easier than react. </p>\n<hr>\n<p>The point to be noted here is that Vuejs is magic, and Reactjs is JavaScript just plain simple JavaScript. </p>\n<p>Most people find react hard because they lack the fundamental understanding of the language (js). </p>\n<p>But they don't find vuejs hard because vue does magical stuff to abstract away all the things which you have to do in order to get the app running.</p>\n<p><strong>Now if I ask someone</strong><br>\n\"How does vuejs works\"<br>\n\"How vuejs handles the template\"<br>\n\"How v-if v-for works?\"  </p>\n<p>Well Vuejs parses the template in 3 stages. </p>\n<ol>\n<li>Compilation stage. </li>\n<li>Optimization stage. </li>\n<li>CodeGen stage.</li>\n</ol>\n<p>Vuejs firstly parses the template and creates an AST representation of the template and parses all the directives, elements, tags, childrens, and then it runs the second stage of optimization stage where it sets a \"static\" flag on the nodes which are not gonna be dynamic, and then finally it generates a render function (an hierarchical representation of VirtualDOM). </p>\n<p>Vue needs to hide these implementations away from the end users in order to make it a breeze to work with. (which is in my opinion is amazing)</p>\n<p>But knowing how vuejs internally works requires knowledge of AST, compilation, optimization, codegen etc etc. which in my opinion is not an easy task.\nAnd we haven't even talk about Reactivity in vuejs.</p>\n<p><strong>Now, let's talk about React.</strong></p>\n<p><strong>If I ask someone</strong><br>\n\"How react works?\"<br>\n\"How conditional rendering works?\"  </p>\n<p>Well in case of react, the internals are far far more simpler than Vuejs.\nand yes modern react uses <code class=\"language-text\">JSX compilation</code> but you need to know how React.createElement works not how <code class=\"language-text\">compilation</code> works in order to understand how react works.</p>\n<p>React has a <code class=\"language-text\">React.createElement</code> method which takes 3 parameters (tagName, props, childrens) in order to create a virtual dom node. </p>\n<p>basically -></p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-jsx line-numbers\"><code class=\"language-jsx\"><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;</span>p</span><span class=\"token punctuation\">></span></span><span class=\"token plain-text\">hello</span><span class=\"token tag\"><span class=\"token tag\"><span class=\"token punctuation\">&lt;/</span>p</span><span class=\"token punctuation\">></span></span> \n<span class=\"token comment\">// converts to</span>\nReact<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span><span class=\"token string\">\"p\"</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"hello\"</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span></span></pre></div>\n<p>Now with this simple factory function we can compose the whole virtual dom tree like this</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-jsx line-numbers\"><code class=\"language-jsx\">React<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span>\n  <span class=\"token string\">'div'</span><span class=\"token punctuation\">,</span> \n  <span class=\"token keyword\">null</span><span class=\"token punctuation\">,</span>\n  React<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span><span class=\"token string\">'p'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">,</span> <span class=\"token string\">\"hello\"</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<p>After composing the dom tree ReactDOM.render will recursively mount the vdom to the actual dom.</p>\n<p>And that's basically it. and the cool thing is because Reactjs is just plain simple javascript it answers all the questions like</p>\n<ul>\n<li>How conditional rendering works.  </li>\n<li>How loops will be handled.  </li>\n</ul>\n<p>Because React.createElement is just a function call you can just pass an array of childrens</p>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre style=\"counter-reset: linenumber NaN\" class=\"language-jsx line-numbers\"><code class=\"language-jsx\"><span class=\"token keyword\">let</span> arr <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token string\">'one'</span><span class=\"token punctuation\">,</span><span class=\"token string\">'two'</span><span class=\"token punctuation\">,</span> <span class=\"token string\">'three'</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n\nReact<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span>\n  <span class=\"token string\">'div'</span><span class=\"token punctuation\">,</span>\n  <span class=\"token keyword\">null</span><span class=\"token punctuation\">,</span>\n  arr<span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">v</span> <span class=\"token operator\">=></span> React<span class=\"token punctuation\">.</span><span class=\"token function\">createElement</span><span class=\"token punctuation\">(</span><span class=\"token string\">'p'</span><span class=\"token punctuation\">,</span> <span class=\"token keyword\">null</span><span class=\"token punctuation\">,</span> v<span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span></code><span aria-hidden=\"true\" class=\"line-numbers-rows\" style=\"white-space: normal; width: auto; left: 0;\"><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></pre></div>\n<hr>\n<p>In my opinion for an average person to understand \"How React works\" is far easier than \"How Vue works\"</p>\n<p>Because in case of vue you need to know about compilers, AST, optimizers, codegeneration.\nbut to understand React you just need to understand how vdom (for vue too) works and how recursion works.</p>\n<p>Now i know some people might disagree but its just my personal opinions.</p>\n<ol>\n<li>React is javascript.</li>\n<li>Vue is magic (not magic, CS actually).</li>\n<li>Vue is <strong>easier to work with.</strong></li>\n<li>React is <strong>easier to understand.</strong></li>\n</ol>\n<p>People like Vuejs because of its simplicity.\nI heard from lot of Vuejs devs that React is hard, and they say that because vuejs is easier to work with and does all the magic as i said earlier. </p>\n<p>But React is much more simpler to understand.</p>\n<hr>\n<p>The point of this whole post is not about \"React is better\" or \"Vue is better\" it's about as a developer we should know how our tools works because knowing that is beneficial. </p>\n<p><strong>\"Don't just use it, understand how it works\"</strong></p>","timeToRead":3,"id":"4a6ab33f-02b6-5902-bf2e-a570110eb889","frontmatter":{"date":"May 27, 2020","title":"Using a tool vs. knowing how a tool works internally"}}},"pageContext":{"slug":"/blog/using-a-tool-vs-knowing-how-a-tool-works-internally"}},"staticQueryHashes":["1033876704","3435786681","550521386"]}