{"id":4488,"date":"2024-05-14T20:11:00","date_gmt":"2024-05-14T14:41:00","guid":{"rendered":"https:\/\/preprod.namastedev.com\/blog\/2024\/05\/14\/laying-the-foundation-namaste-react\/"},"modified":"2024-05-14T20:14:11","modified_gmt":"2024-05-14T14:44:11","slug":"namaste-react-episode-2-laying-the-foundation","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/namaste-react-episode-2-laying-the-foundation\/","title":{"rendered":"Namaste React | Episode 2 | Laying the Foundation!"},"content":{"rendered":"<p>\ud83d\udca1<\/p>\n<p>Please make sure to follow along with the whole&nbsp;<strong><em>\u201dNamaste React&#8221;&nbsp;<\/em><\/strong>series, starting from Episode-1 and continuing through each subsequent episode. The notes are designed to provide detailed explanations of each concept along with examples to ensure thorough understanding. Each episode builds upon the knowledge gained from the previous ones, so starting from the beginning will give<\/p>\n<p>you a comprehensive understanding of React development.<\/p>\n<p><\/p>\n<p>\ud83d\udca1<\/p>\n<p>I&#8217;ve got a quick tip for you. To get the most out of these notes, it&#8217;s a good idea to watch&nbsp;<strong>Episode-2&nbsp;<\/strong>first. Understanding what&nbsp;<strong><em>\u201dAkshay\u201d&nbsp;<\/em><\/strong>shares in the video will make<\/p>\n<p>these notes way easier to understand.<\/p>\n<p><\/p>\n<h4><strong>So far, here&#8217;s what we&#8217;ve learned in the previous episode<\/strong><\/h4>\n<p>We learned that npm is anything but not node package manager and what is npx.<\/p>\n<p>We included node-modules and React in our project.<\/p>\n<p>We got to know the difference between package.json and package-lock.json.<\/p>\n<p>We also explored the concept of bundlers. We learned how to start our app.<\/p>\n<p>Don\u2019t forget \u201cParcel is a Beast\u201d.<\/p>\n<p><\/p>\n<p><\/p>\n<p><strong>Part-1<\/strong><\/p>\n<p><\/p>\n<p><strong>Q ) What is another way of starting the build of the project?<\/strong><\/p>\n<p><span>We will be creating scripts instead of using \u201c<\/span><em>npx parcel<\/em><em>index.html\u201d.&nbsp;We can create different scripts for starting our project in Development and Production.<\/em><\/p>\n<p><strong>package.json<\/strong><\/p>\n<p>In&nbsp;<span>command. <\/span><strong>in the script section write the following<\/strong><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/preprod.namastedev.com\/blog\/wp-content\/uploads\/2024\/05\/1-3.jpg\"><\/p>\n<p><\/p>\n<p><span>To run these scripts, enter the following commands in the terminal,<\/span>To start:<\/p>\n<p>npm run start<\/p>\n<p>or<\/p>\n<p>npm start<\/p>\n<p><\/p>\n<p>For Production Build:<\/p>\n<p>npm run build<\/p>\n<p><\/p>\n<p>\ud83d\udca1<\/p>\n<p>If you\u2019re not sure how to start the project in a new the company then find these scripts in them. <strong>package.json <\/strong>and use<\/p>\n<p><\/p>\n<p><strong>Part-2<\/strong><\/p>\n<p>Revision of previous Episodes<\/p>\n<p><\/p>\n<p><strong>Part-3<\/strong><\/p>\n<p><\/p>\n<h2><strong>Introducing JSX.<\/strong><\/h2>\n<p><\/p>\n<p><strong>App.js<\/strong><\/p>\n<p><span>Before we begin, we have to remove the existing React Code from&nbsp;<\/span>where we used&nbsp;<em>React.createElement()&nbsp;<\/em>for displaying content<\/p>\n<p>on the webpage but its syntax is very bad. It\u2019s not developer- friendly, and very hard to read. To solve this problem Facebook developers built JSX.<\/p>\n<p>JSX makes developer life easy as we no longer have to write our code using&nbsp;<em>React.createElement()<\/em><\/p>\n<p><\/p>\n<p>\ud83d\udce2<\/p>\n<p>NOTE: We write code for both Machines and Humans but<\/p>\n<p>first for Human understanding as it is read by a lot of developers<\/p>\n<p><\/p>\n<p><strong><em>Q ) What is JSX?<\/em><\/strong><\/p>\n<p>JSX is HTML-like or XML-like syntax. JSX stands for JavaScript XML. It&#8217;s a syntax extension for JavaScript.<\/p>\n<p>It is not a part of React. React apps can be built even without JSX but the code will become very hard to read.<\/p>\n<p>It is not HTML inside JavaScript.<\/p>\n<p>JavaScript engine cannot understand JSX as it only understands ECMAScript<\/p>\n<p><\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/preprod.namastedev.com\/blog\/wp-content\/uploads\/2024\/05\/1-4.jpg\"><\/p>\n<p><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/preprod.namastedev.com\/blog\/wp-content\/uploads\/2024\/05\/2-2.jpg\"><\/p>\n<p><\/p>\n<p>When we log&nbsp;<em>heading&nbsp;<\/em>and&nbsp;<em>jsxHeading<\/em>, it gives the same object. From this point, we will not be using&nbsp;<em>React.createElement()<\/em><\/p>\n<p><\/p>\n<h2><strong>Introducing Babel<\/strong><\/h2>\n<p><strong><em>Q ) Is JSX a valid JavaScript?<\/em><\/strong><\/p>\n<p>The answer is yes and no.<\/p>\n<p><span>JSX is not a valid Javascript syntax as it\u2019s not pure HTML or pure JavaScript for a browser to understand. JS does not have built-in JSX. The JS engine does not understand JSX because the JS engine understands ECMAScript or ES6+ code<\/span><\/p>\n<p><\/p>\n<p><strong><em>Q ) If the browser can\u2019t understand JSX how is it still working?<\/em><\/strong><\/p>\n<p>This is because of Parcel because \u201c<em>Parcel is a Beast\u201d.<\/em><\/p>\n<p>Before the code gets to JS Engine it is sent to Parcel and&nbsp;<span>Transpiled&nbsp;<\/span>there. Then after transpilation, the browser gets the code that it can understand.<\/p>\n<p><\/p>\n<p>Transpilation&nbsp;\u21d2&nbsp;<span>Converting the code in such a format that the browsers can understand.<\/span><\/p>\n<p><\/p>\n<p>Parcel is like a manager who gives the responsibility of transpilation to a package called&nbsp;<span>Babel<\/span>.<\/p>\n<p>Babel is a package that is a compiler\/transpiler of JavaScript that is already present inside \u2018<span>node-modules<\/span><span>\u2019<\/span>. It takes JSX and converts it into the code that browsers understand, as soon as we write it and save the file. It is not created by Facebook.<\/p>\n<p><a href=\"https:\/\/babeljs.io\/\" rel=\"noopener noreferrer\" target=\"_blank\" style=\"color: black\">Learn more about Babel on&nbsp;<\/a><a href=\"https:\/\/babeljs.io\/\" rel=\"noopener noreferrer\" target=\"_blank\">babeljs.io<\/a><\/p>\n<p><\/p>\n<p>JSX (transpiled by Babel)&nbsp;\u21d2&nbsp;React.createElement&nbsp;\u21d2&nbsp;ReactElement<\/p>\n<p>\u21d2&nbsp;JS Object&nbsp;\u21d2&nbsp;HTML Element(render)<\/p>\n<p><\/p>\n<p><strong><em>Q ) What is the difference between HTML and JSX?<\/em><\/strong><\/p>\n<ul>\n<li>JSX is not HTML. It\u2019s HTML-like syntax.<\/li>\n<li><span>HTML uses \u2018class\u2019 property whereas JSX uses \u2018className\u2019 property<\/span><\/li>\n<li><span>HTML can use hypens in property names whereas JSX uses camelCase syntax.<\/span><\/li>\n<li><strong>Single Line and Multi Line JSX Code<\/strong><\/li>\n<\/ul>\n<p><\/p>\n<p>Single line code:<\/p>\n<p><span>const&nbsp;<\/span>jsxHeading <span>=<\/span><\/p>\n<p><span>&lt;<\/span>h1<span>&gt;<\/span>Namaste React<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><\/p>\n<p>Multi-line code:<\/p>\n<p>If writing JSX in multiple lines then using \u2018()\u2019 parenthesis is mandatory. To tell Babel from where JSX is starting and ending.<\/p>\n<p><span>const&nbsp;<\/span>jsxHeading <span>=<\/span><\/p>\n<p><span>(<\/span><\/p>\n<p><span>&lt;<\/span><span>div<\/span><span>&gt;<\/span><\/p>\n<p><span>&lt;<\/span>h1<span>&gt;<\/span>Namaste React<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><span>&lt;\/<\/span>div<span>&gt;<\/span><\/p>\n<p><span>)<\/span><\/p>\n<p><\/p>\n<p><\/p>\n<p>\ud83d\udce2<\/p>\n<p>NOTE:<\/p>\n<ol>\n<li>Use \u201cPrettier &#8211; Code Formatter\u201d VS Code Extension to make your code look beautiful with proper formatting<\/li>\n<li>Use \u201cES lint\u201d VS Code Extension for linting<\/li>\n<li>Use \u201cBetter Comments\u201d VS Code Extension to beautify your comments<\/li>\n<\/ol>\n<p>Code all of these things discussed until now for better understanding.<\/p>\n<p><\/p>\n<p><\/p>\n<p><strong>Part-4<\/strong><\/p>\n<p><\/p>\n<h2><strong>Introducing React Components<\/strong><\/h2>\n<p>Everything inside React is a component.<\/p>\n<p><\/p>\n<p><strong><em>Q ) What are Components?<\/em><\/strong><\/p>\n<p>There are 2 types of components:<\/p>\n<ol>\n<li><strong>Class-based Components &#8211;&nbsp;<\/strong><strong>Old way of writing code, used rarely in industry<\/strong><\/li>\n<li><strong>Functional Components &#8211;&nbsp;<\/strong><strong>New way of writing code, most commonly used<\/strong><\/li>\n<\/ol>\n<p><\/p>\n<p><strong><em>Q ) What is a React Functional Components?<\/em><\/strong><\/p>\n<p>It is just a JavaScript Function that returns some JSX or a react element.<\/p>\n<p><\/p>\n<p>Always name React Functional Component with Capital Letters otherwise you will confuse it with normal function<\/p>\n<p><\/p>\n<p><span>\/\/ All are the same for single-line code&nbsp;<\/span><span>const&nbsp;<\/span><span>HeadingComponent1&nbsp;<\/span><span>()&nbsp;<\/span><span>=&gt;&nbsp;<\/span><span>(<\/span><\/p>\n<p><span>&lt;<\/span>h1<span>&gt;<\/span>Namaste<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><span>)<\/span><\/p>\n<p><\/p>\n<p><span>const&nbsp;<\/span><span>HeadingComponent2&nbsp;<\/span><span>()&nbsp;<\/span><span>=&gt;&nbsp;<\/span><span>{&nbsp;<\/span><span>return&nbsp;<\/span><span>&lt;<\/span>h1<span>&gt;<\/span>Namaste<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><span>}<\/span><\/p>\n<p><span>const&nbsp;<\/span><span>HeadingComponent3<\/span><\/p>\n<p><span>()&nbsp;<\/span><span>=&gt; &lt;<\/span>h1<span>&gt;<\/span>Namaste<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><\/p>\n<p>To render a functional component we call them \u2018<span>&lt;Heading1 \/&gt;<\/span>\u2019. This is the syntax that Babel understands.<\/p>\n<p>You can also call them using these ways, \u2018<span>&lt;Title&gt;&lt;\/Title&gt;<\/span>\u2019<\/p>\n<p>or \u2018{<span>Title()<\/span>}\u2019<\/p>\n<p><\/p>\n<p><strong>Components Composition<\/strong><\/p>\n<p>A component inside a component.<\/p>\n<p>Calling a component inside another component is Component Composition.<\/p>\n<p><\/p>\n<p><span>const&nbsp;<\/span><span>Title<\/span><\/p>\n<p><span>()&nbsp;<\/span><span>=&gt; &lt;<\/span>h1<span>&gt;<\/span>Namaste React<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><span>const&nbsp;<\/span><span>HeadingComponent&nbsp;<\/span><span>=&nbsp;<\/span><span>()&nbsp;<\/span><span>=&gt;&nbsp;<\/span><span>(<\/span><\/p>\n<p><span>&lt;<\/span>div id<span>=<\/span><span>&#8220;container&#8221;<\/span><span>&gt;<\/span><\/p>\n<p><span>&lt;<\/span>Title&nbsp;<span>\/&gt;<\/span><\/p>\n<p><span>&lt;\/<\/span>div<span>&gt;<\/span><\/p>\n<p><span>)<\/span><\/p>\n<p><\/p>\n<p>Code inside the \u2018Title\u2019 component will be used inside the \u2018HeadingComponent\u2019 component as the \u2018Title\u2019 component is called inside it. It will become something like this,<\/p>\n<p><\/p>\n<p><span>const&nbsp;<\/span><span>HeadingComponent&nbsp;<\/span><span>=&nbsp;<\/span><span>()&nbsp;<\/span><span>=&gt;&nbsp;<\/span><span>(<\/span><\/p>\n<p><span>&lt;<\/span>div id<span>=<\/span><span>&#8220;container&#8221;<\/span><span>&gt;<\/span><\/p>\n<p><span>&lt;<\/span>h1<span>&gt;<\/span>Namaste React<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><span>&lt;\/<\/span>div<span>&gt;<\/span><\/p>\n<p><span>)<\/span><\/p>\n<p><\/p>\n<p><\/p>\n<p><strong>Part-5<\/strong><\/p>\n<p><\/p>\n<p><strong><em>Q ) How to use JavaScript code inside JSX?<\/em><\/strong><\/p>\n<p>Inside a React Component when \u2018{}\u2019 parenthesis is present we can write any JavaScript expression inside it.<\/p>\n<p><\/p>\n<p><span>const&nbsp;<\/span>number&nbsp;<span>=&nbsp;<\/span><span>10000<\/span><span>;<\/span><\/p>\n<p><span>const&nbsp;<\/span><span>HeadingComponent&nbsp;<\/span><span>=&nbsp;<\/span><span>()&nbsp;<\/span><span>=&gt;&nbsp;<\/span><span>(<\/span><\/p>\n<p><span>&lt;<\/span>div id<span>=<\/span><span>&#8220;containter&#8221;<\/span><span>&gt;<\/span><\/p>\n<p><span>{<\/span>number<span>}<\/span><\/p>\n<p><span>&lt;<\/span>h1<span>&gt;<\/span>Namaste React<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><span>&lt;\/<\/span>div<span>&gt;<\/span><\/p>\n<p><span>)<\/span><\/p>\n<p><\/p>\n<p><strong><em>Q ) How to call React Element in JSX?<\/em><\/strong><\/p>\n<p>We can use \u2018{}\u2019 parenthesis.<\/p>\n<p><span>const&nbsp;<\/span>elem&nbsp;<span>= &lt;<\/span>span<span>&gt;&nbsp;<\/span>React Element&nbsp;<span>&lt;\/<\/span>span<span>&gt;<\/span><\/p>\n<p><span>const&nbsp;<\/span><span>HeadingComponent&nbsp;<\/span><span>=&nbsp;<\/span><span>()&nbsp;<\/span><span>=&gt;&nbsp;<\/span><span>(<\/span><\/p>\n<p><span>&lt;<\/span>div id<span>=<\/span><span>&#8220;containter&#8221;<\/span><span>&gt;<\/span><\/p>\n<p><span>{<\/span>elem<span>}<\/span><\/p>\n<p><span>&lt;<\/span>h1<span>&gt;<\/span>This is Namaste React<span>&lt;\/<\/span>h1<span>&gt;<\/span><\/p>\n<p><span>&lt;\/<\/span>div<span>&gt;<\/span><\/p>\n<p><span>)<\/span><\/p>\n<p><\/p>\n<p><strong><em>Q ) What will happen if we call 2 elements inside each other?<\/em><\/strong><\/p>\n<p>If we put 2 components inside each other, then it will go into an infinite loop and the stack will overflow. It will freeze your browser, so it\u2019s not recommended to do so.<\/p>\n<h3><strong>Advantages of using JSX.<\/strong><\/h3>\n<ol>\n<li>Sanitizes the data<\/li>\n<li>If someone gets access to your JS code and sends some malicious data which will then get displayed on the screen, that attack is called cross-site scripting.<\/li>\n<li>It can read cookies, local storage, session storage, get cookies, get info about your device, and read data. JSx takes care of your data.<\/li>\n<li>If some API passes some malicious data JSX will escape it. It prevents cross-site scripting and sanitizes the data before rendering<\/li>\n<li>Makes code readable<\/li>\n<li>JSX makes it easier to write code as we are no longer creating elements using React.createElement()<\/li>\n<li><\/li>\n<li>Makes code simple and elegant<\/li>\n<li>Show more useful errors and warnings<\/li>\n<li>JSX prevents code injections (attacks)<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udca1 Please make sure to follow along with the whole&nbsp;\u201dNamaste React&#8221;&nbsp;series, starting from Episode-1 and continuing through each subsequent episode. The notes are designed to provide detailed explanations of each concept along with examples to ensure thorough understanding. Each episode builds upon the knowledge gained from the previous ones, so starting from the beginning will<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[170],"tags":[224,223],"class_list":{"0":"post-4488","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-reactjs","7":"tag-react","8":"tag-reactjs"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4488","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=4488"}],"version-history":[{"count":2,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4488\/revisions"}],"predecessor-version":[{"id":4491,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4488\/revisions\/4491"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=4488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=4488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=4488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}