<?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: Kedar</title>
    <description>The latest articles on Forem by Kedar (@kedar9).</description>
    <link>https://forem.com/kedar9</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%2F147901%2Fdfbbce0b-e1e9-42da-98b3-1f674652c524.jpg</url>
      <title>Forem: Kedar</title>
      <link>https://forem.com/kedar9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/kedar9"/>
    <language>en</language>
    <item>
      <title>JavaScript apply, call &amp; bind Simplified</title>
      <dc:creator>Kedar</dc:creator>
      <pubDate>Sun, 04 Oct 2020 22:25:15 +0000</pubDate>
      <link>https://forem.com/kedar9/javascript-apply-call-bind-simplified-2pbi</link>
      <guid>https://forem.com/kedar9/javascript-apply-call-bind-simplified-2pbi</guid>
      <description>&lt;p&gt;One of the aspects of mastering scope and the value of &lt;code&gt;this&lt;/code&gt; in JavaScript is to understand how &lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt; work.&lt;br&gt;
This article aims at simplifying these methods. I'll try to keep the explanation as simple as possible here.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this&lt;/code&gt; in JS refers refers to the &lt;strong&gt;current context&lt;/strong&gt; in which a function is being called. Although covering &lt;code&gt;this&lt;/code&gt; in detail is a separate article in itself, the main point to remember is that to determine the &lt;code&gt;this&lt;/code&gt; object, we need to see where the function is being invoked.&lt;/p&gt;

&lt;p&gt;Amongst several possibilities of &lt;code&gt;this&lt;/code&gt;, we discuss three of the ways JS allows us to set what &lt;code&gt;this&lt;/code&gt; will be for a function.&lt;/p&gt;



&lt;p&gt;To call a function in JS (and most other languages), we simply pass the parameters and invoke it. Example:&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="nx"&gt;logMe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;logMe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Canada&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// Output: 'Canada'&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;When &lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt; come in the picture, it allows us to also specify the &lt;code&gt;this&lt;/code&gt; object for the function.&lt;br&gt;
So, these methods allow us to invoke/call a JS function by specifying:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;strong&gt;scope&lt;/strong&gt; (&lt;code&gt;this&lt;/code&gt; object) and &lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;params&lt;/strong&gt;
for the function we want to call.&lt;/li&gt;
&lt;/ul&gt;


&lt;h2&gt;
  
  
  ☎️ &lt;code&gt;apply&lt;/code&gt; and &lt;code&gt;call&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Both of these methods are similar. &lt;br&gt;
When being used, they both take the &lt;strong&gt;scope&lt;/strong&gt; or the &lt;strong&gt;&lt;code&gt;this&lt;/code&gt; object&lt;/strong&gt; as the first param followed by the parameters/arguments for the function.&lt;/p&gt;

&lt;p&gt;The only difference is the way the arguments for the function are passed.&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="nx"&gt;invokeMe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Note that the first `val` comes from the "this" object&lt;/span&gt;
  &lt;span class="c1"&gt;// that the function is referring to&lt;/span&gt;
  &lt;span class="c1"&gt;// and `val1` and `val2` are the function arguments. &lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&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="nx"&gt;val1&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="nx"&gt;val2&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;thisObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&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;arg1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&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;arg2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;invokeMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thisObj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 'JavaScript Hello World'&lt;/span&gt;

&lt;span class="nx"&gt;invokeMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thisObj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 'JavaScript Hello World'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;apply&lt;/code&gt; takes the arguments to be passed in a single array.&lt;br&gt;
&lt;code&gt;call&lt;/code&gt; takes the arguments to be passed explicitly.&lt;/p&gt;

&lt;p&gt;A useful mnemonic I found &lt;a href="https://stackoverflow.com/a/1986909"&gt;here&lt;/a&gt; is &lt;br&gt;
&lt;strong&gt;"a for array and c for comma."&lt;/strong&gt;&lt;/p&gt;


&lt;h2&gt;
  
  
  🤝 &lt;code&gt;bind&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;The method &lt;code&gt;bind&lt;/code&gt; also has the same calling syntax like &lt;code&gt;call&lt;/code&gt; but, the significant difference is that&lt;br&gt;
&lt;code&gt;call&lt;/code&gt; calls the function immediately and,&lt;br&gt;
&lt;code&gt;bind&lt;/code&gt; only binds the function and creates a new function that can be called later.&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="nx"&gt;invokeMe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val2&lt;/span&gt;&lt;span class="p"&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="nx"&gt;log&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&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="nx"&gt;val1&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="nx"&gt;val2&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;thisObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&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;arg1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&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;arg2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Binding the function ⬇&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bind1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;invokeMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thisObj&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// No Output Yet&lt;/span&gt;
&lt;span class="c1"&gt;// Invoking the function ⬇&lt;/span&gt;
&lt;span class="nx"&gt;bind1&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: 'JavaScript Hello World'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To better understand &lt;code&gt;bind&lt;/code&gt;, focus on the fact that it is for binding a function with a &lt;code&gt;this&lt;/code&gt; scope and not for immediate invocation. &lt;br&gt;
This means that the arguments that need to be passed are not &lt;strong&gt;required&lt;/strong&gt; when binding the function. They can be passed when invoking the function (usual programming style).&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="nx"&gt;invokeMe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;val2&lt;/span&gt;&lt;span class="p"&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="nx"&gt;log&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;val&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="nx"&gt;val1&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="nx"&gt;val2&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;thisObj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;JavaScript&lt;/span&gt;&lt;span class="dl"&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;arg1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello&lt;/span&gt;&lt;span class="dl"&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;arg2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;World&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Binding the function ⬇&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bind2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;invokeMe&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thisObj&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// No Output Yet&lt;/span&gt;
&lt;span class="c1"&gt;// Invoking the function ⬇&lt;/span&gt;
&lt;span class="nx"&gt;bind2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arg1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;arg2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Output: 'JavaScript Hello World'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. That covers the key usage guidelines for &lt;code&gt;apply&lt;/code&gt;, &lt;code&gt;call&lt;/code&gt; and &lt;code&gt;bind&lt;/code&gt;. The explanation helps us understand that in OOPS, these methods help us reuse a single function for different objects without having to rewrite it for a new scope object.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>scope</category>
      <category>this</category>
      <category>object</category>
    </item>
    <item>
      <title>JavaScript Closures: A Simple Explanation.</title>
      <dc:creator>Kedar</dc:creator>
      <pubDate>Sat, 20 Jul 2019 20:47:36 +0000</pubDate>
      <link>https://forem.com/kedar9/closures-a-simple-explanation-1h0g</link>
      <guid>https://forem.com/kedar9/closures-a-simple-explanation-1h0g</guid>
      <description>&lt;p&gt;Without any fancy intro, let's jump directly to what &lt;code&gt;closure&lt;/code&gt; is.&lt;/p&gt;

&lt;p&gt;Simply put, &lt;strong&gt;Closure is an inner function that remembers the environment it was created in&lt;/strong&gt;.&lt;br&gt;
Think of it like an &lt;em&gt;aware&lt;/em&gt; function has access to the values (and parameters) from an outer function.  &lt;/p&gt;

&lt;p&gt;What makes a closure powerful is that it is capable of reading and manipulating the data of its outer functions.&lt;/p&gt;

&lt;p&gt;Here's a very simple example of a closure.&lt;br&gt;
Think of it as a code from an app. Our goal is to prompt the user to rate the app on every 3rd visit.&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="nx"&gt;promptRating&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;appUsage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;appUsagae&lt;/span&gt;&lt;span class="o"&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;appUsage&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please rate the app.&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;promptRating&lt;/code&gt; is a function that returns an inner function. This inner function is a closure. It remembers and has access to the variable &lt;code&gt;appUsage&lt;/code&gt; from the outer function.&lt;/p&gt;

&lt;p&gt;To see it in action:&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="c1"&gt;// Init the outer function&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;prompt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;promptRating&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Call `prompt` in regular workflow.&lt;/span&gt;
&lt;span class="c1"&gt;// If this is a multiple-of-3 visit, the user will be prompted to rate the app.&lt;/span&gt;
&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// No Output&lt;/span&gt;
&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// No Output&lt;/span&gt;
&lt;span class="nx"&gt;prompt&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Output: Please rate the app.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Being so simple yet powerful has its trade-offs: most notably when creating closures inside loops. Remember that closures have access to the data of the outer function.&lt;br&gt;
So in a loop based on &lt;code&gt;i&lt;/code&gt;, the code inside the closure will execute based on the &lt;strong&gt;current&lt;/strong&gt; value of &lt;code&gt;i&lt;/code&gt;. Not the old value of &lt;code&gt;i&lt;/code&gt; which existed when the closure was created.&lt;br&gt;
Here's a simple code to explain this:&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="nx"&gt;arrayOfNums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;// Closure being pushed into the output array:&lt;/span&gt;
    &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&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;i&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;return&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arrayOfNums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt; &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt; &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]();&lt;/span&gt; &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Another gotcha instance would be creating closures inside a timeout/interval. When run, the code inside the closure will execute based on the &lt;strong&gt;current&lt;/strong&gt; data of the outer function. The values of this data may have gone stale before the timeout was met.&lt;/p&gt;

&lt;p&gt;Here's a simple code to explain this:&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="nx"&gt;countdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;upto&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;upto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&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;// Closure set to run after 1000ms&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&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="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="mi"&gt;1000&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;countdown&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;// Output: 5 5 5 5 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In conclusion, closures are simple beings. It is always an inner function which has access to the outer function scope.&lt;br&gt;
If the outer function is called multiple times, every call creates a new closure. Closure's existence is dependent on their parent function's existence. Like I said, simple beings.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>closure</category>
      <category>code</category>
      <category>interview</category>
    </item>
    <item>
      <title>CSS-Only Multi-Color Backgrounds</title>
      <dc:creator>Kedar</dc:creator>
      <pubDate>Fri, 21 Jun 2019 18:21:51 +0000</pubDate>
      <link>https://forem.com/kedar9/css-only-multi-color-backgrounds-paj</link>
      <guid>https://forem.com/kedar9/css-only-multi-color-backgrounds-paj</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxfgfzqhr3su6s7nra9px.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxfgfzqhr3su6s7nra9px.png" width="800" height="241"&gt;&lt;/a&gt;&lt;br&gt;
The Metro design UI brought by Microsoft with Windows 8 may not have been largely received with a positive response (mostly because of the major UI overhaul from Windows 7), but it opened the doors to a new era of User Interface. Other than introducing a beautiful implementation of flat design, thin typography, and expansive images, it brought what I believe was lacking in the traditional Skeuomorph design: &lt;strong&gt;colorfulness&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;iOS and Android then updated their design standards with iOS 7 and Material design respectively. Both iOS and Material designs also went for flat designs and thin typography: Helvetica Neue (later San Francisco) and Roboto respectively. And both, in their design overhauls, &lt;em&gt;introduced&lt;/em&gt; colors and gradients like they were unheard of.&lt;/p&gt;

&lt;p&gt;So now that we've established that vibrant colors play a major role in all modern designs today, let's check out a different implementation to the traditional use of CSS background gradients.&lt;/p&gt;

&lt;p&gt;In this implementation, we rely on the color-stops parameter in the &lt;code&gt;linear-gradient()&lt;/code&gt; (or &lt;code&gt;radial-gradient&lt;/code&gt;) property. These stops decide where you want to start a particular color. The boilerplate for the property we'll be using is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;background: linear-gradient(&amp;lt;angle&amp;gt;, color1 color1-stop-at, color2 color2-start-at);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The simple logic here is that we stop the first color at an &lt;strong&gt;&lt;em&gt;x%&lt;/em&gt;&lt;/strong&gt; and start the second color at &lt;strong&gt;&lt;em&gt;x%&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;&amp;lt;x%&lt;/em&gt;&lt;/strong&gt;. This removes the "gradient" from the linear-gradient and defines a clear edge between both colors giving the background a &lt;strong&gt;multi-colored-material&lt;/strong&gt; effect.&lt;/p&gt;

&lt;p&gt;In Action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;background: linear-gradient(110deg, #fdcd3b 60%, #ffed4b 60%);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;gets us:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gyl1hx55guwf8kzo8px.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0gyl1hx55guwf8kzo8px.png" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks beautiful, doesn't it? Add some text on it and it looks like a nice highlighted element. The above example has some extra CSS style thrown in (like &lt;code&gt;border-radius&lt;/code&gt; and &lt;code&gt;box-shadow&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Essentially, the background property can &lt;strong&gt;&lt;em&gt;hold&lt;/em&gt;&lt;/strong&gt; two contents one over the other: 2 gradients, 2 images or you can mix-and-match any of those.&lt;br&gt;
To use more than 2 colors (because why not), put the powerful &lt;code&gt;rgba()&lt;/code&gt; to use and use 2 gradient parameters.&lt;br&gt;
Here's an example with the background property holding an image and masking some portion of the image with a gradient of orange and transparent.&lt;/p&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;Use a dominant color from the image or a completely contrast color for the rich look.&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Like this:&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1vy2e2qcjq1huook2l3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1vy2e2qcjq1huook2l3d.png" width="800" height="328"&gt;&lt;/a&gt;&lt;br&gt;
Sample code for such a banner would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;background: linear-gradient(-70deg, #fa7c30 30%, rgba(0, 0, 0, 0) 30%), url('planets.jpg');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's a codepen with some examples (including the use of radial-gradient).&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/kedar/embed/MpXgoR?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>css</category>
      <category>ui</category>
      <category>ux</category>
      <category>design</category>
    </item>
    <item>
      <title>Re-creating "Google Pixel 3a" Website's Camera Features Demo</title>
      <dc:creator>Kedar</dc:creator>
      <pubDate>Tue, 14 May 2019 20:16:36 +0000</pubDate>
      <link>https://forem.com/kedar9/re-creating-google-pixel-3a-website-s-camera-features-demo-3pk0</link>
      <guid>https://forem.com/kedar9/re-creating-google-pixel-3a-website-s-camera-features-demo-3pk0</guid>
      <description>&lt;p&gt;Google launched Pixel 3a last week. On its &lt;a href="https://store.google.com/product/pixel_3a" rel="noopener noreferrer"&gt;website&lt;/a&gt;, it brilliantly demos its camera's features: Depth Editor and Color Pop.&lt;/p&gt;

&lt;h6&gt;
  
  
  Depth Editor (change the background blur/ bokeh):&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjzy0tfq436vw5xchxwiy.gif"&gt;
&lt;/h6&gt;

&lt;h6&gt;
  
  
  Color Pop Demo (change the background color to black &amp;amp; white)&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F4txs6c0ek24tez484y4y.gif"&gt;
&lt;/h6&gt;

&lt;p&gt;Pixel camera is undoubtedly amazing. And its demo on the website is intriguing.&lt;br&gt;
You can play with the slider under the pictures. Increase or decrease the effect. It's pretty cool.&lt;/p&gt;
&lt;h4&gt;
  
  
  (Re)Creating that demo:
&lt;/h4&gt;

&lt;p&gt;Let's do the Background-blur or Bokeh effect. I got &lt;a href="https://unsplash.com/photos/470eBDOc8bk" rel="noopener noreferrer"&gt;this&lt;/a&gt; amazing image from Unsplash.&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%2Fi.imgur.com%2FdFIWe48.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%2Fi.imgur.com%2FdFIWe48.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: This is not a how-to coding article on actually blurring the background or changing the background saturation dynamically. That is not what Google is doing on the website either.&lt;/em&gt;&lt;/p&gt;
&lt;h4&gt;
  
  
  Here's how it's done (And how Google is doing it):
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Two&lt;/strong&gt; images: &lt;strong&gt;One&lt;/strong&gt; with no effect applied, the &lt;strong&gt;other&lt;/strong&gt; with maximum effect applied. &lt;strong&gt;One image on top of the other&lt;/strong&gt;. The slider increases/decreases the &lt;code&gt;opacity&lt;/code&gt; of the image on the top.&lt;br&gt;
Smart technique!&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpxk4qfp4mm8mj31870lt.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fpxk4qfp4mm8mj31870lt.jpg"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Here's how to do it:
&lt;/h3&gt;
&lt;h4&gt;
  
  
  Overlapping
&lt;/h4&gt;

&lt;p&gt;The image from Unsplash would be the &lt;strong&gt;no effect applied&lt;/strong&gt; version of our image (version-1).&lt;br&gt;
I blurred the background for that image in Photoshop. (Tip: You can also do this in basic Image Editing apps like &lt;em&gt;Snapseed&lt;/em&gt;).&lt;br&gt;
The edited image with the background blurred would be the &lt;strong&gt;maximum effect applied&lt;/strong&gt; version (version-2).&lt;br&gt;
To stack the images, we simply need to specify &lt;code&gt;position: absolute&lt;/code&gt; and same position attributes (&lt;code&gt;top&lt;/code&gt;, &lt;code&gt;left&lt;/code&gt; ...) for both the images.&lt;/p&gt;

&lt;p&gt;So, in HTML:&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;div class="images"&amp;gt;
    &amp;lt;img src="//version-1"&amp;gt;
    &amp;lt;img id="blur-img" class="blur-img" src="//version-2"&amp;gt;
  &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And in CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  img {
    width: 300px;
    height: auto;
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
  }
  .blur-img {
    opacity: 0;
  }

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

&lt;/div&gt;



&lt;p&gt;Now the image with &lt;code&gt;id="blur-img"&lt;/code&gt; (specified in HTML) will be placed exactly on top of the version-1 image. And we set the &lt;code&gt;opacity&lt;/code&gt; of the version-2 image (the blurred one) to &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
So, the only image visible would be version-1 (the original one).&lt;/p&gt;
&lt;h4&gt;
  
  
  Sliding through
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;input&lt;/code&gt; HTML element with &lt;code&gt;type="range"&lt;/code&gt; acts as a slider. It takes in &lt;code&gt;min&lt;/code&gt; and &lt;code&gt;max&lt;/code&gt; values. For our convenience, we set the min to &lt;code&gt;0&lt;/code&gt; and max to &lt;code&gt;100&lt;/code&gt;.&lt;br&gt;
With JS, we need to &lt;strong&gt;get&lt;/strong&gt; the value of the range input. &lt;br&gt;
Based on the value of the range input, the opacity of the top image (&lt;code&gt;id="blur-img"&lt;/code&gt;) will go from &lt;code&gt;0&lt;/code&gt; (hidden) to &lt;code&gt;1&lt;/code&gt; (visible).&lt;/p&gt;

&lt;p&gt;So, in JS, add a method to handle that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const changeBlur = (value) =&amp;gt; {
  document.getElementById("blur-img").style.opacity = value/100;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;We divide the &lt;code&gt;value&lt;/code&gt; by &lt;code&gt;100&lt;/code&gt; because the range input will return values between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;100&lt;/code&gt;, and the opacity of the image needs to range from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;1&lt;/code&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And in HTML, add the range input:&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;input class="slider" type="range" min="0" max="100" value="0"
oninput="changeBlur(this.value)"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🎉 That's pretty much it. It's very few lines of code, but the effect is exceptional.&lt;/p&gt;

&lt;p&gt;Here's the CodePen demo for the Background Blur effect:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/kedar/embed/WBrgdQ?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Here's the CodePen demo for the Color Pop effect:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/kedar/embed/RmKbyY?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

</description>
      <category>google</category>
      <category>css</category>
      <category>javascript</category>
      <category>html</category>
    </item>
    <item>
      <title>Creating a Node app with React, Webpack 4, Babel 7, Express and Sass</title>
      <dc:creator>Kedar</dc:creator>
      <pubDate>Fri, 10 May 2019 18:35:55 +0000</pubDate>
      <link>https://forem.com/kedar9/creating-a-node-app-with-react-webpack-4-babel-7-express-and-sass-3mae</link>
      <guid>https://forem.com/kedar9/creating-a-node-app-with-react-webpack-4-babel-7-express-and-sass-3mae</guid>
      <description>&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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrcn8xq3e7h2cmx54qjq.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%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fjrcn8xq3e7h2cmx54qjq.jpg" alt="By your powers combined..."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  🏁 Prologue
&lt;/h2&gt;

&lt;p&gt;🆕 Make a new directory. Let's call it react-boilerplate.&lt;br&gt;
&lt;code&gt;mkdir react-boilerplate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And &lt;em&gt;cd&lt;/em&gt; into it.&lt;br&gt;
&lt;code&gt;cd react-boilerplate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Make sure you have node and npm installed. Run these commands to make sure:&lt;br&gt;
&lt;code&gt;node -v&lt;/code&gt;&lt;br&gt;
&lt;code&gt;npm -v&lt;/code&gt;&lt;br&gt;
If you don't have either of it, click &lt;a href="https://www.npmjs.com/get-npm" rel="noopener noreferrer"&gt;here&lt;/a&gt; and install them first.&lt;/p&gt;

&lt;p&gt;🎬 Now, initialize the node project.&lt;br&gt;
&lt;code&gt;npm init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;✨ You will be prompted to enter some basic information. Once that is entered and done, you should have a &lt;strong&gt;package.json&lt;/strong&gt; file in your folder.&lt;/p&gt;
&lt;h1&gt;
  
  
  🌲 Chapter 1: Tree of Life
&lt;/h1&gt;
&lt;h3&gt;
  
  
  1.1 Express
&lt;/h3&gt;

&lt;p&gt;First things first: we make a server. We're using &lt;strong&gt;Express.js&lt;/strong&gt; framework so that we can architect our server, handle our routes and build RESTful APIs.&lt;/p&gt;

&lt;p&gt;If handling routes and APIs is not your requirement, you can still use Express or to keep things simpler, you can look into &lt;a href="https://github.com/webpack/webpack-dev-server" rel="noopener noreferrer"&gt;&lt;em&gt;webpack-dev-server&lt;/em&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;📦 Install Express.js:&lt;br&gt;
&lt;code&gt;npm install --save express&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;✨ A folder called &lt;strong&gt;node_modules&lt;/strong&gt; should automatically be created. Anything we install in our project will reside in that folder.&lt;/p&gt;

&lt;p&gt;🆕 Time to write the server. Create a new folder called &lt;strong&gt;server&lt;/strong&gt;. In that new folder, create a file &lt;strong&gt;index.js&lt;/strong&gt;. Add this basic minimal code to that file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
const mockResponse = {
  foo: 'bar',
  bar: 'foo'
};
app.get('/api', (req, res) =&amp;gt; {
  res.send(mockResponse);
});
app.get('/', (req, res) =&amp;gt; {
 res.status(200).send('Hello World!');
});
app.listen(port, function () {
 console.log('App listening on port: ' + port);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This allows the app to run on the specified port.&lt;br&gt;
The code also tells the app that home route: "/" should return a status of 200 (success) and send the text: Hello World. Basic enough!&lt;br&gt;
It also has a route "/api" that returns a dummy JSON object. It shows how fetching data would work.&lt;/p&gt;

&lt;p&gt;⭐️ Remember that the order of the routes matters. When a request comes through, Express starts matching routes from the top. When it matches a route, the response is returned and further routes are not checked.&lt;/p&gt;

&lt;p&gt;✏️ Now that the server is set up, in the package.json file, under &lt;code&gt;"scripts"&lt;/code&gt;, add a &lt;code&gt;start&lt;/code&gt; command like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node server/index.js",
  "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we're telling Node, that to start the project, start with server/index.js.&lt;/p&gt;

&lt;p&gt;🏃🏻‍♂️If you run the &lt;code&gt;npm run start&lt;/code&gt; command now, you should get a message:&lt;br&gt;
"App listening on port: 3000" on the terminal.&lt;/p&gt;

&lt;p&gt;🎉 Now go to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; on your browser and the &lt;strong&gt;"Hello World"&lt;/strong&gt; message should be showing up there. Go to &lt;a href="http://localhost:3000/api" rel="noopener noreferrer"&gt;http://localhost:3000/api&lt;/a&gt; and the dummy JSON should show up.&lt;/p&gt;
&lt;h3&gt;
  
  
  1.2 Webpack
&lt;/h3&gt;

&lt;p&gt;📦 Time to install &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;webpack (The bundler)&lt;/li&gt;
&lt;li&gt;webpack-cli (Command Line Interface to be able to run webpack commands)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save-dev webpack webpack-cli&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;package.json&lt;/strong&gt; file, under &lt;code&gt;"scripts"&lt;/code&gt;, add a &lt;code&gt;build&lt;/code&gt; command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node server/index.js",
  "build": "webpack --mode production",
  "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🆕 Now, create a folder called &lt;strong&gt;src&lt;/strong&gt;. This is where most of our project's source code will exist. In that new folder src, create a file &lt;strong&gt;index.js&lt;/strong&gt;.&lt;br&gt;
Leave the file empty for now.&lt;/p&gt;

&lt;p&gt;🏃🏻‍♂️If you run the &lt;code&gt;npm run build&lt;/code&gt; command, it will create a &lt;strong&gt;dist&lt;/strong&gt; folder with a bundled &lt;strong&gt;main.js&lt;/strong&gt; file in it. The code in it will be minified for production use.&lt;/p&gt;
&lt;h1&gt;
  
  
  🛰️ Chapter 2: Twilight Ozone
&lt;/h1&gt;
&lt;h3&gt;
  
  
  2.1 Babel
&lt;/h3&gt;

&lt;p&gt;🤗 React embraces &lt;a href="https://reactjs.org/docs/introducing-jsx.html" rel="noopener noreferrer"&gt;JSX&lt;/a&gt;. (Although JS would work perfectly fine too).&lt;br&gt;
Babel helps compile JSX syntax to JS.&lt;br&gt;
♻️ Not just that, but for regular .js files, we can now use the ES6 syntax and Babel will compile that to its equivalent ES5 form.&lt;/p&gt;

&lt;p&gt;📦 Install&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/core (To transform ES6+ code to ES5)&lt;/li&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-env (Preset to allow polyfills)&lt;/li&gt;
&lt;li&gt;
&lt;a class="mentioned-user" href="https://dev.to/babel"&gt;@babel&lt;/a&gt;/preset-react (Preset for React and JSX)&lt;/li&gt;
&lt;li&gt;babel-loader (Webpack helper)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;npm install --save-dev @babel/core @babel/preset-env @babel/preset-react babel-loader&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Among these 4 new packages, 2 of them are &lt;em&gt;presets&lt;/em&gt;. Babel core needs to know that it has these plugins. They need to be specified.&lt;/p&gt;

&lt;p&gt;🆕 On the project's root level, create a &lt;strong&gt;.babelrc&lt;/strong&gt; file. And specify the presets as an array like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "presets": ["@babel/preset-env", "@babel/preset-react"]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your code needs to be polyfilled, you will also need these Node packages: &lt;code&gt;core-js&lt;/code&gt; and &lt;code&gt;regenerator-runtime&lt;/code&gt;. More about that &lt;a href="https://github.com/zloirock/core-js/blob/master/docs/2019-03-19-core-js-3-babel-and-a-look-into-the-future.md" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.2 Babel + Webpack
&lt;/h3&gt;

&lt;p&gt;Based on Babel's functionality, Webpack needs to know that .js and .jsx files need to go through Babel before being bundled.&lt;br&gt;
So, we need to configure Webpack for that rule.&lt;/p&gt;

&lt;p&gt;🆕 On the project's root level, create a &lt;strong&gt;webpack.config.js&lt;/strong&gt; file. This will be the file for all webpack configs. Add the rule to it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⭐️ Remember: Since webpack bundles everything and creates a simple browser-readable code, all packages, presets and plugins you install will need to be configured in webpack.&lt;/p&gt;

&lt;h1&gt;
  
  
  🏖️ Chapter 3: Utopia
&lt;/h1&gt;

&lt;h3&gt;
  
  
  3.1 React
&lt;/h3&gt;

&lt;p&gt;Time to install react and react-dom.&lt;br&gt;
&lt;code&gt;npm install --save react react-dom&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🆕 In the folder &lt;strong&gt;src/&lt;/strong&gt;, create a new file &lt;strong&gt;index.html&lt;/strong&gt;. This will be the main and the only HTML file in our project.&lt;br&gt;
It would be like any regular HTML file you've made, with one difference: It needs a &lt;code&gt;&amp;lt;div&amp;gt;&lt;/code&gt; in the &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; that React can &lt;em&gt;populate&lt;/em&gt;. &lt;br&gt;
🔍 And it needs some form of identifier that React can lookup for.&lt;br&gt;
We set &lt;code&gt;id="root"&lt;/code&gt; on the &lt;code&gt;div&lt;/code&gt;. You can set the id to anything you want.&lt;br&gt;
Here's what a simple index.html with &lt;code&gt;&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt; looks like:&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;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
  &amp;lt;meta charset="UTF-8"&amp;gt;
  &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"&amp;gt;
  &amp;lt;meta http-equiv="X-UA-Compatible" content="ie=edge"&amp;gt;
  &amp;lt;title&amp;gt;React Boilerplate&amp;lt;/title&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
  &amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✏️ Remember that empty &lt;strong&gt;src/index.js&lt;/strong&gt; file we created in Section 1.2?&lt;br&gt;
Time to add code to it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';
const Index = () =&amp;gt; {
  return &amp;lt;div&amp;gt;Welcome to React!&amp;lt;/div&amp;gt;;
};
ReactDOM.render(&amp;lt;Index /&amp;gt;, document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚡️Here, &lt;code&gt;Index&lt;/code&gt; is a functional component that returns a React element. And ReactDOM renders it inside the &lt;code&gt;&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt; from &lt;strong&gt;index.html&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 React + Webpack
&lt;/h3&gt;

&lt;p&gt;Similar to what we did for .js and .jsx files, we need to tell Webpack what to do with the new .html file. Webpack needs to bundle it to the &lt;strong&gt;dist&lt;/strong&gt; folder.&lt;/p&gt;

&lt;p&gt;📦 For that we install html-webpack-plugin.&lt;br&gt;
&lt;code&gt;npm install --save-dev html-webpack-plugin&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;✏️ The webpack config file needs to be updated to handle this plugin. We also tell webpack that the now-coded &lt;strong&gt;src/index.js&lt;/strong&gt; is the entry point.&lt;br&gt;
Here's what the config file looks like after adding that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const HtmlWebPackPlugin = require("html-webpack-plugin");
const path = require('path');
const htmlPlugin = new HtmlWebPackPlugin({
  template: "./src/index.html", 
  filename: "./index.html"
});
module.exports = {
  entry: "./src/index.js",
  output: { // NEW
    path: path.join(__dirname, 'dist'),
    filename: "[name].js"
  }, // NEW Ends
  plugins: [htmlPlugin],
  module: {
    rules: [
      {
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      }
    ]
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When instantiating &lt;code&gt;htmlPlugin&lt;/code&gt;, the &lt;code&gt;template&lt;/code&gt; option tells webpack what file to pick and the &lt;code&gt;filename&lt;/code&gt; option tells what to name the bundled .html file in the dist folder.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.3 React + Express
&lt;/h3&gt;

&lt;p&gt;🏃🏻‍♂️ If you run &lt;code&gt;npm run start&lt;/code&gt; now, we would still get the &lt;strong&gt;"Hello World"&lt;/strong&gt; message on localhost. That is because the Express server does not know of these new files.&lt;/p&gt;

&lt;p&gt;✏️ In package.json, the &lt;code&gt;start&lt;/code&gt; script simply starts the server.&lt;br&gt;
We now need the webpack to bundle up our files and then start the server. &lt;br&gt;
Under &lt;code&gt;"scripts"&lt;/code&gt;, add a new &lt;code&gt;dev&lt;/code&gt; command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "start": "node server/index.js",
  "dev": "webpack --mode development &amp;amp;&amp;amp; node server/index.js",
  "build": "webpack --mode production",
  "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should now update Express and change what the route "/" returns.&lt;br&gt;
It should return the &lt;strong&gt;dist/index.html&lt;/strong&gt; file.&lt;/p&gt;

&lt;p&gt;✏️ In server/index.js, make the updates (New parts of code end with a comment: &lt;code&gt;// NEW&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;const express = require('express');
const path = require('path'); // NEW

const app = express();
const port = process.env.PORT || 3000;
const DIST_DIR = path.join(__dirname, '../dist'); // NEW
const HTML_FILE = path.join(DIST_DIR, 'index.html'); // NEW
const mockResponse = {
  foo: 'bar',
  bar: 'foo'
};
app.use(express.static(DIST_DIR)); // NEW
app.get('/api', (req, res) =&amp;gt; {
  res.send(mockResponse);
});
app.get('/', (req, res) =&amp;gt; {
 res.sendFile(HTML_FILE); // EDIT
});
app.listen(port, function () {
 console.log('App listening on port: ' + port);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🎉 Now run &lt;code&gt;npm run dev&lt;/code&gt; and go to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; on your browser. The &lt;strong&gt;"Welcome to React!"&lt;/strong&gt; message from &lt;strong&gt;src/index.js&lt;/strong&gt; should be showing up there. The "/api" route still works as before.&lt;/p&gt;

&lt;h1&gt;
  
  
  🍵 Chapter 4: Bottom Line Green
&lt;/h1&gt;

&lt;h3&gt;
  
  
  4.1 Sass
&lt;/h3&gt;

&lt;p&gt;Time to make stuff look good. Time to install node-sass and the required loaders: style-loader, css-loader and sass-loader.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install --save-dev node-sass style-loader css-loader sass-loader&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🆕 Create a new file &lt;strong&gt;styles.scss&lt;/strong&gt; in the folder &lt;strong&gt;src/&lt;/strong&gt;. Add some styles to that file.&lt;/p&gt;

&lt;p&gt;Here's a basic (and popular) code to use system fonts on your page.&lt;br&gt;
We also set its color property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI",
  Helvetica, Roboto, Arial, sans-serif;
  color: brown;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be a good file to add top-level and/or global styles.&lt;/p&gt;

&lt;p&gt;✏️ Import the new &lt;strong&gt;styles.scss&lt;/strong&gt; file. You can add it either to the &lt;strong&gt;index.html&lt;/strong&gt; or to the index.js file. To plan for consistency, we import it in &lt;strong&gt;index.js&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';
import './styles.scss';
const Index = () =&amp;gt; {
  return &amp;lt;div&amp;gt;Welcome to React!&amp;lt;/div&amp;gt;;
};
ReactDOM.render(&amp;lt;Index /&amp;gt;, document.getElementById('app'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4.2 Sass + Webpack
&lt;/h3&gt;

&lt;p&gt;Like everything else, Webpack needs to know what to do with &lt;strong&gt;.scss&lt;/strong&gt; files to correctly bundle them to a browser-understandable code.&lt;/p&gt;

&lt;p&gt;✏️ In &lt;strong&gt;webpack.config.js&lt;/strong&gt;, add the new rule just like the rule we added for babel-loader. So, in the &lt;code&gt;rules&lt;/code&gt; array in the &lt;code&gt;module&lt;/code&gt; object in &lt;code&gt;module.exports&lt;/code&gt;, add:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  test: /\.s?css$/,
  use: ['style-loader', 'css-loader', 'sass-loader']
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🎉 Now run &lt;code&gt;npm run dev&lt;/code&gt; and go to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; on your browser. The &lt;strong&gt;"Welcome to React! message"&lt;/strong&gt; should show up in the system font in brown.&lt;/p&gt;

&lt;h2&gt;
  
  
  ⌛ Epilogue
&lt;/h2&gt;

&lt;h3&gt;
  
  
  🖇️ React Components
&lt;/h3&gt;

&lt;p&gt;React projects are made up of a number of smaller components. The &lt;code&gt;Index&lt;/code&gt; in &lt;strong&gt;src/index.js&lt;/strong&gt; is one such component. You will be creating more such components and importing them (into one another 🤨).&lt;/p&gt;

&lt;p&gt;📂 I would suggest creating a folder called &lt;strong&gt;components/&lt;/strong&gt; inside the &lt;strong&gt;src/&lt;/strong&gt; folder. And store all the other components in that folder.&lt;/p&gt;

&lt;p&gt;Ideally, inside of &lt;strong&gt;components/&lt;/strong&gt;, create a sub-folder for every component. &lt;br&gt;
But it's up to the individual preference!&lt;/p&gt;

&lt;p&gt;💡 Don't forget that: React component files should export the component &lt;code&gt;Class&lt;/code&gt; or &lt;code&gt;function&lt;/code&gt;.&lt;br&gt;
Once you add some components to the &lt;strong&gt;src/index.js&lt;/strong&gt;, it would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';
import ReactDOM from 'react-dom';
import Header from './components/Header/index.jsx';
import Content from './components/Content/index.jsx';
const Index = () =&amp;gt; {
  return (
    &amp;lt;div className="container"&amp;gt;
      &amp;lt;Header /&amp;gt;
      &amp;lt;Content /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};
ReactDOM.render(&amp;lt;Index /&amp;gt;, document.getElementById('app'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🔧 Additional Webpack Configuration
&lt;/h3&gt;

&lt;p&gt;Like other files, images or any other static files also need to be bundled. Webpack needs to know of that.&lt;br&gt;
📦 Install &lt;code&gt;file-loader&lt;/code&gt; as a devDependency (&lt;code&gt;--save-dev&lt;/code&gt;) for all of such files.&lt;br&gt;
And add the following rule in &lt;strong&gt;webpack.config.js&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  test: /\.(png|svg|jpg|gif)$/,
  loader: "file-loader",
  options: { name: '/static/[name].[ext]' }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code, the test regex only specifies general image extensions. But, you can add any extensions for other files too (in the same rule object). &lt;/p&gt;

&lt;p&gt;✏️ To use an image or any other assets in the components, it needs to be imported in that.js/.jsx file first. So, Webpack can bundle it right and make it available in the bundled folder. You could use the actual &lt;code&gt;[name]&lt;/code&gt; of the file or &lt;code&gt;[hash]&lt;/code&gt; it up. With or without the file &lt;code&gt;[ext]&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;// Import
import LOGO from '&amp;lt;path-to-file&amp;gt;/logo.png';

...

// Usage
&amp;lt;img src={LOGO} alt="Page Logo" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🙅🏼‍♂️ Git, ignore!
&lt;/h3&gt;

&lt;p&gt;For deployment, a Node-compatible platform like Heroku or Netlify, runs the &lt;code&gt;build&lt;/code&gt; command in your app. That installs all the &lt;strong&gt;node_modules&lt;/strong&gt; and generates the &lt;strong&gt;dist&lt;/strong&gt; folder and its contents.&lt;br&gt;
So, we don't need to push the local folders: &lt;strong&gt;node_modules&lt;/strong&gt; and &lt;strong&gt;dist&lt;/strong&gt; to remote.&lt;/p&gt;

&lt;p&gt;🆕 To let Git know this, create a new file &lt;strong&gt;.gitignore&lt;/strong&gt; on the project root level.&lt;br&gt;
Anything you want Git to ignore can be added here. Here's a basic version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Deployment directories
node_modules
dist
# Optional npm cache directory
.npm
# Mac
.DS_Store
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🍺 That concludes the setup. This project can act as a great boilerplate for any future React w/ server apps or even for stand-alone Express projects.&lt;/p&gt;

&lt;p&gt;👍🏼 Thanks for making it all the way through the long article. Setting up an error-free Node app with Webpack and Babel and Express is definitely not a cakewalk. But I hope this article helped you. &lt;/p&gt;

&lt;p&gt;🌏 Go Planet!&lt;/p&gt;

</description>
      <category>react</category>
      <category>express</category>
      <category>webpack</category>
      <category>babel</category>
    </item>
  </channel>
</rss>
