{"id":4489,"date":"2024-05-14T20:13:00","date_gmt":"2024-05-14T14:43:00","guid":{"rendered":"https:\/\/preprod.namastedev.com\/blog\/2024\/05\/14\/talk-is-cheap-show-me-the-code-namaste-react\/"},"modified":"2024-05-14T20:16:16","modified_gmt":"2024-05-14T14:46:16","slug":"talk-is-cheap-show-me-the-code-namaste-react","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/talk-is-cheap-show-me-the-code-namaste-react\/","title":{"rendered":"Namaste React | Episode 4 | Talk is Cheap, Show me the Code!"},"content":{"rendered":"<p>Please make sure to follow along with the whole <em>\u201dNamaste React&#8221; <\/em>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 you a comprehensive understanding of React development.<\/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 Episode-3 first. Understanding what <em>\u201dAkshay\u201d<\/em> shares in the video will make these notes way easier to understand.<\/p>\n<blockquote><p>So far, here&#8217;s what we&#8217;ve learned in the previous episode<\/p><\/blockquote>\n<blockquote><p>We learned what\u2019s JSX.<\/p><\/blockquote>\n<blockquote><p>We explored what is transpilation and Babel.<\/p><\/blockquote>\n<blockquote><p>We got to know the difference between Class Based Components and Functional Components.<\/p><\/blockquote>\n<blockquote><p>We also explored the concept of bundlers.<\/p><\/blockquote>\n<blockquote><p>We learned what is component composition.<\/p><\/blockquote>\n<h2>Part-1<\/h2>\n<p>In this episode, we will start actual coding by starting a new project. Our app is going to a Food Ordering App.<\/p>\n<p>Planning for the UI<br \/>\nBefore we start coding, plan things out. Planning will make things easier to understand. We should know exactly what to build:<\/p>\n<p>Name the App<\/p>\n<p>UI Structure<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>Header<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Logo<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Nav Items<\/p>\n<p>Body<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Search<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Restaurant Container<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Restaurant Card<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Dish Name<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Image<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Restaurant Name<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Rating<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Cuisines<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Time to Deliver<\/p>\n<p>Footer<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Copyright<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Links<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Address<\/p>\n<p><span style=\"background-color: transparent;\">&#8211;<\/span> Contact<\/p>\n<p>\u200b<\/p>\n<p>Keep that as a reference and start coding the app.<\/p>\n<p>Let\u2019s start coding!<\/p>\n<p>It is recommended that you code on your own but for some examples, we have mentioned some pieces for you along with the component name.<\/p>\n<p>Main components = AppLayout<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const AppLayout <span style=\"background-color: transparent;\">=<\/span> () <span style=\"background-color: transparent;\">=&gt;<\/span> {<\/p>\n<p>return (<\/p>\n<p>&lt;div className=&#8221;app&#8221;&gt;<\/p>\n<p>&lt;Header\/&gt;<\/p>\n<p>&lt;Body\/&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>)<\/p>\n<p>}<\/p>\n<p>\u200b<\/p>\n<p>Header Component<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const Header <span style=\"background-color: transparent;\">=<\/span> () <span style=\"background-color: transparent;\">=&gt;<\/span> {<\/p>\n<p>return(<\/p>\n<p>&lt;div className=&#8221;header&#8221;&gt;<\/p>\n<p>&lt;div className=&#8221;logo-container&#8221;&gt;<\/p>\n<p>&lt;img className=&#8221;logo&#8221; src=&#8221;url&#8221; \/&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;div className=&#8221;nav-items&#8221;&gt;<\/p>\n<p>&lt;ul&gt;<\/p>\n<p>&lt;li&gt;Home&lt;\/li&gt;<\/p>\n<p>&lt;li&gt;About Us&lt;\/li&gt;<\/p>\n<p>&lt;li&gt;Contact Us&lt;\/li&gt;<\/p>\n<p>&lt;li&gt;Cart&lt;\/li&gt;<\/p>\n<p>&lt;\/ul&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>)<\/p>\n<p>}<\/p>\n<p>\u200b<\/p>\n<p>Inline Styling<\/p>\n<p>Writing the CSS along with the element in the same file. It is not recommended to use inline styling. So you should avoid writing it.<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>&lt;div<\/p>\n<p>className=&#8221;red-card&#8221;<\/p>\n<p>style={{ backgroundColor<span style=\"background-color: transparent;\">:<\/span> &#8220;#f0f0f0&#8221; }}<\/p>\n<p>&gt;<\/p>\n<p>&lt;h3&gt; Meghana Foods &lt;\/h3&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>\u200b<\/p>\n<p>In \u2018<em>style={{ backgroundColor: &#8220;#f0f0f0&#8221; }}<\/em>\u2019, first bracket is to tell that whatever is coming next will be JavaScript and the second bracket is for JavaScript object<\/p>\n<p>or you can store the CSS in a variable and then use it<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const styleCard <span style=\"background-color: transparent;\">=<\/span> { backgroundColor<span style=\"background-color: transparent;\">:<\/span> &#8220;#f0f0f0&#8243; };<\/p>\n<p>&lt;div<\/p>\n<p>className=&#8221;red-card&#8221;<\/p>\n<p>style={styleCard}<\/p>\n<p>&gt;<\/p>\n<p>&lt;h3&gt; Meghana Foods &lt;\/h3&gt;<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>\u200b<\/p>\n<h2>Part-2<\/h2>\n<h2><span style=\"color: inherit;\">Introducing Props.<\/span><\/h2>\n<h2><\/h2>\n<p>Short form for properties. To dynamically send data to a component we use props. Passing a prop to a function is like passing an argument to a function.<\/p>\n<p>Passing Props to a Component<\/p>\n<p>Example,<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>&lt;RestaurantCard<\/p>\n<p>resName=&#8221;Meghana Foods&#8221;<\/p>\n<p>cuisine=&#8221;Biryani, North Indian&#8221;<\/p>\n<p>\/&gt;<\/p>\n<p>\u200b<\/p>\n<p>\u2018<em>resName<\/em>\u2019 and <em>\u2018cuisine\u2019 <\/em>a props and this is prop passing to a component.<\/p>\n<p>Receiving props in the Component<\/p>\n<p>Props will be wrapped and send in Javascript object<\/p>\n<p>Example,<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const RestaurantCard <span style=\"background-color: transparent;\">=<\/span> (props) <span style=\"background-color: transparent;\">=&gt;<\/span> {<\/p>\n<p>return(<\/p>\n<p>&lt;div&gt;{props.resName}&lt;\/div&gt;<\/p>\n<p>)<\/p>\n<p>}<\/p>\n<p>\u200b<\/p>\n<p>Destructuring Props<\/p>\n<p>Example,<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const RestaurantCard <span style=\"background-color: transparent;\">=<\/span> ({resName, cuisine}) <span style=\"background-color: transparent;\">=&gt;<\/span> {<\/p>\n<p>return(<\/p>\n<p>&lt;div&gt;{resName}&lt;\/div&gt;<\/p>\n<p>)<\/p>\n<p>}<\/p>\n<p>\u200b<\/p>\n<h2><span style=\"color: inherit;\">Config Driven UI.<\/span><\/h2>\n<p>It is a user Interface that is built and configured using a declaration configuration file or data structure, rather than being hardcoded.<\/p>\n<p>Config is the data coming from the api which keeps on changing according to different factors like user, location, etc.<\/p>\n<p>To add something in the elements of array<\/p>\n<p>Example, Adding \u201c,\u201d after every value<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>resData.data.cuisine.join(&#8220;, &#8220;)<\/p>\n<p>\u200b<\/p>\n<p>Good Practices<\/p>\n<p><span style=\"color: inherit;\">Destructuring props-<\/span><\/p>\n<p>Optional Chaining<\/p>\n<p>Example,<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const {name, avgRating, cuisine} <span style=\"background-color: transparent;\">=<\/span> resData<span style=\"background-color: transparent;\">?.<\/span>data;<\/p>\n<p>\u200b<\/p>\n<p><span style=\"color: inherit;\">Repeating ourselves (repeating a piece of code again and again)-<\/span><\/p>\n<p>Dynamic Component listing using JS map() function to loop over an array and pass the data to component once instead of hard coding the same component with different props values<\/p>\n<p>Avoid <span style=\"background-color: initial;\"><img decoding=\"async\" src=\"image\/gif;base64,R0lGODlhAQABAIAAAP\/\/\/wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==\" alt=\"\u274c\" \/><\/span>\u200b<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>&lt;RestaurantCard<\/p>\n<p>resName=&#8221;Meghana Foods&#8221;<\/p>\n<p>\/&gt;<\/p>\n<p>&lt;RestaurantCard<\/p>\n<p>resName=&#8221;KFC&#8221;<\/p>\n<p>\/&gt;<\/p>\n<p>&lt;RestaurantCard<\/p>\n<p>resName=&#8221;McDonald&#8217;s&#8221;<\/p>\n<p>\/&gt;<\/p>\n<p>&lt;RestaurantCard<\/p>\n<p>resName=&#8221;Dominos&#8221;<\/p>\n<p>\/&gt;<\/p>\n<p>\u200b<\/p>\n<p>Follow <span style=\"background-color: initial;\"><img decoding=\"async\" src=\"image\/gif;base64,R0lGODlhAQABAIAAAP\/\/\/wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==\" alt=\"\u2705\" \/><\/span>\u200b<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const resList <span style=\"background-color: transparent;\">=<\/span> [<\/p>\n<p>{<\/p>\n<p>resName<span style=\"background-color: transparent;\">:<\/span> &#8220;Meghana Foods&#8221;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>resName<span style=\"background-color: transparent;\">:<\/span> &#8220;KFC&#8221;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>resName<span style=\"background-color: transparent;\">:<\/span> &#8220;McDonald&#8217;s&#8221;<\/p>\n<p>},<\/p>\n<p>{<\/p>\n<p>resName<span style=\"background-color: transparent;\">:<\/span> &#8220;Dominos&#8221;<\/p>\n<p>}<\/p>\n<p>]<\/p>\n<p>const Body <span style=\"background-color: transparent;\">=<\/span> () <span style=\"background-color: transparent;\">=&gt;<\/span> {<\/p>\n<p>return(<\/p>\n<p>&lt;div&gt;<\/p>\n<p>{resList.map((restaurant) <span style=\"background-color: transparent;\">=&gt;<\/span> (<\/p>\n<p>&lt;RestaurantCard resData={restaurant} \/&gt;<\/p>\n<p>))}<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>)<\/p>\n<p>}<\/p>\n<p>\u200b<\/p>\n<p><span style=\"color: inherit;\">Unique Key id while using map-<\/span><\/p>\n<p>Each item in the list must be uniquely identified<\/p>\n<p>Why?<\/p>\n<p>When we have components at same level and if a new component comes on the first without ID, DOM is going to re-render all the components again. As DOM can\u2019t identify where to place it.<\/p>\n<p>But if we give each of them a unique ID then react knows where to put that component according to the ID. It is a good optimization and performance thing.<\/p>\n<p>Note* Never use index as keys in map. It is not recommended.<\/p>\n<p>JavaScript<\/p>\n<p>Copy<\/p>\n<p>const Body <span style=\"background-color: transparent;\">=<\/span> () <span style=\"background-color: transparent;\">=&gt;<\/span> {<\/p>\n<p>return(<\/p>\n<p>&lt;div&gt;<\/p>\n<p>{resList.map((restaurant) <span style=\"background-color: transparent;\">=&gt;<\/span> (<\/p>\n<p>&lt;RestaurantCard key={restaurant.id} resData={restaurant} \/&gt;<\/p>\n<p>))}<\/p>\n<p>&lt;\/div&gt;<\/p>\n<p>)<\/p>\n<p>}<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Please make sure to follow along with the whole \u201dNamaste React&#8221; 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<\/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":["post-4489","post","type-post","status-publish","format-standard","category-reactjs","tag-react","tag-reactjs"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4489","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=4489"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4489\/revisions"}],"predecessor-version":[{"id":4495,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/4489\/revisions\/4495"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=4489"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=4489"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=4489"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}