<?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>Forem: Suryaraj Bhaduri</title>
    <description>The latest articles on Forem by Suryaraj Bhaduri (@suryaraj1).</description>
    <link>https://forem.com/suryaraj1</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%2F587321%2F5e2d6577-042d-4430-b354-16a0ded52477.jpeg</url>
      <title>Forem: Suryaraj Bhaduri</title>
      <link>https://forem.com/suryaraj1</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/suryaraj1"/>
    <language>en</language>
    <item>
      <title>Polyfills in JavaScript 🔧⚙</title>
      <dc:creator>Suryaraj Bhaduri</dc:creator>
      <pubDate>Fri, 14 Jan 2022 18:05:19 +0000</pubDate>
      <link>https://forem.com/suryaraj1/polyfills-in-javascript-13kd</link>
      <guid>https://forem.com/suryaraj1/polyfills-in-javascript-13kd</guid>
      <description>&lt;p&gt;Who doesn't want their web apps to perform flawlessly across all web browsers i.e be &lt;strong&gt;cross-browser compatible&lt;/strong&gt;! But with more and more new features coming to JavaScript, many old browsers now do not have support for all of them.❗💀&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Polyfills&lt;/strong&gt; is the solution to the problem.🌈✨&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic JavaScript (if, else, for-loops, etc.)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; keyword&lt;/li&gt;
&lt;li&gt;Prototype (can read from this &lt;a href="https://www.javascripttutorial.net/javascript-prototype/" rel="noopener noreferrer"&gt;link&lt;/a&gt;)&lt;/li&gt;
&lt;li&gt;Higher order functions e.x &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt;, &lt;code&gt;reduce()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Will to learn 🚀&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;&lt;strong&gt;Polyfill&lt;/strong&gt; is a piece of code that is used to bring support for newer features in older browsers that currently do not have native support for these features.&lt;br&gt;
For example, suppose JavaScript releases a new function, say &lt;code&gt;x&lt;/code&gt; as a part of their language iteration. Now some older browsers might not have support for this function. But we developers would want our apps to be cross-browser compatible. Polyfills help us in making this possible with custom code.👨‍💻&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note📝- To code the polyfill for any feature, one must first be aware of the internal workings and specifications of the same.⚙&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Polyfill Demo 🚀
&lt;/h2&gt;

&lt;p&gt;Enough talk, we need some code now.🤓👨‍💻&lt;/p&gt;

&lt;p&gt;For the sake of this blog we would be coding polyfills of three higher-order functions which were added to JavaScript as a part of ES5, namely &lt;code&gt;map()&lt;/code&gt;, &lt;code&gt;filter()&lt;/code&gt; and &lt;code&gt;reduce()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;❌ &lt;strong&gt;Disclaimer&lt;/strong&gt; - By no means should this be considered a guide to using the aforementioned higher-order functions.&lt;/p&gt;

&lt;p&gt;That being said, take a look at the following image for a refresher of these functions if you're feeling a bit rusty.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F8twv4je0nn21kgli4fia.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F8twv4je0nn21kgli4fia.jpg" alt="Higher Order Functions"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Polyfill for Map
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The &lt;code&gt;map()&lt;/code&gt; method creates a new array populated with the results of calling a provided function on every element in the calling array - MDN Web Docs&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's look at the following code. The code here is for -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;computing squares of numbers of a given array&lt;/li&gt;
&lt;li&gt;alternate casing of characters of a string (if the index is even , the character should be in uppercase else lowercase, e.x. &lt;code&gt;polyfill&lt;/code&gt; =&amp;gt; &lt;code&gt;PoLyFiLl&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fld3ttde02vnr2wogghwl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fld3ttde02vnr2wogghwl.png" alt="Map code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, the fun part🌈!!&lt;br&gt;
Let's code the basic polyfill i.e our custom implementation for the &lt;code&gt;map&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Take a look at the following code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fse9s45ewexdd2ir7wjsv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fse9s45ewexdd2ir7wjsv.png" alt="Basic map polyfill"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉Steps to code the polyfill for &lt;code&gt;map&lt;/code&gt; - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have a clear understanding of how &lt;code&gt;map()&lt;/code&gt; works&lt;/li&gt;
&lt;li&gt;Create a function, which here we have named &lt;code&gt;customMap&lt;/code&gt; and attach it to &lt;code&gt;Array.prototype&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Pass a function as an argument that would be run over every element (this resembles the &lt;code&gt;callback&lt;/code&gt; in &lt;code&gt;map&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Create a new array that is initially empty&lt;/li&gt;
&lt;li&gt;Loop over every element of the array using &lt;code&gt;this&lt;/code&gt; and push every new element to the new array&lt;/li&gt;
&lt;li&gt;Finally return the output array&lt;/li&gt;
&lt;li&gt;Voila!! 🎉🎉 You're ready with your first polyfill!!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's put our &lt;code&gt;customMap&lt;/code&gt; to the test.🚀🚀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Frdykg3iuye4l8nk6acfq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Frdykg3iuye4l8nk6acfq.png" alt="Polyfill map use"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our polyfill passed the test i.e it yields the same results as the original &lt;code&gt;map&lt;/code&gt; function.✅✅&lt;/p&gt;

&lt;p&gt;Next, let's move to the &lt;code&gt;filter&lt;/code&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polyfill for Filter
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The &lt;code&gt;filter()&lt;/code&gt; method creates a new array with all elements that pass the test implemented by the provided function. - MDN Web Docs&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's look at the following code. The code here is for - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;filtering out all odd numbers from a given array of numbers&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fggjhw56imrkj0gj2u4q0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fggjhw56imrkj0gj2u4q0.png" alt="Filter code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, time to code the basic polyfill for the &lt;code&gt;filter&lt;/code&gt; function.🌈&lt;/p&gt;

&lt;p&gt;Look at the following code for better understanding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fy0gntzar4nmdvm6u5q9a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fy0gntzar4nmdvm6u5q9a.png" alt="Basic Filter polyfill"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;👉Steps to code our &lt;code&gt;filter&lt;/code&gt; polyfill - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have a clear understanding of &lt;code&gt;filter()&lt;/code&gt; and how it works&lt;/li&gt;
&lt;li&gt;Create a function, which here we have named &lt;code&gt;customFilter&lt;/code&gt; and attach it to &lt;code&gt;Array.prototype&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Pass a function as an argument that would be run for each element &lt;/li&gt;
&lt;li&gt;Create a new empty array to store the filtered items&lt;/li&gt;
&lt;li&gt;Loop over the array items using &lt;code&gt;this&lt;/code&gt; and push only those items to the new array which satisfy the test&lt;/li&gt;
&lt;li&gt;Finally return the output array containing filtered items&lt;/li&gt;
&lt;li&gt;Superb!!🎉🎉 You have coded your &lt;code&gt;filter&lt;/code&gt; polyfill!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Similar to the above, let's put our &lt;code&gt;customFilter&lt;/code&gt; to the test.😅&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F32lt62lwp8vaddcn53po.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F32lt62lwp8vaddcn53po.png" alt="Filter polyfill run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our polyfill for &lt;code&gt;filter&lt;/code&gt; yields the same result as using the inbuilt &lt;code&gt;filter&lt;/code&gt; function!!✅✅&lt;/p&gt;

&lt;p&gt;We've almost reached the end of the blog.🏁 Next, we would quickly cover the &lt;code&gt;reduce&lt;/code&gt; function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Polyfill for Reduce
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The &lt;code&gt;reduce()&lt;/code&gt; method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value. - MDN Web Docs&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's look at the code below. The code here -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;computes the sum of all elements of an array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.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%2Flp0c4whcvm1pke2ygt9i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Flp0c4whcvm1pke2ygt9i.png" alt="Reduce code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try and write the basic polyfill for &lt;code&gt;reduce&lt;/code&gt; function. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✔Recommended&lt;/strong&gt; - First try on your own and then proceed to the code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fgriiun46mvz9a6ibqmpb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fgriiun46mvz9a6ibqmpb.png" alt="Polyfill for reduce"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's try testing whether our polyfill code works or not!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fhwy94ikqfkom4an6erx1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fhwy94ikqfkom4an6erx1.png" alt="Reduce polyfill run"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome!! 🎉🎉 This also yields the same result as before.&lt;/p&gt;

&lt;p&gt;If you made it this far, pat your back. You're awesome!🌈🚀&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus🦄 - History of Polyfill
&lt;/h2&gt;

&lt;p&gt;The name Polyfill comes from the name of a product used to cover cracks and holes on a wall, called &lt;em&gt;Polyfilla&lt;/em&gt;. The term was coined by Remy Sharp while writing his book "Introducing HTML5" back in 2009. &lt;/p&gt;

&lt;p&gt;📕Can read more about this - &lt;a href="https://remysharp.com/2010/10/08/what-is-a-polyfill" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Congratulations!!&lt;/strong&gt; 🎉🎉 You made it till the end. If you're still here chances are you probably liked the blog. &lt;/p&gt;

&lt;p&gt;I am still learning Polyfills. Hope this blog gave you a decent introduction to what they are.&lt;/p&gt;

&lt;p&gt;Do let me know how you liked the blog and where I need to improve. Would be eagerly waiting for feedback!🙂&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Transpilers vs Compilers⚙</title>
      <dc:creator>Suryaraj Bhaduri</dc:creator>
      <pubDate>Wed, 12 Jan 2022 04:50:31 +0000</pubDate>
      <link>https://forem.com/suryaraj1/transpilers-vs-compilers-3ohj</link>
      <guid>https://forem.com/suryaraj1/transpilers-vs-compilers-3ohj</guid>
      <description>&lt;p&gt;When it comes to &lt;strong&gt;transpilers&lt;/strong&gt; vs &lt;strong&gt;compilers&lt;/strong&gt;, there is a world of difference between the two. As a software engineer, it is almost absolutely essential to have a firm idea about what transpilers and compilers are and this blog would help achieve the same.👨‍💻✨&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Will to learn 🚀&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, let's define a few terms before getting to know about transpilers and compilers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstraction
&lt;/h2&gt;

&lt;p&gt;The term &lt;strong&gt;Abstraction&lt;/strong&gt; will come up often in this blog, so let's define it before moving forward.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Abstraction - It refers to presenting the user with only the necessary details instead of all the implementational details.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Example📝&lt;/strong&gt; - We all have used a remote to operate our television sets 📺. Do we know or ever intend to know how the remote works internally while changing channels? 🙄&lt;br&gt;
No right! This is an example of abstraction in real life. The remote provides us its interface beyond which we don't really need to know anything for successful operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Machine Code
&lt;/h2&gt;

&lt;p&gt;What is machine code? 🤖&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In computer programming, machine code is any low-level programming language, consisting of machine language instructions, which are used to control a computer's central processing unit (CPU) - Wikipedia&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Programming languages of the likes of &lt;strong&gt;Java&lt;/strong&gt;, &lt;strong&gt;Python&lt;/strong&gt;, &lt;strong&gt;JavaScript&lt;/strong&gt; are considered &lt;strong&gt;high-level programming languages&lt;/strong&gt; as they provide a great level of abstraction to the developer. They allow the developer to worry less about intricate details and focus entirely on software development.🌈&lt;/p&gt;

&lt;p&gt;On the other hand, machine code has very little abstraction and is not built to be best suited for development purposes.💀&lt;/p&gt;

&lt;p&gt;Take a look at the following image for better understanding. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fw7x6dhiatfdwg51hvguw.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fw7x6dhiatfdwg51hvguw.jpg" alt="accessibility for programming languages"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, let's move to the main part of the blog.&lt;/p&gt;

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

&lt;p&gt;A &lt;strong&gt;compiler&lt;/strong&gt; is a utility program that takes in our source code and generates executable machine code 🤖. &lt;/p&gt;

&lt;p&gt;There are different types of compilers - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Just in Time Compiler (JIT)&lt;/li&gt;
&lt;li&gt;Incremental Compiler &lt;/li&gt;
&lt;li&gt;Load and Go Compiler&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;and many more.&lt;/p&gt;

&lt;p&gt;Take a look at the following image to get a better understanding of how a compiler works at a higher level.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F4rdxr0gmhjdopyu2oku8.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F4rdxr0gmhjdopyu2oku8.jpg" alt="how compiler works!"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;On one hand, where a compiler takes source code and converts it into a lower-level language in comparison to the source code, a transpiler takes our code and translates it into something similar while keeping the abstraction level the same as before.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example📝&lt;/strong&gt; - For any TypeScript code to run, it has to be first converted to JavaScript and it is done via &lt;strong&gt;transpiling&lt;/strong&gt; the TypeScript source code.&lt;/p&gt;

&lt;p&gt;Also, Babel is another transpiler used to transpile ES6+ JavaScript to more compatible forms suitable for older browsers.&lt;/p&gt;

&lt;p&gt;The following image shows ES6 JavaScript code being transpiled to ES5 code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2F3epic1uwb9yr7t9z52i5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2F3epic1uwb9yr7t9z52i5.jpg" alt="transpiler img"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Transpiling Demo 🚀
&lt;/h2&gt;

&lt;p&gt;Here, we would see how a TypeScript code is transpiled to JavaScript code.&lt;/p&gt;

&lt;h3&gt;
  
  
  TypeScript Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fjbd862s7j8lokceu1j1k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fjbd862s7j8lokceu1j1k.png" alt="TS Code"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To transpile the TypeScript code we use &lt;code&gt;tsc file_name.ts&lt;/code&gt; in the terminal. This creates a JavaScript file containing the transpiled code with the name &lt;code&gt;file_name.js&lt;/code&gt; in the same directory. &lt;/p&gt;

&lt;h3&gt;
  
  
  Transpiled JavaScript Code 🌈
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fwu1y3i0b0r0r1v460h34.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fwu1y3i0b0r0r1v460h34.png" alt="JS Code"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Congratulations!! 🎉🎉 You made it till the end. If you're still here chances are you probably liked the blog.🚀&lt;br&gt;
Here, we merely scratched the surface of transpilers and compilers. &lt;/p&gt;

&lt;p&gt;Do let me know how you liked the blog and where I need to improve. Would be eagerly waiting for feedback!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>typescript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Implement dark mode in React 🌞🌕</title>
      <dc:creator>Suryaraj Bhaduri</dc:creator>
      <pubDate>Sat, 18 Sep 2021 17:59:04 +0000</pubDate>
      <link>https://forem.com/suryaraj1/implement-dark-mode-in-react-4fi8</link>
      <guid>https://forem.com/suryaraj1/implement-dark-mode-in-react-4fi8</guid>
      <description>&lt;p&gt;Who doesn't want a dark theme on their website! Not only does it help us focus better, but also keeps our eyes safer 🤓.&lt;/p&gt;

&lt;p&gt;The blog might add value if you belong to either of these situations -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Maybe you have been in a place wherein you want to implement a dark theme for your website but are struggling around the same 😵. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You are starting out with React, so this might just be another fun exercise for you to try 🎉.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of React and terminologies (state, props, etc.)&lt;/li&gt;
&lt;li&gt;Basic knowledge of CSS&lt;/li&gt;
&lt;li&gt;Will to learn 🚀&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Spin up a new React Project ✨
&lt;/h2&gt;

&lt;p&gt;You could use your local development environment or maybe use a sandbox. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note📝 - If using local setup prefer cleaning up the factory code before moving further.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Also, I will be using class based components for this one - &lt;em&gt;not using React Hooks&lt;/em&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Create a simple page
&lt;/h2&gt;

&lt;p&gt;In our &lt;code&gt;App()&lt;/code&gt;, just return a &lt;code&gt;div&lt;/code&gt; for the time being. (Can use a &lt;code&gt;button&lt;/code&gt; as well)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2F01QinBSQ0fNs.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2F01QinBSQ0fNs.png%3Fo%3D1" alt="Init setup"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Add state to our app
&lt;/h2&gt;

&lt;p&gt;We cannot do much with a &lt;em&gt;dumb component&lt;/em&gt; when it comes to embedding logic inside of it. So we need to add &lt;code&gt;state&lt;/code&gt; to the React component.&lt;/p&gt;

&lt;p&gt;Add a state called &lt;code&gt;isDarkThemeEnabled&lt;/code&gt; to the &lt;code&gt;App&lt;/code&gt; component and set it to &lt;code&gt;false&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2Fag3pVgfVXhTE.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2Fag3pVgfVXhTE.png%3Fo%3D1" alt="Adding state"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Creating the theme switcher logic 💡
&lt;/h2&gt;

&lt;p&gt;This is the crux of the entire problem. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;How to build the theme switcher / toggler ?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This might become simple, if you try to draw an analogy to a light bulb. A light bulb has two states &lt;code&gt;on&lt;/code&gt; and &lt;code&gt;off&lt;/code&gt;. &lt;br&gt;
At any given point of time it is in one of the either states.&lt;/p&gt;

&lt;p&gt;Now, imagine the theme switcher to be a bulb and try to map the idea.&lt;/p&gt;

&lt;p&gt;You got it! 🎉 If the theme switcher is imagined to be a light bulb, &lt;code&gt;light mode&lt;/code&gt; becomes &lt;code&gt;on&lt;/code&gt; and &lt;code&gt;dark mode&lt;/code&gt; becomes &lt;code&gt;off&lt;/code&gt; or vice-versa.&lt;/p&gt;

&lt;p&gt;Now, how to toggle the state ? Pretty simple, just flip the current state whenever a certain event fires.&lt;br&gt;
In simpler terms - on clicking a button change the state value from &lt;code&gt;true&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; or vice versa.&lt;/p&gt;
&lt;h2&gt;
  
  
  Implementing the theme switcher logic
&lt;/h2&gt;

&lt;p&gt;Now, as we have designed the approach it's good time to start coding up the idea. &lt;br&gt;
FYI for changing state in React we use the &lt;code&gt;setState&lt;/code&gt; function. You can read about the &lt;code&gt;setState&lt;/code&gt; API from &lt;a href="https://reactjs.org/docs/react-component.html#setstate" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's create a &lt;code&gt;clickHandler&lt;/code&gt; and embed the state-toggler logic inside of it. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FD1Tgr6rNzzYs.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FD1Tgr6rNzzYs.png%3Fo%3D1" alt="Click handler"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Pass this &lt;code&gt;clickHandler&lt;/code&gt; to the &lt;code&gt;onClick&lt;/code&gt; prop of the div, to trigger the function whenever user clicks on the div.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FJRBqnvVocHHr.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FJRBqnvVocHHr.png%3Fo%3D1" alt="clickHandler passed to prop"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Adding SVG to our app
&lt;/h2&gt;

&lt;p&gt;I want to let the user know which mode he can currently switch to. Can be accomplished with text, but what better than using SVG icons to enhance the UI !🎉&lt;/p&gt;

&lt;p&gt;I will be using &lt;a href="https://heroicons.com/" rel="noopener noreferrer"&gt;heroicons&lt;/a&gt; for high quality SVG icons.&lt;/p&gt;

&lt;p&gt;Create two files in your &lt;code&gt;/src&lt;/code&gt; directory, for two SVG icons for the two states. I will be using a Moon and a Sun SVG in this case.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2F5dvXgTl8N2Sb.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2F5dvXgTl8N2Sb.png%3Fo%3D1" alt="folder structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use any SVG in our React app, we can directly use them in our code or import the SVG as a React Component. I prefer the latter as it helps to keep the code cleaner.&lt;/p&gt;

&lt;p&gt;This is how we import an SVG icon as a React component -&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;ReactComponent&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Moon&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;./moon.svg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to toggle the SVG icons as well to suit our purpose. For this we will use &lt;code&gt;conditional rendering&lt;/code&gt;. &lt;br&gt;
This is a gem 💎. You can read about this &lt;a href="https://reactjs.org/docs/conditional-rendering.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The idea is, whenever &lt;code&gt;isDarkThemeEnabled&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; we need the &lt;code&gt;sun&lt;/code&gt; SVG to show up else the &lt;code&gt;moon&lt;/code&gt; SVG should show up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FEHAchyNyzNeh.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FEHAchyNyzNeh.png%3Fo%3D1" alt="conditional render"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is how the app looks right now. You're almost there.🏁&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FYBGzbUgmiLwfZ8btYR%2Fgiphy.gif%3Fcid%3D790b76119a759f7e2e69f43d496b14312912450918b94cdf%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia.giphy.com%2Fmedia%2FYBGzbUgmiLwfZ8btYR%2Fgiphy.gif%3Fcid%3D790b76119a759f7e2e69f43d496b14312912450918b94cdf%26rid%3Dgiphy.gif%26ct%3Dg" alt="demo GIF"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Let's style up our app
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;dark mode&lt;/code&gt; is enabled we would want to paint our app background dark. So, for this we would prefer applying &lt;code&gt;classNames conditionally&lt;/code&gt;.&lt;br&gt;
You can read on this from &lt;a href="https://www.pluralsight.com/guides/applying-classes-conditionally-react" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;We would be using &lt;code&gt;JSX expression&lt;/code&gt; and &lt;code&gt;Template Literals&lt;/code&gt; for this. When &lt;code&gt;isDarkThemeEnabled&lt;/code&gt; is set to &lt;code&gt;true&lt;/code&gt;, which means &lt;code&gt;dark mode&lt;/code&gt; is enabled, we would append a &lt;code&gt;className&lt;/code&gt; to the &lt;code&gt;App&lt;/code&gt; className.&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;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`App &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;isDarkThemeEnabled&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark-mode&lt;/span&gt;&lt;span class="dl"&gt;"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets add some style for the &lt;code&gt;.App.dark-mode&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FAYcZa5vYeBd1.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FAYcZa5vYeBd1.png%3Fo%3D1" alt="CSS for App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's also style up the SVG icons 💅. Add a className of &lt;code&gt;moon&lt;/code&gt; and &lt;code&gt;sun&lt;/code&gt; to &lt;code&gt;&amp;lt;Moon /&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Sun /&amp;gt;&lt;/code&gt; components respectively.&lt;/p&gt;

&lt;p&gt;This is how the App code looks right now.&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&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="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&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;ReactComponent&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Moon&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="s2"&gt;./moon.svg&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;ReactComponent&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Sun&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="s2"&gt;./sun.svg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&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="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&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;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;isDarkThemeEnabled&lt;/span&gt;&lt;span class="p"&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="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;isDarkThemeEnabled&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&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;state&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="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;isDarkThemeEnabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isDarkThemeEnabled&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="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;isDarkThemeEnabled&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&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;state&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;`App &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;isDarkThemeEnabled&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dark-mode&lt;/span&gt;&lt;span class="dl"&gt;"&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="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;toggler-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&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;handleClick&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="nx"&gt;isDarkThemeEnabled&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Sun&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;sun&lt;/span&gt;&lt;span class="dl"&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="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Moon&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;moon&lt;/span&gt;&lt;span class="dl"&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Add desired styles in the &lt;code&gt;styles.css&lt;/code&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FEG3sEjQT2J30.png%3Fo%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgcdn.pbrd.co%2Fimages%2FEG3sEjQT2J30.png%3Fo%3D1" alt="styles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;✨ Add a transition time to the wrapper class to make the color switch smoother. I added &lt;code&gt;transition: 0.3s ease&lt;/code&gt; to &lt;code&gt;.App&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This is how the App looks finally!&lt;br&gt;
&lt;strong&gt;Note&lt;/strong&gt; - I have added some additional styles and animations.&lt;/p&gt;

&lt;p&gt;For a larger websites use &lt;code&gt;css variables&lt;/code&gt; to make painting UI components on theme toggle easier.&lt;/p&gt;

&lt;p&gt;Feel free to play around with the sandbox.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/competent-snowflake-d8ox5"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Congratulations!!&lt;/strong&gt; 🎉🎉 You made it till the end. If you're still here chances are you probably liked the blog. &lt;/p&gt;

&lt;p&gt;You can also make improvisations to this simple project and share them. This would help in getting the fundamentals stronger 🏋 🚀.&lt;/p&gt;

&lt;p&gt;Do let me know how you liked the blog and where I need to improve. Would be eagerly waiting for feedback!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>react</category>
      <category>css</category>
    </item>
    <item>
      <title>Health for Software Engineers🤾</title>
      <dc:creator>Suryaraj Bhaduri</dc:creator>
      <pubDate>Thu, 09 Sep 2021 19:01:12 +0000</pubDate>
      <link>https://forem.com/suryaraj1/software-development-and-health-45pc</link>
      <guid>https://forem.com/suryaraj1/software-development-and-health-45pc</guid>
      <description>&lt;p&gt;There's this common saying - &lt;em&gt;"health is wealth"&lt;/em&gt;, a proverb often forgotten in modern world. As the world is picking pace and moving faster than ever before, our health is what turns out to be most neglected 😞. &lt;/p&gt;

&lt;p&gt;Now coming to us developers, most us might not prefer to speak on boring topics like health, and quite similarly the software world is very famous for its sedentary lifestyle.&lt;/p&gt;

&lt;p&gt;Many software engineers have complained of severe back pain and other issues till they realised that health is something which is important as well.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tips for better health ✅
&lt;/h2&gt;

&lt;p&gt;If you are a software engineer, these following suggestions might gift you a better health and a better life. 🎉&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get Moving🚶‍♂️&lt;/strong&gt;&lt;br&gt;
This is probably the most simple yet the most effective decision you as a developer could take for your health. &lt;/p&gt;

&lt;p&gt;After sitting on your chair for hours you might feel a strange dip in your focus and energy levels. A simple walk for 30 mins outside will bring back the focus and refresh your energy levels🔋. &lt;/p&gt;

&lt;p&gt;Who knows you might even get an idea for the bug long left unsolved 🤯!&lt;/p&gt;

&lt;p&gt;Human body was not meant to sit for hours in front of a computer. So try getting up and moving around every now and then and I cannot stress this enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Drink Water 🚰&lt;/strong&gt;&lt;br&gt;
This is one of the most underrated options. You need to keep yourself hydrated, so consider sipping on a glass of water every now and then. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Up to 60% of the human adult body is water&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, do make sure to keep yourself hydrated 🥤.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Workout🏃‍♂️&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I agree its hard to find time to workout on a regular basis. But lack of physical activity is a silent killer🔪.&lt;/p&gt;

&lt;p&gt;Make a determination to workout 3-4 times a week as this will only help you stay healthy in the long run. And better health correlates to better focus 🧠 which equals better software development (if you're a developer).&lt;/p&gt;

&lt;p&gt;If its hard for you to find time to hit the gym, no worries, I am attaching some links to workout sessions which I personally find very helpful.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=edIK5SZYMZo"&gt;Replace Treadmill With This 10 Min HIIT/CARDIO Workout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=KxKJEIqS1HQ&amp;amp;t=872s"&gt;20-Minute Interval Run Workout&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=xcG8GnWwGfo&amp;amp;t=11s"&gt;Running In Place Workout At Home - Lose Weight Fast&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even 20 mins of home workout, 3 / 4 times a week is way better than weeks of no activity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Nutrition 🥦&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Proper nutrition plays a vital role in your health. Instead of eating out prefer food made at home, and prefer fruits and vegetables🍎 over junks 🍔. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sleep 😴&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Probably the most important of all. A good night's sleep can give you a full day's energy and to take on the nasty bug that has been giving you sleepless nights for weeks.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Sleep is so underrated ✅.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These were some of the changes you as a developer can make in your lifestyle that may not show immediate results but more often than not will help you in the long run.🎉&lt;/p&gt;

&lt;p&gt;P.S - Would be great if you let me know in comments where I went wrong or have missed something 😀.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Tutorial Hell 101✅✅</title>
      <dc:creator>Suryaraj Bhaduri</dc:creator>
      <pubDate>Tue, 07 Sep 2021 18:32:41 +0000</pubDate>
      <link>https://forem.com/suryaraj1/tutorial-hell-101-8bc</link>
      <guid>https://forem.com/suryaraj1/tutorial-hell-101-8bc</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Tutorial Hell🔥?&lt;/strong&gt;&lt;br&gt;
Well this is a situation in which the learner always feels incompetent or scared to implement things out on their own and relies heavily on tutorials.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How does one land up in Tutorial Hell?🤔&lt;/strong&gt;&lt;br&gt;
There's no one answer to this, but usually this situation is common in beginners or people trying to master a new skill. At that point they need quick explanation and depend on video tutorials which is a good idea.But over consumption of tutorials leads to a form of dependency which becomes hard to overcome in future.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How do I know if I am in Tutorial Hell or not?😨&lt;/strong&gt;&lt;br&gt;
These are some of the situations that might be an indicator of the same - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You find yourself busy watching video tutorials most of the time&lt;/li&gt;
&lt;li&gt;You think you will not be able to code without an instructor's guidance&lt;/li&gt;
&lt;li&gt;You keep course shopping or spend time on course websites thinking of getting that one course that might teach you the topic the best way possible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Looks like I am in hell, any idea how to move out of it?🏃&lt;/strong&gt;&lt;br&gt;
No issues. Firstly don't panic and get yourself a glass of water. Now showing you the way out of this place. Well there's only one way I know of which is - &lt;em&gt;"start doing things on your own"&lt;/em&gt;.You will make mistakes, and this might seem very hard at the beginning and even at later stages, but the key 🔑 is to never,ever give up.Am I saying you should never watch tutorials? Absolutely not! You should always go for tutorials whenever you feel like, but its just that the frequency should be lesser than what it was previously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How I got myself out of the hell?&lt;/strong&gt;&lt;br&gt;
Like many of you budding developers out there, I was in the same place. Was finding it very hard to get out of the same. I was trying to learn front-end web development. Although I had previous knowledge of computer programming, still I faced lot of issues and fear in initial days. Gradually I made up my mind to take on projects on my own and started with the simplest of projects. This helped🎉.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important Note👈&lt;/strong&gt;&lt;br&gt;
If you are in the process of moving out of tutorial hell, you are basically trying to move out of your comfort zone which can be extremely hard. Make sure to not over stress yourself ,take ample rest, drink water, workout and rejuvenate yourself from time to time. This might help make the process a little easier.&lt;/p&gt;

&lt;p&gt;P.S - This is my first ever blog.There are surely tons of things I missed and many places I went wrong. Do let me know in the comments.😄&lt;/p&gt;

</description>
      <category>programming</category>
      <category>codenewbie</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
