<?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: Ahmed Murtaza</title>
    <description>The latest articles on DEV Community by Ahmed Murtaza (@ahmedgmurtaza).</description>
    <link>https://dev.to/ahmedgmurtaza</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%2F228622%2Fe593c283-e42e-4d7f-a1f9-cbcb2ad9ec1c.jpg</url>
      <title>DEV Community: Ahmed Murtaza</title>
      <link>https://dev.to/ahmedgmurtaza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmedgmurtaza"/>
    <language>en</language>
    <item>
      <title>Nuxt Joins Vercel. What This Means for the Future of Web Frameworks</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Wed, 09 Jul 2025 08:08:45 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/nuxt-joins-vercel-what-this-means-for-the-future-of-web-frameworks-dc7</link>
      <guid>https://dev.to/ahmedgmurtaza/nuxt-joins-vercel-what-this-means-for-the-future-of-web-frameworks-dc7</guid>
      <description>&lt;p&gt;In a major development in the JavaScript ecosystem, &lt;strong&gt;Nuxt.js&lt;/strong&gt; the popular Vue-based framework, has officially joined Vercel, the creators behind &lt;strong&gt;Next.js&lt;/strong&gt;, the dominant React-based framework. This strategic move brings the two leading meta frameworks, powered by Vue and React respectively under one roof.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Quick Look at Nuxt.js and Next.js
&lt;/h2&gt;

&lt;p&gt;Next.js has become the go to solution for server side rendering (SSR), static site generation (SSG), and hybrid apps in the React ecosystem. With a strong developer experience, built in routing, and first class support for performance, it's widely used in production by giants like Netflix, TikTok, Uber.. and yep, even many projects inside Bank Albilad (shoutout to my team!).&lt;/p&gt;

&lt;p&gt;Nuxt.js, on the other hand, brings the same principles to the Vue ecosystem, simplifying SSR, static generation, routing, and even full-stack capabilities with Nuxt Server. It’s loved for its modular architecture and ease of use, especially in the Vue community.&lt;/p&gt;

&lt;p&gt;Both frameworks have shaped the way developers build modern web apps, opinionated yet flexible, optimized yet extensible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why This Merger Matters
&lt;/h3&gt;

&lt;p&gt;This move could reshape the future of meta-frameworks. Here’s why:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Multi framework vision: Vercel embracing Vue officially signals its ambition to become the universal platform for frontend frameworks, not just React as it acquired Svelte too lately.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What Could Happen Next?
&lt;/h3&gt;

&lt;p&gt;Here are some of my assumptions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Nuxt's tighter integration with Vercel, zero-config deployment, automatic performance tuning, and deeper analytics.&lt;/li&gt;
&lt;li&gt;Shared infrastructure enhancements, improvements in build speed, caching, and streaming may benefit both frameworks.&lt;/li&gt;
&lt;li&gt;Community crossovers, this may encourage more collaboration between Vue and React devs, reducing the divide and increasing innovation.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Thoughts
&lt;/h3&gt;

&lt;p&gt;This is not just a merger of teams, it’s a merging of philosophies and developer ecosystems. As Nuxt becomes a core part of the vercel family, the developer experience across both Vue and React is bound to get better, faster, and more unified.&lt;/p&gt;

&lt;p&gt;We’re entering a new era where &lt;strong&gt;framework wars might shift to framework harmony&lt;/strong&gt;, and that’s a big win for the web.&lt;/p&gt;




&lt;p&gt;Hey, I’m an indie hacker who loves building tools that help real people in real ways and ideally get used by millions. Right now, I’m working on &lt;a href="https://arabicworksheet.com" rel="noopener noreferrer"&gt;arabicworksheet.com&lt;/a&gt;, a free resource I personally use every day to learn local Saudi Arabic.&lt;/p&gt;

&lt;p&gt;If you're living in Saudi or anywhere in the Middle East and still getting by on just "Shukran" and "Maafi Maloom Arabi," this is your sign. Go check it out, no cost, super practical, and built exactly for you. It might just make your life a whole lot easier.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>👮🛡️ Let's Navigate the Angular Playground with Route Guards</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Sun, 17 Dec 2023 07:29:03 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/lets-navigate-the-angular-playground-with-route-guards-3dka</link>
      <guid>https://dev.to/ahmedgmurtaza/lets-navigate-the-angular-playground-with-route-guards-3dka</guid>
      <description>&lt;p&gt;Imagine we are in a big, exciting playground filled with different areas to explore. Angular route guards are like our friendly guides, making sure we have fun while keeping things safe and sound. Let's discuss what these route guards have to offer, helping us navigate our playground safely and securely.&lt;/p&gt;

&lt;p&gt;Here is the sneak peak of when and where we use Route Guards based on different scenarios:&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%2Ficd16p5poekt1rl00or2.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%2Ficd16p5poekt1rl00or2.png" alt=" " width="800" height="820"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Can I Come In? - The CanActivate Guard
&lt;/h2&gt;

&lt;p&gt;You know that feeling when you want to enter a special play zone, but there is a nice adult checking if you are allowed? Thats what &lt;em&gt;CanActivate&lt;/em&gt; does in Angular. It ensures that you have the green light to access a particular route. It's perfect for scenarios like letting only loggedin users into the VIP section of our play area.&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%2Fdtz2qjvfrxlusuhqoeyt.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%2Fdtz2qjvfrxlusuhqoeyt.png" alt=" " width="800" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Ready to Leave? - The CanDeactivate Guard
&lt;/h2&gt;

&lt;p&gt;Sometimes, we need to check if you're ready to leave an area. The &lt;em&gt;CanDeactivate&lt;/em&gt; guard is like asking, "&lt;strong&gt;Are you sure you want to leave? Did you collect all your toys?&lt;/strong&gt;" It comes in handy when you want to make sure users don't accidentally leave a page with unsaved changes or partially filled form. So basically this protects the route, however it does not prevent that module from loading.&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%2Feo6i2ztuy0uv4vp1bof3.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%2Feo6i2ztuy0uv4vp1bof3.png" alt=" " width="800" height="626"&gt;&lt;/a&gt;&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%2F2hu031ekd7mx4863y4b7.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%2F2hu031ekd7mx4863y4b7.png" alt=" " width="800" height="466"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Show Your Badge - The CanLoad Guard
&lt;/h2&gt;

&lt;p&gt;Imagine some areas in the playground require a special permission. Angular's &lt;em&gt;CanLoad&lt;/em&gt; guard checks if you have the right permission before loading an entire section. This is super useful for lazy-loaded modules, ensuring that only the cool kids with the proper permissions can access certain parts of our application. &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%2F6i5qskrzjddpvng3m3ws.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%2F6i5qskrzjddpvng3m3ws.png" alt=" " width="800" height="538"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In comparision to &lt;em&gt;CanActivate&lt;/em&gt; guard, &lt;em&gt;CanLoad&lt;/em&gt; protects the route completely. Such as lazy loading the module and also protects all the routes associated with that module. If the user is unauthorized then this &lt;em&gt;canLoad&lt;/em&gt; guard avoids additional loading of auth Modules&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Checking Your Bag - The Resolve Guard
&lt;/h2&gt;

&lt;p&gt;Before you enter a play zone, it's always a good idea to check if you have everything you need. &lt;em&gt;Resolve&lt;/em&gt; guard is like looking into your bag to make sure you are prepared. It lets you fetch data or do some last minute setup before the route is activated.&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%2Fkxszs03funedqtf8rghc.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%2Fkxszs03funedqtf8rghc.png" alt=" " width="800" height="482"&gt;&lt;/a&gt;&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%2F9cczj1ho3a2l5trzvywu.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%2F9cczj1ho3a2l5trzvywu.png" alt=" " width="800" height="601"&gt;&lt;/a&gt;&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%2Fqzlroeafqnyfdvdshkgp.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%2Fqzlroeafqnyfdvdshkgp.png" alt=" " width="800" height="555"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And there you have it, a playful introduction to Angular route guards!! They are your trusty companions, making sure your journey through the Angular playground is smooth, secure and full of excitement. &lt;/p&gt;

&lt;p&gt;Happy coding folks! 🚀&lt;/p&gt;

</description>
      <category>angular</category>
      <category>react</category>
      <category>javascript</category>
      <category>security</category>
    </item>
    <item>
      <title>🏋Map &amp; Set, Secret Weapons for Effortless Data Management in JavaScript</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Sun, 10 Dec 2023 09:46:42 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/map-and-set-secret-weapons-for-effortless-data-management-in-javascript-ojd</link>
      <guid>https://dev.to/ahmedgmurtaza/map-and-set-secret-weapons-for-effortless-data-management-in-javascript-ojd</guid>
      <description>&lt;p&gt;Ever found yourself wrestling with arrays and plain objects? 🥴 Wishing there was a simpler way to handle data in JavaScript? &lt;/p&gt;

&lt;p&gt;Well, good news!! We've got some cool features called &lt;strong&gt;Map&lt;/strong&gt; and &lt;strong&gt;Set&lt;/strong&gt; that make handling data way simpler. Today we will discuss these handy tools and see why they are much friendlier compared to the traditional ways.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. JavaScript Map: Friends with Key-Value Pairs
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Back in the Day:&lt;/strong&gt;&lt;br&gt;
Remember when we used plain objects to store data like this?&lt;br&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%2Fjyquut02lkiggryroyb8.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%2Fjyquut02lkiggryroyb8.png" alt=" " width="800" height="359"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The New and easy Way:&lt;/strong&gt;&lt;br&gt;
Now with Map, it's like upgrading from an old phone to a shiny new smartphone:&lt;br&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%2F42h3pfdqjcg6ae22g0l1.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%2F42h3pfdqjcg6ae22g0l1.png" alt=" " width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Why Map is Cool:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;It lets you use any type of data as a key&lt;/li&gt;
&lt;li&gt;Built-in methods make it super easy to work with, no more tricky workarounds.&lt;/li&gt;
&lt;li&gt;It keeps things neat with a guaranteed order when you go through the items&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. JavaScript Set: Keeping Things Unique and Fast
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Back to the Grind:&lt;/strong&gt;&lt;br&gt;
Remember the hassle of checking for duplicate numbers in arrays?? Not the most fun part:&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%2Fqmi80jr9hsglgii517yy.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%2Fqmi80jr9hsglgii517yy.png" alt=" " width="800" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hello Simplicity:&lt;/strong&gt;&lt;br&gt;
Now, &lt;strong&gt;Set&lt;/strong&gt; comes to the rescue, doing all the hard work for us:&lt;br&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%2Fw5p0fkpce843jlu2f3q2.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%2Fw5p0fkpce843jlu2f3q2.png" alt=" " width="800" height="246"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Why Set is Awesome:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;No need to manually check for duplicates, it keeps things unique without us lifting a finger.&lt;/li&gt;
&lt;li&gt;Faster lookups and removals.&lt;/li&gt;
&lt;li&gt;It remembers the order things were added.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Conclusion:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So here you have a sneak peek into the world of Map and Set, Your new best buddies for easy data handling🏋. &lt;br&gt;
Say goodbye to the old, clunky ways and welcome a simpler and more efficient way to manage your data. Happy coding!!😎&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>✨React State Management: UseState vs UseReducer</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Fri, 13 Jan 2023 09:48:38 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/usecases-for-usestate-vs-usereducer-in-react-43o9</link>
      <guid>https://dev.to/ahmedgmurtaza/usecases-for-usestate-vs-usereducer-in-react-43o9</guid>
      <description>&lt;p&gt;In React, &lt;strong&gt;useState&lt;/strong&gt; and &lt;strong&gt;useReducer&lt;/strong&gt; are two hooks that are used to manage state in a functional component. Both hooks allow you to update state and re-render your component, but they have different use cases and trade-offs.&lt;/p&gt;

&lt;h3&gt;
  
  
  useState
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;useState&lt;/strong&gt; is a Hook that allows you to store a single piece of state in your component. It is easy to use and understand, making it a good choice for small and simple state management tasks.&lt;/p&gt;

&lt;p&gt;To use &lt;strong&gt;useState&lt;/strong&gt;, you need to call the Hook in your component and provide an initial value. The Hook will return an array with two elements, the first being the current value of the state, and the second being a function to update the state.&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%2F8e07971466c9dgs0hanz.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%2F8e07971466c9dgs0hanz.png" alt=" " width="800" height="312"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we are using &lt;strong&gt;useState&lt;/strong&gt; to store a piece of state called &lt;strong&gt;name&lt;/strong&gt;, with an initial value of "Ahmed Murtaza". The variable &lt;strong&gt;name&lt;/strong&gt; contains the current value of the state, and the variable &lt;strong&gt;setName&lt;/strong&gt; is a function that can be used to update the state.&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%2Fto9rt8g5rj9tcks23o5r.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%2Fto9rt8g5rj9tcks23o5r.png" alt=" " width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we are using the &lt;strong&gt;name&lt;/strong&gt; state variable as the value of an input field, and using the &lt;strong&gt;setName&lt;/strong&gt; function to update the state when the input field value changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  useReducer
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;useReducer&lt;/strong&gt; is a Hook that allows you to store multiple pieces of state and perform more complex state updates. It is more powerful than &lt;strong&gt;useState&lt;/strong&gt;, but also more complex to use and understand, making it a better choice &lt;em&gt;for larger and more complex state management tasks&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To use &lt;strong&gt;useReducer&lt;/strong&gt;, you need to call the Hook in your component and provide a reducer function and an initial state. The Hook will return an array with two elements, the first being the current state, and the second being a function to dispatch actions to the reducer.&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%2Frpsrx2q50eelmnvj0hqn.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%2Frpsrx2q50eelmnvj0hqn.png" alt=" " width="800" height="522"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we are using &lt;strong&gt;useReducer&lt;/strong&gt; to store multiple pieces of state in an object called &lt;strong&gt;state&lt;/strong&gt;, with an initial value of &lt;strong&gt;{ name: "Ahmed Murtaza", age: 29 }&lt;/strong&gt;. The variable &lt;strong&gt;state&lt;/strong&gt; contains the current state, and the variable &lt;strong&gt;dispatch&lt;/strong&gt; is a function that can be used to dispatch actions to the reducer.&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%2Fofdex59klt831duy1jx0.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%2Fofdex59klt831duy1jx0.png" alt=" " width="800" height="305"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the example above, we are using the &lt;strong&gt;state.name&lt;/strong&gt; state variable as the value of an input field, and using the &lt;strong&gt;dispatch&lt;/strong&gt; function to update the state when the input field value changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, &lt;strong&gt;useState&lt;/strong&gt; and &lt;strong&gt;useReducer&lt;/strong&gt; are both very useful Hooks in React. It's more personal choice as well as other factors i.e performance and complexity, which we can consider picking the most suitable hook.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>programming</category>
    </item>
    <item>
      <title>⚡️React Performance Optimization: useMemo vs useCallback</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Sun, 08 Jan 2023 09:18:51 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/react-performance-optimization-usememo-vs-usecallback-2p2a</link>
      <guid>https://dev.to/ahmedgmurtaza/react-performance-optimization-usememo-vs-usecallback-2p2a</guid>
      <description>&lt;p&gt;&lt;strong&gt;useCallback&lt;/strong&gt; and &lt;strong&gt;useMemo&lt;/strong&gt; are both React Hooks that help optimize the performance of a React application by memoizing values. They both accept a function as an argument and return a memoized version of the function.&lt;/p&gt;

&lt;p&gt;Here is a simplified explanation of the difference between the two:&lt;/p&gt;

&lt;h3&gt;
  
  
  useCallback
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;useCallback&lt;/strong&gt; is a hook that returns a memoized callback. A callback is a function that is passed as an argument to another function. In the context of React, a callback is often used as an event handler or to pass data between components. The &lt;strong&gt;useCallback&lt;/strong&gt; hook takes two arguments: a function and a dependency array. It will return a memoized version of the function that only changes if one of the dependencies has changed.&lt;/p&gt;

&lt;p&gt;Here's an example of how you might use the &lt;strong&gt;useCallback&lt;/strong&gt; hook:&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%2Fttij427xwmussu5ch4ml.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%2Fttij427xwmussu5ch4ml.png" alt=" " width="800" height="604"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the handleClick function is passed as an event handler to the button element. The &lt;strong&gt;useCallback&lt;/strong&gt; hook ensures that the handleClick function is only re-created if the count prop changes. This can be useful if the ParentComponent is re-rendered frequently, as it can help to prevent the function from being unnecessarily re-created.&lt;/p&gt;

&lt;h3&gt;
  
  
  useMemo
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;useMemo&lt;/strong&gt; is a hook that returns a memoized value. It takes two arguments: a function that returns a value and a dependency array. It will call the function and return its result only if one of the dependencies has changed.&lt;/p&gt;

&lt;p&gt;Here's an example of how you might use the &lt;strong&gt;useMemo&lt;/strong&gt; hook:&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%2F0dw0lijomvrmjw99428d.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%2F0dw0lijomvrmjw99428d.png" alt=" " width="800" height="447"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the processedData value is created by calling the function passed to useMemo. This function does some expensive processing with the data prop. The useMemo hook ensures that the processedData value is only re-computed if the data prop changes. This can be useful if the processing is time-consuming and you don't want it to be re-done unnecessarily.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;To summarize, the main difference between useCallback and useMemo is the type of value they return. useCallback returns a memoized callback function, while useMemo returns a memoized value. Both hooks can be used to optimize the performance of your React components by avoiding unnecessary re-creations of functions or values.&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>⚡️JavaScript Optional Chaining (?.)</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Wed, 17 Aug 2022 11:28:00 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/javascript-optional-chaining--2cn1</link>
      <guid>https://dev.to/ahmedgmurtaza/javascript-optional-chaining--2cn1</guid>
      <description>&lt;p&gt;Optional chaining allows us to safely read the value of each deeply nested property within an object (or chained objects) without the need to separately evaluate the existence of each property.&lt;/p&gt;

&lt;p&gt;For newbies, it might not touch them deeply, but optional chaining solves a great deal of errors JavaScript developers face very frequently.&lt;/p&gt;

&lt;h2&gt;
  
  
  📌Example #1:
&lt;/h2&gt;

&lt;p&gt;Consider a &lt;strong&gt;user&lt;/strong&gt; object, which is supposed to have user relevant details as properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Let user = {
   name: ‘John Doe’, 
   email: john@doe.com
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consider you try to access &lt;code&gt;user.address.postcode&lt;/code&gt;, which will eventually give “Cannot read property of undefined..” Error. It’s because &lt;strong&gt;address&lt;/strong&gt; is &lt;strong&gt;undefined&lt;/strong&gt; and attempt to get &lt;code&gt;user.address.postcode&lt;/code&gt; fails with an error as &lt;strong&gt;undefined&lt;/strong&gt; doesn’t have Object like properties.&lt;/p&gt;

&lt;h2&gt;
  
  
  📌Example #2:
&lt;/h2&gt;

&lt;p&gt;In web application we access DOM elements, such as, &lt;code&gt;document.querySelector(‘#heading’)&lt;/code&gt;, and can access its properties or manipulate content using &lt;code&gt;document.querySelector(‘#heading’).innerText&lt;/code&gt;. Now In-case &lt;code&gt;document.querySelector(‘#heading’)&lt;/code&gt; didn’t return any element instead &lt;strong&gt;undefined&lt;/strong&gt;, it will again fail and show same ‘non-existing property’ error.&lt;/p&gt;

&lt;h2&gt;
  
  
  🌟Introducing Optional Chaining (?.):
&lt;/h2&gt;

&lt;p&gt;Optional chaining helps us by not showing an Error in-case there is nullish (null or undefined) property before ?. operator and return &lt;strong&gt;undefined&lt;/strong&gt; without further evaluation.&lt;/p&gt;

&lt;p&gt;In other words:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;user?.address?.postcode&lt;/code&gt; will return undefined if address is undefined instead of throwing an Error.&lt;/li&gt;
&lt;li&gt;It greatly helps to reduce multiple conditions evaluating each nested property whether they are nullish or not.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>How to use Async/Await in JavaScript</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Wed, 26 Jan 2022 12:48:36 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/how-to-use-asyncawait-in-javascript-5c1m</link>
      <guid>https://dev.to/ahmedgmurtaza/how-to-use-asyncawait-in-javascript-5c1m</guid>
      <description>&lt;p&gt;Promises have been a great escape from callbacks, ever since they were introduced. Instead of callbacks hell, promises provide &lt;strong&gt;.then()&lt;/strong&gt; API to chain multiple promises. However, in-case there are multiple dependencies, using &lt;strong&gt;.then&lt;/strong&gt; becomes dreadful too and seems no much different than callbacks hell:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(...))&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;fetchImage&lt;/span&gt;&lt;span class="p"&gt;(...))&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayPublicUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPublic&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="nf"&gt;displayUser&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Introducing Async/Await
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Async&lt;/strong&gt; and &lt;strong&gt;Await&lt;/strong&gt; keywords were introduced in ECMAScript 2017. Though they work as a synthetical sugar for Promises only, However, the asynchronous behavior of promises looks much like synchronous now and the code becomes much easier to handle. Comparing the above code using Async/Await will be like:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchImage&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;     
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayPublicUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isPublic&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nf"&gt;displayUser&lt;/span&gt;&lt;span class="p"&gt;(...);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Await must be inside Async function&lt;/strong&gt;&lt;br&gt;
In order to use &lt;strong&gt;await&lt;/strong&gt;, make sure the parent function has async keyword attached to it. In case the function is not Async, then using Promises would work fine. Or there is an alternative to use IIFE wrapper to introduce &lt;strong&gt;Async&lt;/strong&gt; keyword:&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`https://some-api/user/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//WRONG&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//CORRECT &lt;/span&gt;
&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="o"&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;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{...})&lt;/span&gt;

&lt;span class="c1"&gt;//OR&lt;/span&gt;

&lt;span class="c1"&gt;//CORRECT&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Error Handling
&lt;/h2&gt;

&lt;p&gt;It's recommended to handle errors prior to their appearance. I have got two of the most used approaches below:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The one I like and used most is:&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;displayUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fetchUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it for now, your comments are highly appreciated. See ya! 👋.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>Show loading indicator for Lazy Modules in Angular</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Sun, 23 Jan 2022 17:59:37 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/show-loading-indicator-for-lazy-modules-in-angular-4knf</link>
      <guid>https://dev.to/ahmedgmurtaza/show-loading-indicator-for-lazy-modules-in-angular-4knf</guid>
      <description>&lt;p&gt;In Angular, By default, all modules are loaded as soon as the applications load, irrespective of which modules are immediately necessary and which are not. &lt;/p&gt;

&lt;h2&gt;
  
  
  Why Lazy-loaded Modules
&lt;/h2&gt;

&lt;p&gt;In the case of applications with many routes, these modules would eventually increase initial load time and consequently bad user experience. To prevent large load time we prefer lazy-loaded modules to minimize initial load time as well as bundle size. Every module is of different sizes as well as the network conditions, which will take different times to load. &lt;em&gt;For a better user experience, showing loader would definitely be a good idea!&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Loader code
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;app.component.html&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;router-outlet&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"loader"&lt;/span&gt; &lt;span class="na"&gt;*ngIf=&lt;/span&gt;&lt;span class="s"&gt;"isLoading"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/router-outlet&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;app.component.css&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nc"&gt;.loader&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;margin-right&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;translateY&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;-50%&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nc"&gt;.loader&lt;/span&gt;&lt;span class="nd"&gt;:after&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;" "&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;50%&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="nb"&gt;solid&lt;/span&gt; &lt;span class="m"&gt;#000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;border-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;#000&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt; &lt;span class="m"&gt;#000&lt;/span&gt; &lt;span class="nb"&gt;transparent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt; &lt;span class="m"&gt;1.2s&lt;/span&gt; &lt;span class="n"&gt;linear&lt;/span&gt; &lt;span class="n"&gt;infinite&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0deg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;360deg&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;app.component.ts&lt;/strong&gt;&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RouteConfigLoadStart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RouteConfigLoadEnd&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;RouterEvent&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@angular/router&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Component&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;selector&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;app-root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;templateUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.html&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;styleUrls&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.component.css&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppComponent&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RouterEvent&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;RouteConfigLoadStart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;RouteConfigLoadEnd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isLoading&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The actual source code is &lt;a href="https://codesandbox.io/s/show-loader-in-lazy-module-in-angular-tb25o" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The Loader part is &lt;br&gt;
&lt;code&gt;&amp;lt;span class="loader" *ngIf="isLoading"&amp;gt;&amp;lt;/span&amp;gt;&lt;/code&gt; which has a condition to show and hide based on &lt;strong&gt;isLoading&lt;/strong&gt; boolean. The last part is app.component.ts where we have added the following code block:&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="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;RouterEvent&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;RouteConfigLoadStart&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;RouteConfigLoadEnd&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isLoading&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we are subscribed to router events and switching &lt;strong&gt;isLoading&lt;/strong&gt; based on &lt;strong&gt;RouteConfigLoadStart&lt;/strong&gt; and &lt;strong&gt;RouteConfigLoadStart&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Hope this would be useful, see you guys soon 👋. &lt;/p&gt;

</description>
      <category>angular</category>
      <category>javascript</category>
      <category>async</category>
      <category>webdev</category>
    </item>
    <item>
      <title>✨ How to Immutably remove property from JavaScript Object</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Wed, 25 Aug 2021 17:48:40 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/immutably-remove-property-from-javascript-object-3kb7</link>
      <guid>https://dev.to/ahmedgmurtaza/immutably-remove-property-from-javascript-object-3kb7</guid>
      <description>&lt;p&gt;Say we want to create a copy of an existing object, reusing most of the properties while dropping few. In order to remove unwanted properties, there are two basic patterns we usually follow.  &lt;/p&gt;

&lt;p&gt;Let say we have the following object to work with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Let obj = {
    Name:'Ahmed Murtaza',
    Email:'ahmed_murtaza@xyz.com',
    twitter:'ahmedgmurtaza',
    fb:'ahmedgmurtaza'
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Old school way
&lt;/h2&gt;

&lt;p&gt;First approach is to use &lt;code&gt;delete&lt;/code&gt; operator, for that we first duplicate the original object and then explicitly delete the unwanted property out of it, here the unwanted property is &lt;code&gt;twitter&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Let obj2 = Object.assign({}, obj);
delete obj2.twitter;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🌟 Using Object destructuring + rest operator:
&lt;/h2&gt;

&lt;p&gt;using this pattern, we isolate the removing property using destructuring format and name the rest of the properties as new object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let { twitter, ...obj2 } = obj;
console.log(obj2); // obj2 does not carries twitter property
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the above approach, we can immutably remove any property out of the object or can pick the one we need while ignoring the rest of the properties.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>angular</category>
    </item>
    <item>
      <title>Introduction to Variables in CSS🔥</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Thu, 22 Jul 2021 17:04:42 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/enjoy-reusability-using-variables-in-css-347c</link>
      <guid>https://dev.to/ahmedgmurtaza/enjoy-reusability-using-variables-in-css-347c</guid>
      <description>&lt;p&gt;Complex websites have very large amount of CSS, often with a lot of repeated values. For instance, we have same color value used in multiple files, searching it globally and replacing would be a big cost, instead we can use CSS variables to store repeated values at one place and reference them where ever it's required. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is :root ?
&lt;/h2&gt;

&lt;p&gt;:root is a &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes" rel="noopener noreferrer"&gt;CSS pseudo-class&lt;/a&gt; which is considered equivalent to &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; in HTML, referring to the entire document. We declare variables inside &lt;code&gt;:root&lt;/code&gt; and can use everywhere. Variables are referred by double hyphens signature &lt;code&gt;--main-color: hotpink&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:root {
  --main-color: hotpink;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Referencing a variable
&lt;/h2&gt;

&lt;p&gt;Once a variable is set in &lt;code&gt;:root&lt;/code&gt;, we can easily access it with in any selector using &lt;code&gt;var&lt;/code&gt; function as below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:root {
   --main-color: orange;
}

.some-other-class {
   background-color: var(--main-color);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Declaring a variable inside &lt;code&gt;:root&lt;/code&gt; simply means we can access the variable within any CSS selector, as &lt;code&gt;:root&lt;/code&gt; is considered to be the top most parent for all selectors, just as &lt;code&gt;&amp;lt;html&amp;gt;&lt;/code&gt; for all other tags in HTML. Similarly, we can define the variable inside any other selectors too:&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- first  element --&amp;gt;
&amp;lt;div class="first-parent"&amp;gt;
    &amp;lt;div class="first-parent__child"&amp;gt;First child text&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;!-- /first  element --&amp;gt;

&amp;lt;!-- second  element --&amp;gt;
&amp;lt;div class="second-parent"&amp;gt;
    &amp;lt;div class="second-parent__child"&amp;gt;Second child text&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;!-- /second  element --&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  CSS
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.first-parent {
  --main-color: orange;
}
.first-parent__child {
  background-color: var(--main-color);
}
.second-parent__child {
  background-color: var(--main-color);  /* this won't work */
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What above shows is that &lt;code&gt;--main-color&lt;/code&gt; can be referenced only within selectors which refers to either &lt;code&gt;first-parent&lt;/code&gt; itself or it's children tags, and not outside &lt;code&gt;first-parent&lt;/code&gt; tag.&lt;/p&gt;

&lt;p&gt;That's it for today 🎉. Would love to hear your thoughts, and do share me how often you embrace the power of CSS variables in your projects. Stay tuned 👋&lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Use Closures for Memory Optimizations in JavaScript (a case study)</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Sat, 10 Jul 2021 18:58:20 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/use-closures-for-memory-optimizations-in-javascript-a-case-study-43h9</link>
      <guid>https://dev.to/ahmedgmurtaza/use-closures-for-memory-optimizations-in-javascript-a-case-study-43h9</guid>
      <description>&lt;p&gt;Beside &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions" rel="noopener noreferrer"&gt;functions&lt;/a&gt; being first class citizens in JavaScript, there are plenty of other features, allowing functions to make an extra mile ride. Closures are one of them. &lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Closure?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment)&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures#lexical_scoping" rel="noopener noreferrer"&gt;Mozilla &lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's take an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function adder(a) {
  return function(b) {
    return a + b;
  };
}

let add5 = adder(5);
let add10 = adder(10);

console.log(add5(2));  // 7
console.log(add10(2)); // 12
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From above code, it's clear how closures work, and how it remembers the lexical scope, the function was declared within. But how this could be useful, or is it just non-practical questions in JavaScript interviews? &lt;/p&gt;

&lt;p&gt;Don't worry, we have got plenty of applications and usage, of Closures throughout JavaScript ecosystem. Today, the usage we want to discuss is &lt;em&gt;how we can optimize memory usage using Closures&lt;/em&gt;. Consider the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiply(y){
    let x = Math.pow(10,10);
    return x* y;
}
multiply(25); //250000000000
multiply(45); //450000000000

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

&lt;/div&gt;



&lt;p&gt;It looks very straight forward, right? ... No, actually if you notice every time the &lt;strong&gt;multiply()&lt;/strong&gt; function is called, the &lt;strong&gt;let x = Math.pow(10,10)&lt;/strong&gt; is recreated and occupy certain memory, in this case quite a large memory for sure, due to the large numeric value it's generating.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Bringing in the Closure
&lt;/h2&gt;

&lt;p&gt;What if we make it possible to create &lt;strong&gt;let x = Math.pow(10,10);&lt;/strong&gt; only once, to the extent where it has been repetitive across the &lt;strong&gt;multiply()&lt;/strong&gt; function calls, this is where the &lt;em&gt;Closures&lt;/em&gt; come into play. Let's take a look at the below modified code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function multiply(){
    let x = Math.pow(10,10);
    return function(y){
        return x* y;
    }
}
let multiplier= multiply();
multiplier(25); //250000000000
multiplier(45); //450000000000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we introduced returning an inner function, which creates a closure, and does not recreate &lt;strong&gt;let x = Math.pow(10,10);&lt;/strong&gt; with each &lt;strong&gt;multiplier()&lt;/strong&gt; call, and hence avoid excess memory leaks. This conveys us, by using &lt;em&gt;Closures&lt;/em&gt; we can easily avoid costly memory jobs. &lt;/p&gt;

&lt;p&gt;So that's it for today. Hope you have benefited from this case study, or Do share if you ever faced similar scenarios and what approaches you have followed. Stay tuned for the next JS hack!😃&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>angular</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Types of Data Binding in Angular</title>
      <dc:creator>Ahmed Murtaza</dc:creator>
      <pubDate>Tue, 06 Jul 2021 09:06:11 +0000</pubDate>
      <link>https://dev.to/ahmedgmurtaza/types-of-data-binding-in-angular-2878</link>
      <guid>https://dev.to/ahmedgmurtaza/types-of-data-binding-in-angular-2878</guid>
      <description>&lt;p&gt;Data binding is the automatic synchronization of data between model and view. In Angular, we have 3 fundamental ways to make to-and-fro binding between view and source.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Source to View (one-way)&lt;/li&gt;
&lt;li&gt;View to Source (one-way)&lt;/li&gt;
&lt;li&gt;View to Source to View (two-way)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Source to View (one-way)
&lt;/h2&gt;

&lt;p&gt;This refers to as &lt;em&gt;Property binding&lt;/em&gt; method, it includes all following patterns to share data from source to view.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interpolation
&lt;/h3&gt;

&lt;p&gt;Interpolation refers to embedding expressions into markup text. By default Angular interpolation uses &lt;code&gt;{{&lt;/code&gt; and &lt;code&gt;}}&lt;/code&gt; to encapsulates the expression.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;p&amp;gt;{{title}}&amp;lt;/p&amp;gt;
&amp;lt;div&amp;gt;&amp;lt;img src="{{itemImageUrl}}"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Property Binding
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;[]&lt;/code&gt; brackets cause Angular to consider right-hand side value as dynamic, without brackets Angular evaluates the right-hand value as static string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img [src]="itemImageUrl"&amp;gt;
&amp;lt;app-item-detail childItem="parentItem"&amp;gt;&amp;lt;/app-item-detail&amp;gt; 

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Attribute, Style and class binding
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Attribute bindings
&lt;/h4&gt;

&lt;p&gt;Apart from element properties, we have element attributes &lt;a href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA" rel="noopener noreferrer"&gt;ARIA&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en-US/docs/Web/SVG" rel="noopener noreferrer"&gt;SVG&lt;/a&gt; for which we can use attribute bindings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button [attr.aria-label]="actionName"&amp;gt;{{actionName}} with Aria&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Class binding
&lt;/h4&gt;

&lt;p&gt;Single class binding&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[class.sale]="onSale"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiple Class bindings&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[class]="'my-class-1 my-class-2 my-class-3'"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Styles binding
&lt;/h4&gt;

&lt;p&gt;Single style binding&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;nav [style.background-color]="expression"&amp;gt;&amp;lt;/nav&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Multiple styles binding&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;nav [style]="stylesExpression"&amp;gt;&amp;lt;/nav&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  View to Source (one-way)
&lt;/h2&gt;

&lt;p&gt;More specifically, we call this &lt;em&gt;Custom Event binding&lt;/em&gt;, utilizing Angular &lt;a href="https://angular.io/api/core/EventEmitter" rel="noopener noreferrer"&gt;Event Emitter&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;Consider the following code showing button which alerts on click. The alert content is passed through &lt;code&gt;&amp;lt;app-button&amp;gt;&lt;/code&gt; component template using custom event &lt;code&gt;showMessage()&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/sparkling-sea-quywp"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  3. View to Source to View (two-way)
&lt;/h2&gt;

&lt;p&gt;Angular two-way data binding is the combination of square brackets &lt;code&gt;[]&lt;/code&gt; and parentheses &lt;code&gt;()&lt;/code&gt;. &lt;code&gt;[()]&lt;/code&gt; refers to property binding followed by event binding.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;app-sizer [(size)]="fontSize"&amp;gt;&amp;lt;/app-sizer&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/friendly-knuth-hp422"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;So, which methods you have already used or would love to use soon, or something else? And Why? Let me know in the comments section. 😃&lt;/p&gt;

&lt;p&gt;See you again in another exciting article. Until then, happy coding! 💻&lt;/p&gt;

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