<?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: Nick</title>
    <description>The latest articles on Forem by Nick (@stlnick).</description>
    <link>https://forem.com/stlnick</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%2F420087%2F17853aea-aa22-4f9f-ac6a-6ceb9a501d3e.jpg</url>
      <title>Forem: Nick</title>
      <link>https://forem.com/stlnick</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/stlnick"/>
    <language>en</language>
    <item>
      <title>Consistency</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Thu, 10 Feb 2022 04:05:44 +0000</pubDate>
      <link>https://forem.com/stlnick/consistency-38go</link>
      <guid>https://forem.com/stlnick/consistency-38go</guid>
      <description>&lt;p&gt;Do you ever get that thought that pops in your head like, &lt;strong&gt;"I should really start doing / should do [action]..."?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's definitely normal so don't worry. Well I like to believe it since &lt;em&gt;I definitely&lt;/em&gt; get that thought!&lt;/p&gt;

&lt;p&gt;The point of this article for me: simply to post. &lt;/p&gt;

&lt;p&gt;I haven't done that in a long time after I was fairly consistent to start. When I &lt;em&gt;started&lt;/em&gt; I was really focused on landing my first dev job while attending college and a code bootcamp (long story for another time) right about a year and a half ago.&lt;/p&gt;

&lt;p&gt;So here I am. Posting something. I promise it'll be better next time but to at least put a lil something out there.&lt;/p&gt;

&lt;h3&gt;
  
  
  What tool, system, approach, reward, punishment or combination do you use to maintain consistency?
&lt;/h3&gt;

&lt;p&gt;As a note that consistency is very subjective and could pertain to whatever you want - as long as you do it habitually.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>discuss</category>
    </item>
    <item>
      <title>How To Deploy a Full-Stack MERN App with Heroku/Netlify</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Sat, 29 Aug 2020 19:35:39 +0000</pubDate>
      <link>https://forem.com/stlnick/how-to-deploy-a-full-stack-mern-app-with-heroku-netlify-ncb</link>
      <guid>https://forem.com/stlnick/how-to-deploy-a-full-stack-mern-app-with-heroku-netlify-ncb</guid>
      <description>&lt;p&gt;This post is intended to be a guide for those that want to deploy a full-stack MERN app. It will be very specific on steps so it's a bit of a read, however, it will ensure there is minimal to no confusion on how to get from point A to point B.&lt;/p&gt;

&lt;p&gt;Feel free to hop around if you've got some of these steps done, know how to do them already, what have you.&lt;/p&gt;

&lt;p&gt;If this is your first time &lt;em&gt;don't intimidate yourself!&lt;/em&gt; Just read and complete each step &lt;strong&gt;one at a time&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This guide is based on &lt;em&gt;my specific project structure&lt;/em&gt; and this works well for it. There are definitely other ways to deploy &lt;em&gt;and&lt;/em&gt; other ways people prefer to have their project structured.&lt;/p&gt;

&lt;h1&gt;
  
  
  Let's get started!
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Project Context
&lt;/h2&gt;

&lt;p&gt;So you've got a cool project you'd like to show off to the world, how do we deploy a full-stack MERN app?&lt;/p&gt;

&lt;p&gt;Let's first get some basics out of the way and context on how I did this: my project structure, basic configuration, and why I chose this way of deployment.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;What is MERN&lt;/strong&gt;?
&lt;/h3&gt;

&lt;p&gt;MERN stands for &lt;strong&gt;MongoDB - Express - React - Node&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MongoDB for the database&lt;/li&gt;
&lt;li&gt;Node &amp;amp; Express for the server-side&lt;/li&gt;
&lt;li&gt;React for the client-side&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There's also the MEAN stack, which uses Angular instead of React, and the... MEVN(?) stack... whatever, it uses Vue instead of React or Angular.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Project Structure&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One thing that is definitely dev to dev, company to company, is how we structure our project as a whole. By this I mean where our directories are in relation to each other. Here is my fully collapsed root directory to show you the top-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%2Fi%2Fr5a5u5mpha2066whz3gl.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%2Fi%2Fr5a5u5mpha2066whz3gl.png" alt="Root directory structure"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just three things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;My VS Code settings and extensions JSON files&lt;/li&gt;
&lt;li&gt;Client-Side Code&lt;/li&gt;
&lt;li&gt;Server-Side Code&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I like this structure because to me there's a clear separation of responsibilities. It makes it easier for me to work on one or the other by just collapsing that whole folder. For instance if there's some UI feature I want to add/fix/alter then I have no reason to open my &lt;code&gt;server&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;I feel it makes it logical and simple to work with. Again, this is absolutely personal preference. This is also a big reason why this project was deployed in the way this guide will describe.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Some Options for Deployment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Credit to Dave Ceddia for putting this list together - this was found &lt;a href="https://daveceddia.com/deploy-react-express-app-heroku/" rel="noopener noreferrer"&gt;in a blog post&lt;/a&gt; of his. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Keep &lt;code&gt;client&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt; together

&lt;ul&gt;
&lt;li&gt;Your Express and React files will live on the same machine and Express will both serve your API requests &lt;strong&gt;and&lt;/strong&gt; the React files as well&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Put your API behind a proxy

&lt;ul&gt;
&lt;li&gt;This will allow your Express and React files to still live on one machine but each will be served by a different server&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Split &lt;code&gt;client&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt; apart &lt;em&gt;(the option for this guide)&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;Host your Express API on one machine and the React App on another&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Deployment is not something I have a ton of experience with so the first two options I've never attempted. &lt;/p&gt;

&lt;p&gt;I do know that in some cases for deployment on one machine a lot of devs must nest their entire &lt;code&gt;client&lt;/code&gt; directory &lt;strong&gt;inside of&lt;/strong&gt; &lt;code&gt;server&lt;/code&gt;. This was a big reason I went the route I did for deployment because for me I wanted to keep the structure pictured above with a clear separation of client-side code and server-side code.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Project Configuration&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The base of the project I deployed was the Create-React-App, which comes with a &lt;code&gt;webpack.config.js&lt;/code&gt; and other basic configurations out of the box, for the front-end. &lt;/p&gt;

&lt;p&gt;I also used Webpack for the &lt;code&gt;server&lt;/code&gt; directory as well to allow all those beautiful &lt;code&gt;import&lt;/code&gt;s and &lt;code&gt;export&lt;/code&gt;s to work for me and allow me to separate responsibilities on the server-side.&lt;/p&gt;

&lt;p&gt;Here's a shot of the &lt;code&gt;client&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt; directories expanded just to show you how I have it setup. There's only a couple things that will &lt;em&gt;need to be there&lt;/em&gt; for this project structure to work which I'll point out.&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%2Fi%2Fn4igseg6h83wfz3vyzwv.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%2Fi%2Fn4igseg6h83wfz3vyzwv.png" alt="Expanded root directory"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each directory, &lt;code&gt;client&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt;, needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.gitignore&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;To ignore the files and directories we don't want stored in our repo&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;package.json&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;To specify our separate &lt;code&gt;dependencies&lt;/code&gt; and &lt;code&gt;devDependencies&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;
&lt;code&gt;webpack.config.js&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;code&gt;client&lt;/code&gt; &lt;strong&gt;doesn't have one&lt;/strong&gt; because Create-React-App again provides one out of the box so I didn't need to specify it there&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;You may have noticed I have a &lt;code&gt;yarn.lock&lt;/code&gt; inside &lt;code&gt;client&lt;/code&gt; and a &lt;code&gt;package-lock.json&lt;/code&gt; inside &lt;code&gt;server&lt;/code&gt;. I intentionally use &lt;code&gt;yarn&lt;/code&gt; for client-side and &lt;code&gt;npm&lt;/code&gt; for server-side because it helps me just keep those separate in my mind. I know if I'm using &lt;code&gt;yarn&lt;/code&gt; then I'm in &lt;code&gt;client&lt;/code&gt; and if I'm using &lt;code&gt;npm&lt;/code&gt; I'm in &lt;code&gt;server&lt;/code&gt;. Once again just a personal preference - you could use &lt;code&gt;yarn&lt;/code&gt; or &lt;code&gt;npm&lt;/code&gt; for both and have no issues.&lt;/p&gt;

&lt;p&gt;You may have also noticed the &lt;code&gt;Procfile&lt;/code&gt; which we'll get to later in the guide. As a teaser this will be needed by Heroku to deploy our &lt;code&gt;server&lt;/code&gt; code which is why it lives inside of &lt;code&gt;server&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we have some context on how my project is setup let's talk about the actual process of deploying.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deployment Process
&lt;/h2&gt;

&lt;p&gt;What we're going to do specifically is host our &lt;code&gt;server&lt;/code&gt; code on Heroku and our &lt;code&gt;client&lt;/code&gt; code on Netlify. So our React App hosted on Netlify will make API requests to our Express API hosted on Heroku.&lt;/p&gt;

&lt;p&gt;This will assume that you have both &lt;code&gt;client&lt;/code&gt; and &lt;code&gt;server&lt;/code&gt; running correctly and that you have &lt;em&gt;already connected your app to a hosted database on MongoDB&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Steps&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://app.netlify.com/signup" rel="noopener noreferrer"&gt;Create a Netlify account&lt;/a&gt; or sign in if you have one&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://signup.heroku.com/login" rel="noopener noreferrer"&gt;Create a Heroku account&lt;/a&gt; or sign in if you have one&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href="https://devcenter.heroku.com/articles/heroku-cli#download-and-install" rel="noopener noreferrer"&gt;Install the Heroku CLI&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;There is instructions on how to do this for both Mac and Windows in this linked article&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Go to your terminal and type &lt;code&gt;heroku login&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This will prompt you to press any key, once you do it will take you to your browser where you will just need to click 'Log In'.&lt;/li&gt;
&lt;li&gt;Once this is successful you can close that browser window and &lt;strong&gt;navigate to your text editor now with your project open&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a file named &lt;code&gt;Procfile&lt;/code&gt;, &lt;strong&gt;no file extension&lt;/strong&gt;, in &lt;code&gt;/server&lt;/code&gt; directory.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Include this text and &lt;strong&gt;this text only&lt;/strong&gt; in the &lt;code&gt;Procfile&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;web: node dist/main.js&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;This tells Heroku what to do after building our app - which specifically is to run our bundled file from webpack with all of our server logic inside it&lt;/li&gt;
&lt;li&gt;If we didn't include this it would likely build just fine but never actually start our server&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we must add to our &lt;code&gt;server/package.json&lt;/code&gt; the following block: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can more than likely add this anywhere (not nested in any other property) but I added it right below the &lt;code&gt;"main"&lt;/code&gt; property near the top in the &lt;code&gt;server/package.json&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Recommended to check your node and npm versions first to know which versions to put in this next block. While it likely won't break your app with a slightly different version it is safer to specify the versions you used in development to prevent unexpected behavior.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;node -v&lt;/code&gt; or &lt;code&gt;node --version&lt;/code&gt; to check your Node version&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;npm -v&lt;/code&gt; or &lt;code&gt;npm --version&lt;/code&gt; to check your npm version&lt;/li&gt;
&lt;li&gt;Add those versions that return to this &lt;code&gt;engines&lt;/code&gt; property if they're different than mine.&lt;/li&gt;
&lt;li&gt;I used &lt;code&gt;"14.x"&lt;/code&gt; for &lt;code&gt;node&lt;/code&gt; to say, "As long as the major version of Node is 14 then use the latest minor version. Major version releases are when they're likely to be &lt;em&gt;breaking changes&lt;/em&gt; which we want to avoid.
```jsx
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;"engines": {&lt;br&gt;
    "node": "14.x",&lt;br&gt;
    "npm": "6.14.7"&lt;br&gt;
},&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
7. We need to allow access to your MongoDB database now from a new IP address. For simplicity, I added all IP addresses to be allowed.
    - **BIG NOTE**: In a production app **_you do not want to allow all IP addresses to access your database_**! You will want to find the specific IP address that hosts your server-side and add just that one as allowed.
    - The app I deployed does not contain any sensitive information. It's just a simple card game so no need for me to be super secure. _Once more_, do not do this for a real production app or even a side project that may contain sensitive information.
        1. Go to the [MongoDB Website](https://www.mongodb.com/) and sign in
        2. Navigate to the Project you're deploying using the dropdown on the top left
        ![MongoDB project drop down menu](https://dev-to-uploads.s3.amazonaws.com/i/mioopez440kdf4lbaxen.png)
        3. Then click the `Network Access` tab on the left side bar
        ![MongoDB Network Access Tab](https://dev-to-uploads.s3.amazonaws.com/i/xnosrkijcoy4o6rt259b.png)
        4. Click the green button on the right of the screen that says `Add IP Address`
        ![MongoDB Add IP address button](https://dev-to-uploads.s3.amazonaws.com/i/i4wg95qv9jpuzec054hu.png)
        5. A modal will popup. Click the button that says `Allow Access from Anywhere`. This will place `0.0.0.0/0` in the `Whitelist Entry` input. Then click `Confirm`.
        ![MongoDB Add IP address modal](https://dev-to-uploads.s3.amazonaws.com/i/k7rniqtjzn2ulf4wqrdy.png)

8. In development I had the express server listen to `localhost:5000` to run there and my react app ran on port `3000`. We must change what this Express server will listen for to `app.listen(process.env.PORT || 5000)`
    - So this will tell the server to listen to the `.env` environment variable `PORT` which **_we should not have set in our own_** `.env` _file_. OR it will listen to port 5000.
    - The `process.env.PORT` will be set by Heroku's `.env` - they essentially will tell our server which port to listen to.
    - If there is no such `process.env.PORT`, in other words there is no `.env` file that has been configured, then it will default to `5000`. This will allow us to keep that code in *and* be able to run it in our local development environment on port `5000` because we *do not* have a `.env` variable `PORT`.
    ```js


    // server.js

    /*
      Other code...
    */

    app.listen(process.env.PORT || 3000);


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

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---

**Ensure in your terminal that you are inside the _root_ of the project you are deploying for these next two steps**

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

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Now back inside your Terminal the next command we type is &lt;code&gt;heroku git:remote -a &amp;lt;project-name&amp;gt;&lt;/code&gt;. Replace &lt;code&gt;&amp;lt;project-name&amp;gt;&lt;/code&gt; with really whatever name you want - I recommend the name of your project to keep it simple. So for example it will look like &lt;code&gt;heroku git:remote -a my-project&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is going to create a remote connection with Heroku specifically and also create a branch on that remote for you and your app name will be the name you provided, such as &lt;code&gt;my-project&lt;/code&gt; above. Next we're going to actually &lt;code&gt;push&lt;/code&gt; our server code to that remote Heroku branch.
```
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;heroku git:remote -a my-project&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
10. The command we need to push our `server` code specifically, because we have the separation of `client` and `server` in our project structure, is `git subtree push --prefix server heroku master`
    - This is telling git to push a subtree, or in other words a *subdirectory*, from our current git repo. The `--prefix` flag comes before that subdirectory that we want to push, in our case it is `server`. And lastly we're telling it to push to the remote `heroku` our `master` branch.
    ```


    git subtree push --prefix server heroku master


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

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;---

**IF you want to test and ensure that you've deployed your server to Heroku just add in a test route in your `server.js` like:** 
```js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/', (req, res) =&amp;gt; { res.send('Hello from Express!')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    You can view your app by:
        - Copy and pasting the url that will appear in your Terminal after successfully pushing your code to the remote Heroku branch
        - Navigating to your project on [the Heroku website](http://heroku.com) and there will be a button that says 'View App' - click it and it will take you there
    - If you see the 'Hello from Express!' (or whatever test message you used) then that means your server is running correctly

    ---

11. Now for our MongoDB connection to work we must define an environment variable for Heroku to store our MongoDB connection string. 
    &amp;gt; We want to keep our connection string secret as we don't want anybody able to connect to our database and change, delete, add things etc. 
    &amp;gt; In our local build the connection string, is stored in our `.env` file inside of `/server`. I named by connection string `MONGODB_URI`. You can do this next step on the Heroku CLI or on the Heroku website. 
    &amp;gt; I recommend the website it's more straightforward and you don't have to worry about escaping special characters. I'm going to describe the next steps going through the website.
    - Navigate to your dashboard on Heroku
    - Click on your project name that will be present from our previous steps where we created our remote branch and pushed the code
    - Navigate to the Settings tab near the top
    - The second section is 'Config Vars' - click on 'Reveal Config Vars'
    - You'll see two inputs:
        - One is the name of your environment variable - name this **whatever you have it named in your local build**. For me that is `MONGODB_URI`.
        - Second is the value - paste your whole connection string here that you should copy from your `.env` file directly to avoid any typos.
    - Then just click 'Add' and our MongoDB connection environment variable is set.
    ![Heroku site config vars](https://dev-to-uploads.s3.amazonaws.com/i/7ewhhh5fybrk94691c6i.png)

    ---

    Our `server` code is officially deployed and configured correctly. Now onto the `client` code with Netlify.

    ---

12. Next is to deploy the front-end React code in `/client` to Netlify. Login to Netlify if you haven't already, the default tab should be `Sites` or just navigate to that tab.
    - Click the button that says `New site from Git`
    ![Netlify new site from git button](https://dev-to-uploads.s3.amazonaws.com/i/y01zt0d6tout6tvt3757.png)
    - Click the `Github` (or `GitLab` / `BitBucket` if that's where your repo is hosted) button below `Continuous Deployment`
    ![Netlify new site continuous deployment provider](https://dev-to-uploads.s3.amazonaws.com/i/egvtiqb6tc31p36ao838.png)
    - There will be a search bar that says `Search Repos` - type in the name of your project repo here to find it then click the line that has the repo you're looking for
    ![Netlify find repo for new site](https://dev-to-uploads.s3.amazonaws.com/i/ruf2nbhjpasof8z5ayv6.png)
    - Click `Deploy Site`. 
    ![Netlify new site deploy button](https://dev-to-uploads.s3.amazonaws.com/i/3kcvihxjge4zze3x1prn.png)
    - Once that's done it will take you to a page that shows a box near the top with your auto-generated site name and your site url right under it. In that box click `Site Settings`.
    ![Netlify post deploy screen](https://dev-to-uploads.s3.amazonaws.com/i/kt8un31fsx5ljswffwo1.png)
    - First you can change your site name here if you'd like by clicking the `Change site name` button on that first page. Do that if you'd like.
    ![Netlify change site name button](https://dev-to-uploads.s3.amazonaws.com/i/ze54wf50bk7vv9cz6nwm.png)
    - Now on the left menu bar click on the tab that says `Build &amp;amp; Deploy`.
    ![Netlify sidebar](https://dev-to-uploads.s3.amazonaws.com/i/bbjpw5oqo41pr4x5rcen.png)
    - Click the `Edit Settings`
    ![Netlify edit site settings button](https://dev-to-uploads.s3.amazonaws.com/i/os7b2tio2luqtew0azkn.png)
    - Change your `Base directory` to `client`, change your `Build command` to `yarn build`, and change your `Publish directory` to `client/build`. Then click the `Save` button on the bottom left.
    ![Netlify edit site build settings form](https://dev-to-uploads.s3.amazonaws.com/i/t8d1x1jsckuk24v5tfns.png)
    To ensure this all redeploys after we change these settings I like to explicity trigger a redeploy.
    - Near the top in the tabs located right under your team name and site name click on `Deploys`
    ![Netlify deploys tab](https://dev-to-uploads.s3.amazonaws.com/i/31mwopk5aw35ybs80p3x.png)
    - Then click the `Trigger deploy` button which has a drop down with two options. I always use `clear cache and deploy site` to ensure a fresh build that has all changes I've made.
    ![Netlify deploys tab](https://dev-to-uploads.s3.amazonaws.com/i/o05ey38l34lsdu80dhm6.png)

13. **LASTLY** after we deploy our front-end React code we must ensure _any requests_ we're sending from the client-side is changed to use our Heroku URL now instead of localhost.
    - In my structure the requests were being made from `client/api/index.js` so navigate to that file and *any request that contains* `http://localhost:5000` must be replaced by your Heroku URL.
    ```js


    // Before
    const res = await fetch('http://localhost:5000/scores/'

    // After
    const res = await fetch('https://my-project.herokuapp.com/scores/')

    // 'my-project' in the URL will either be the auto-generated
    // name from Netlify or if you changed the name it will
    // be the name you gave it


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Ensure that you push these changes up to GitHub.&lt;/strong&gt; Netlify will trigger a redeploy when they detect changes to your &lt;code&gt;master&lt;/code&gt; branch. So for this to work you must make those changes apparent to Netlify essentially.&lt;/p&gt;

&lt;p&gt;Now any request, instead of going to your local server you've ran while developing, will go to the hosted server you just deployed to Heroku with all of your Express Routes still intact and functioning properly.&lt;/p&gt;




&lt;h1&gt;
  
  
  You did it!
&lt;/h1&gt;

&lt;p&gt;It's undoubtedly a long process to get this done. But the important thing is that &lt;em&gt;it can be done&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;It's awesome to see the things we build on our local machine in development. After this though you can send a &lt;em&gt;live link&lt;/em&gt; to your friends, colleagues, etc. for them to check it out!&lt;/p&gt;

&lt;p&gt;This is awesome to have a live example as well for anyone looking for a job. Being able to not only have a link on your Resume or Portfolio site to the code on GitHub but for recruiters, managers, whoever to be able to see the project in action &lt;em&gt;is huge&lt;/em&gt;!&lt;/p&gt;

&lt;h2&gt;
  
  
  Let me know in the comments...
&lt;/h2&gt;

&lt;p&gt;... how you like to deploy! Link me an article, give me a brief overview, or whatever you want. I'd love to hear of the other ways devs get their projects out there for the world to see and enjoy!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>node</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Connect NodeJS and MongoDB</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 24 Aug 2020 14:26:29 +0000</pubDate>
      <link>https://forem.com/stlnick/connect-nodejs-and-mongodb-3jjc</link>
      <guid>https://forem.com/stlnick/connect-nodejs-and-mongodb-3jjc</guid>
      <description>&lt;p&gt;Getting into full-stack development requires that we're able to work with server-side code and databases. If you're working in JavaScript you'll be using Node most likely and a popular NoSQL database MongoDB.&lt;/p&gt;

&lt;p&gt;This article will assume you've got the basics of each already. The main topic here is &lt;em&gt;how&lt;/em&gt; we get Node and Mongo to talk to each other.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: &lt;a href="https://www.mongodb.com/"&gt;Visit MongoDB's website&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You should either &lt;strong&gt;sign in&lt;/strong&gt; or &lt;strong&gt;register as a new user&lt;/strong&gt;. Simple first steps we're already gaining momentum.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pesK_zEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vhvgjn5hjfhdm933e5rd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pesK_zEy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vhvgjn5hjfhdm933e5rd.png" alt="Alt Text" width="880" height="741"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Create and Connect to a Cluster
&lt;/h2&gt;

&lt;p&gt;If you do not have a Cluster yet on the site you will need to create one. It's been a while since I had zero Clusters so I'm not positive but I think it will automatically prompt you to create one.&lt;/p&gt;

&lt;p&gt;If not, no worries, you'll see in the picture below somewhere on your screen there should be a green button that says 'Create a New Cluster'. My advice, as I've not toyed with any of the settings nor want to pay for a Cluster, just keep the default settings and make sure to create a &lt;strong&gt;free Cluster&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gUIeHTH4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7tymqflt35q323t5ty1z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gUIeHTH4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7tymqflt35q323t5ty1z.png" alt="Alt Text" width="880" height="397"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that you have a Cluster, or if you had one already, your screen should like like the picture above. Next you'll need to click the little &lt;strong&gt;CONNECT&lt;/strong&gt; button below your Cluster name. And it will show you this screen below.&lt;/p&gt;

&lt;p&gt;Click on the middle option that says &lt;strong&gt;Connect your Application&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0T1AdPOS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vp9jcdll7hj3u99kc1fk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0T1AdPOS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vp9jcdll7hj3u99kc1fk.png" alt="Alt Text" width="880" height="737"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright we're almost done on the website. Ensure you do &lt;strong&gt;Step 1&lt;/strong&gt; of this Connect Popup - Select your driver (which is probably Node if you're reading this) and make sure the Version is appropriate. Since we're on Node 14 I would assume you're all safe in keeping the &lt;em&gt;3.6 or later&lt;/em&gt; option selected.&lt;/p&gt;

&lt;p&gt;Then just click the &lt;em&gt;Copy&lt;/em&gt; button to copy that long string which is how &lt;em&gt;your application&lt;/em&gt; will actually gain access to &lt;em&gt;your database&lt;/em&gt;. &lt;strong&gt;You will need to replace &lt;code&gt;&amp;lt;password&amp;gt;&lt;/code&gt; with your actual password and &lt;code&gt;&amp;lt;database&amp;gt;&lt;/code&gt; with your actual database name&lt;/strong&gt;. Potentially, you may have to replace &lt;code&gt;&amp;lt;username&amp;gt;&lt;/code&gt; but normally it auto-inserts that for me before I even copy the string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--deqE0EJK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7xi3facalls90xy4585x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--deqE0EJK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7xi3facalls90xy4585x.png" alt="Alt Text" width="880" height="713"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Setup a MongoClient
&lt;/h2&gt;

&lt;p&gt;Now we must go into your application code. Wherever your server code lives is now going to need a &lt;strong&gt;Client&lt;/strong&gt; to create the connection between Node and Mongo. Here's a simple representation of my project directories and where the code I'm going to show lives.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-client-&amp;gt; (front-end code)
-server
  |__db
  |  |_client.js
  |  |_index.js
  |
  |__server.js
  |__.env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First up let's look at &lt;code&gt;client.js&lt;/code&gt; because that's the first part we need to establish our connection.&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="nx"&gt;dotenv&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;dotenv&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;MongoClient&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;mongodb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;dotenv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&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;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MongoClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;useUnifiedTopology&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Close the connection after the server is shut down&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// 'CTRL + C' -&amp;gt; 'SIGINT' means signal interrupt i.e. server shut down&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGINT&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;close&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGINT received: DB connection is closing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// Avoid plugging up ports - ensures all processes are stopped&lt;/span&gt;
      &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&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="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;client&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright there's a bit going on there so let's look at it in chunks.&lt;/p&gt;

&lt;p&gt;So first off we have some imports. The &lt;code&gt;dotenv&lt;/code&gt; import is &lt;em&gt;only to store the copied string from the last step and use it&lt;/em&gt;. That long string we copied from Mongo's website on our Cluster is stored in the &lt;code&gt;.env&lt;/code&gt; you saw in my directory structure. The &lt;code&gt;dotenv.config()&lt;/code&gt; simply allows me to use those environment variables I've declared in &lt;code&gt;.env&lt;/code&gt; in this file here. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;process.env.MONGODB_URI&lt;/code&gt; could be replaced by the actual string we copied. So if you don't want to setup a &lt;code&gt;.env&lt;/code&gt; you don't have to.&lt;/p&gt;

&lt;p&gt;Now the &lt;code&gt;MongoClient&lt;/code&gt; import is fairly self-explanatory on what it's role is - it's how we will establish the connection. The &lt;code&gt;const client =&lt;/code&gt; part is where we declare a new &lt;code&gt;MongoClient&lt;/code&gt; and pass some arguments.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First is our connection string (again you could just paste the string here if you wanted)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;An object to pass additional options - here we just pass &lt;code&gt;useUnifiedTopology: true&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&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="nx"&gt;dotenv&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;dotenv&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;MongoClient&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;mongodb&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;dotenv&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;config&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;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;MongoClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;env&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MONGODB_URI&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;useUnifiedTopology&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To be honest, I'm not 100% on why &lt;code&gt;useUnifiedTopology: true&lt;/code&gt; helps us. Here's &lt;a href="https://mongodb.github.io/node-mongodb-native/3.3/reference/unified-topology/"&gt;Mongo's Reference Page on it&lt;/a&gt;. I read through and in the most basic sense it seems like a step towards making future updates easier. I could totally be wrong on that though.&lt;/p&gt;

&lt;p&gt;Let me know in the comments what your understanding of &lt;code&gt;useUnifiedTopology&lt;/code&gt; is!&lt;/p&gt;

&lt;p&gt;Alright, next chunk of code.&lt;/p&gt;

&lt;p&gt;Obviously we export the &lt;code&gt;client&lt;/code&gt; we've created so lets just get that bit out of the way.&lt;/p&gt;

&lt;p&gt;Now the bulk of this code is that Asynchronous IIFE. It's purpose is to establish our connection and then when our server shuts down to &lt;em&gt;close that connection&lt;/em&gt;. The comments in there already should help clarify certain parts.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We connect by awaiting the method available to us from our MongoClient object =&amp;gt; &lt;code&gt;connect()&lt;/code&gt;. Pretty simple.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then once we get the &lt;code&gt;SIGINT&lt;/code&gt; from the server we close that database connection. Because our parents taught us to clean up after ourselves and it's not just for our rooms!&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Close the connection after the server is shut down&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// 'CTRL + C' -&amp;gt; 'SIGINT' means signal interrupt i.e. server shut down&lt;/span&gt;
  &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGINT&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;close&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nx"&gt;then&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;info&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;SIGINT received: DB connection is closing&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// Avoid plugging up ports - ensures all processes are stopped&lt;/span&gt;
      &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;exit&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="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;client&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Use your &lt;code&gt;client&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now how do we use this &lt;code&gt;client&lt;/code&gt; that has connected for us? Here's one method out of my &lt;code&gt;db/index.js&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;client&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;./client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// GOOD&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addNewUser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newUser&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="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;throwaway&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;collection&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;insertOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newUser&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Import our client from our file&lt;/li&gt;
&lt;li&gt;Then simply use it as such in the try/catch block

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;client&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.db(&amp;lt;NAME OF YOUR DATABASE&amp;gt;)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.collection(&amp;lt;NAME OF YOUR COLLECTION&amp;gt;)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;.&amp;lt;METHOD YOU NEED&amp;gt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some notes here: &lt;code&gt;newUser&lt;/code&gt; is an object passed in from the client-side request. And since it is an object already I can pass it directly into the &lt;code&gt;insertOne&lt;/code&gt; method.&lt;/p&gt;

&lt;h2&gt;
  
  
  Moving Forward
&lt;/h2&gt;

&lt;p&gt;In my experience MongoDB can be pretty particular about how things are formatted, or passed in especially to those CRUD methods like &lt;code&gt;insertOne()&lt;/code&gt;, &lt;code&gt;find(0)&lt;/code&gt;, &lt;code&gt;findOneAndUpdate()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This is important to making sure the data you're trying to read, write, delete, or update is actually having their operation complete properly. &lt;/p&gt;

&lt;h3&gt;
  
  
  Get Some Practice
&lt;/h3&gt;

&lt;p&gt;I recommend instead of worrying about coming up with a project that will need data and coming up with how that data should look - just use MongoDB's sample data.&lt;/p&gt;

&lt;p&gt;Once you create a cluster you can essentially download some sample Collections.&lt;/p&gt;

&lt;p&gt;Do that, spin up a quick project structure like an index.html, barebones CSS and the server-side code you've seen in this article. The play around with how to pass data in correctly, get your client-side requests to the server-side (which is a whole different article), even just define a &lt;code&gt;const&lt;/code&gt; in your server code and pass it to the DB method and see if it updates.&lt;/p&gt;

&lt;p&gt;Time on task will help more than most things!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>mongodb</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>React Lifecycle Methods - The Basics</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 17 Aug 2020 15:14:41 +0000</pubDate>
      <link>https://forem.com/stlnick/react-lifecycle-methods-4lh4</link>
      <guid>https://forem.com/stlnick/react-lifecycle-methods-4lh4</guid>
      <description>&lt;p&gt;If you're using React then you're likely using Components. Why else would you be using React if you weren't, right? If you're using Components then you'll end up needing at least a little knowledge of how the &lt;strong&gt;Component Lifecycle&lt;/strong&gt; operates.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Basics
&lt;/h2&gt;

&lt;p&gt;In the &lt;a href="https://reactjs.org/docs/react-component.html"&gt;React Docs on Components&lt;/a&gt; they do a great job of laying it all out. I definitely recommend reading it over. &lt;em&gt;This article&lt;/em&gt; should provide you a quick overview of the &lt;strong&gt;Lifecycle&lt;/strong&gt;, it's most commonly used methods, and how some of the common &lt;strong&gt;Hooks&lt;/strong&gt; fit into that lifecycle.&lt;/p&gt;

&lt;p&gt;So first, as React was initially based around, and still probably most commonly used at least for now, classes. The &lt;em&gt;Lifecycle Methods&lt;/em&gt; are methods of &lt;em&gt;React Classes&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We can break these methods up into three categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Mounting&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Updating&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unmounting&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Updating seems self-explanatory, but what does it mean to &lt;em&gt;mount&lt;/em&gt; or &lt;em&gt;unmount&lt;/em&gt;?&lt;/p&gt;

&lt;h2&gt;
  
  
  Mounting
&lt;/h2&gt;

&lt;p&gt;When a Component has &lt;em&gt;mounted&lt;/em&gt; that means that a couple things have already taken place. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;That Component's &lt;code&gt;constructor()&lt;/code&gt; has been called and finished&lt;/li&gt;
&lt;li&gt;The Component's &lt;code&gt;render()&lt;/code&gt; has ran for the first time&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;At this point in time specifically&lt;/strong&gt; after this Component &lt;strong&gt;for the first time&lt;/strong&gt; has been initialized with its &lt;code&gt;constructor()&lt;/code&gt; and it's first &lt;code&gt;render()&lt;/code&gt; called &lt;strong&gt;it has mounted&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It is, in simplest terms, the first time that a Component renders to the screen. &lt;em&gt;This is where the &lt;code&gt;componentDidMount()&lt;/code&gt; lifecycle method comes in&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;componentDidMount()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This method is where, as a developer you'd like to make an API call, an initial database query, basically anything that can take some time fetching from a remote source. The Component has already rendered so the User won't be staring at a blank screen while waiting for the data to come back because these are &lt;em&gt;asynchronous&lt;/em&gt; tasks.&lt;/p&gt;

&lt;p&gt;You can also &lt;code&gt;setState()&lt;/code&gt; inside of this method and if it's &lt;em&gt;not&lt;/em&gt; dependent on an asynchronous task it will fire another &lt;code&gt;render()&lt;/code&gt;. The good thing is with a synchronous task here the User &lt;em&gt;will not see the intermediary state&lt;/em&gt;. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;constructor()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;render()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidMount()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;render()&lt;/code&gt; (If the state was changed in &lt;em&gt;componentDidMount()&lt;/em&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Updating
&lt;/h2&gt;

&lt;p&gt;This one again is pretty self-explanatory. This is when a Componenet has updated. To be more specific, whenever we pass in new props to a Component or if the state within that component has changed the Component &lt;em&gt;has updated&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So what happens on an update? Well, a &lt;code&gt;render()&lt;/code&gt; of course! Followed up by our next lifecycle method &lt;code&gt;componentDidUpdate()&lt;/code&gt;. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Now there is a less used method &lt;code&gt;shouldComponentUpdate()&lt;/code&gt;, but take a look at the &lt;a href="https://reactjs.org/docs/react-component.html"&gt;React Docs on Components&lt;/a&gt; if you want to know more about the usage of that.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;componentDidUpdate()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This'll be a quick one. We already know that new props or &lt;code&gt;setState()&lt;/code&gt; causes a &lt;code&gt;render()&lt;/code&gt; and then &lt;code&gt;componentDidUpdate()&lt;/code&gt; will fire.&lt;/p&gt;

&lt;p&gt;What do we use it for though?&lt;/p&gt;

&lt;p&gt;It's almost exactly the same use as &lt;code&gt;componentDidMount()&lt;/code&gt;. Network requests are a big use case for this method. You can just the same call &lt;code&gt;setState()&lt;/code&gt; inside this lifecycle method. &lt;/p&gt;

&lt;p&gt;One catch to using &lt;code&gt;setState()&lt;/code&gt; inside &lt;code&gt;componentDidUpdate()&lt;/code&gt; is to be wary of the &lt;em&gt;infinite loop&lt;/em&gt;. If you &lt;strong&gt;must use &lt;code&gt;setState()&lt;/code&gt; inside &lt;code&gt;componenetDidUpdate()&lt;/code&gt;&lt;/strong&gt; then you &lt;strong&gt;must also compare props with previous props&lt;/strong&gt;. Doing this will prevent that infinite loop.&lt;/p&gt;

&lt;p&gt;To clarify here's an example straight from the Docs that we'll add one line to and break down a little bit.&lt;/p&gt;

&lt;p&gt;We can tell there's a method on this class Component called &lt;code&gt;fetchData()&lt;/code&gt; that clearly takes a &lt;code&gt;userID&lt;/code&gt;. We see that the &lt;code&gt;userID&lt;/code&gt; is coming from &lt;code&gt;props&lt;/code&gt;. Okay, nothing crazy.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;componentDidUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&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="k"&gt;this&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="nx"&gt;userID&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataObject&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;fetchData&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;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;userID&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dataObject&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;To understand where the problem lies lets layout the lifecycle method calls.&lt;/p&gt;

&lt;p&gt;Lets say this component has already went through&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;constructor()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;render()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidMount()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And now in our program the user has performed an action that changes our props. Lets say they entered &lt;em&gt;their ID into an input&lt;/em&gt; and now we pass that down as a prop to &lt;strong&gt;this Component&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User enters ID into input on a form and submits&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;The props of this component change&lt;/em&gt; 

&lt;ul&gt;
&lt;li&gt;Which will trigger a &lt;code&gt;componentDidUpdate()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;componentDidUpdate()&lt;/code&gt;

&lt;ul&gt;
&lt;li&gt;Which has a &lt;code&gt;setState()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;The state of this component changes&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;Which will trigger a &lt;code&gt;componentDidUpdate()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;At this moment&lt;/strong&gt; if we do not compare the Previous Props to the current Props and &lt;em&gt;ensure that they are different&lt;/em&gt; we will fall into that infinite loop because &lt;strong&gt;state is set inside the update&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Props change&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidUpdate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setState()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidUpdate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setState()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;componentDidUpdate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;etc. etc. etc. NOOoooooo, why me!?!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Unmounting
&lt;/h2&gt;

&lt;p&gt;This will also be quick. The only time this is ran is when the Component is being taken out of the DOM tree altogether and destroyed. And it has only one method: &lt;code&gt;componentWillUnmount()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now this is one personally I haven't used yet. The Docs put it pretty succinctly and in a way that &lt;em&gt;I understand&lt;/em&gt; but haven't in my little React experience &lt;em&gt;needed quite yet&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Perform any necessary cleanup in this method, such as invalidating timers, canceling network requests, or cleaning up any subscriptions that were created in &lt;code&gt;componentDidMount()&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Don't bother using &lt;code&gt;setState()&lt;/code&gt; in this method as the Component is removed and destroyed immediately after whatever functionality you have inside this function.&lt;/p&gt;

&lt;p&gt;No amount of standing outside it's house with a boombox jamming "In Your Eyes" by Peter Gabriel will bring it back.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--S2InwV45--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tktajrv4co8fkcyyoh8o.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--S2InwV45--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tktajrv4co8fkcyyoh8o.jpg" alt="John Cusack holding boombox in 'Say Anything' movie" width="880" height="586"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Definitely still go to the &lt;a href="https://reactjs.org/docs/react-component.html"&gt;React Docs on Components&lt;/a&gt; and read up. They have great examples, they show the less used (but still useful) lifecycle methods, and they also include the Error Boundary ones which I did not for simplicity.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My biggest recommendation for understanding these methods and their timing&lt;/strong&gt; is to just &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use &lt;code&gt;create-react-app&lt;/code&gt; to make a project&lt;/li&gt;
&lt;li&gt;create a Component under &lt;code&gt;App&lt;/code&gt; and a child under that as well

&lt;ul&gt;
&lt;li&gt;Maybe something like App &amp;gt; Parent &amp;gt; Child&lt;/li&gt;
&lt;li&gt;Literally just call them that for clarity&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;In each component&lt;/em&gt; call these methods with a simple &lt;code&gt;console.log&lt;/code&gt; and the method name&lt;/li&gt;
&lt;li&gt;Then change some props, change some state, etc. and see the order in which these methods fire!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This was absolutely the biggest help for me to get a better grasp on these React Lifecycle Methods. Oh, and did I mention to read the &lt;a href="https://reactjs.org/docs/react-component.html"&gt;React Docs on Components&lt;/a&gt;?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>`useEffect()` and `async`</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 10 Aug 2020 14:03:53 +0000</pubDate>
      <link>https://forem.com/stlnick/useeffect-and-async-4da8</link>
      <guid>https://forem.com/stlnick/useeffect-and-async-4da8</guid>
      <description>&lt;p&gt;If you've learned the traditional class-based React Components and you're now trying to move into Hooks there's a few things along the way that'll throw you for a loop.&lt;/p&gt;

&lt;p&gt;One such thing that took a little digging for me is the combination of &lt;code&gt;useEffect()&lt;/code&gt; - essentially the replacement for &lt;code&gt;componentDidMount&lt;/code&gt;, &lt;code&gt;componentDidUpdate&lt;/code&gt;, and &lt;code&gt;componentWillUnmount&lt;/code&gt; - and &lt;code&gt;async&lt;/code&gt;/&lt;code&gt;await&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Situation
&lt;/h2&gt;

&lt;p&gt;Common scenario: Our app loads, we get something on the screen &lt;em&gt;AND THEN&lt;/em&gt; we want to fetch some sort of data. We don't want to block the User from moving on or make them stare at a blank screen because our API call is taking longer than expected. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;componentDidMount&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This is the method in which we remedied this problem if using React Class-based Components. It ensures that the actual Component is inserted into the DOM tree first and then &lt;code&gt;render()&lt;/code&gt; is called.&lt;/p&gt;

&lt;p&gt;If we &lt;em&gt;don't need&lt;/em&gt; an API request to be made then the Component will simply render to the screen. If we do need to make an API request we should do that inside of &lt;code&gt;componentDidMount()&lt;/code&gt;. In addition if upon receiving the API response we must update, or &lt;code&gt;setState()&lt;/code&gt;, then that will actually trigger a second render.&lt;/p&gt;

&lt;p&gt;Then the calls will look like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;componentDidMount()&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;li&gt;setState() [from inside componentDidMount()]&lt;/li&gt;
&lt;li&gt;render()&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though there is a second render, the React Docs state that the user &lt;strong&gt;will not see the intermediate state&lt;/strong&gt;. So no weird flickering UI - that's good for us! However, the Docs also say to be cautious as this way could lead to performance issues. &lt;/p&gt;

&lt;p&gt;More specifically, any DOM node that needs insertion into the tree belongs in this spot, &lt;code&gt;componentDidMount()&lt;/code&gt;. If you can React recommends that initialization of &lt;code&gt;state&lt;/code&gt; be done in the &lt;code&gt;constructor()&lt;/code&gt; instead of here. Obviously, that isn't always viable but it is what they recommend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/api/index.js &lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;index&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;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://my-json-server.typicode.com/Claim-Academy-JS/products/products&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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="cm"&gt;/* ------------------ */&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;api&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;api&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;componentDidMount&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;filteredProducts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&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;This code shows the fetch call being made inside &lt;code&gt;componentDidMount()&lt;/code&gt; and in this case for my project I needed this setup. These products were to be inserted into the DOM tree so I make the fetch call and set my state. Of course &lt;code&gt;async&lt;/code&gt; is on both &lt;code&gt;componentDidMount()&lt;/code&gt; and my exported object's &lt;code&gt;index()&lt;/code&gt; method. Then inside both of those we &lt;code&gt;await&lt;/code&gt; the results.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;useEffect()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now with Hooks, more specifically &lt;code&gt;useEffect()&lt;/code&gt; there's a couple things we need to be aware of. First we must understand that &lt;code&gt;useEffect()&lt;/code&gt; takes &lt;strong&gt;two arguments&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;First Argument&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Callback function&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Second Argument&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Property to watch for a change -&amp;gt; then fire the provided callback&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So as per usual, a callback is needed - no surprise there. The second parameter can cause some problems &lt;em&gt;if we forget to specify it&lt;/em&gt;. If there is &lt;strong&gt;no second argument provided&lt;/strong&gt; the &lt;code&gt;useEffect()&lt;/code&gt; will fire on every update no matter what is being updated. Even further, if there's a &lt;code&gt;setState()&lt;/code&gt; or a &lt;code&gt;useState()&lt;/code&gt; setter being used inside - &lt;code&gt;useEffect()&lt;/code&gt; will go into an infinite loop.&lt;/p&gt;

&lt;p&gt;Let's take a look at an equivalent code to the previous example just instead utilizing &lt;code&gt;useEffect()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="nx"&gt;useEffect&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="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="nx"&gt;setFilteredProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;setProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})()&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[])&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;This time you see there is an Immediately Invoked Function Expression, or IIFE, inside. We could just as well name that function and then specifically invoke it inside too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchProducts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="nx"&gt;setFilteredProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;setProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;fetchProducts&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;Also take note, we are actually providing a callback to &lt;code&gt;useEffect()&lt;/code&gt; and &lt;em&gt;within that callback&lt;/em&gt; we must define another function and invoke it. That's due to a fetch call returning a Promise. So essentially &lt;code&gt;useEffect()&lt;/code&gt; itself isn't responsible for that so our defined function will handle it. &lt;/p&gt;

&lt;p&gt;Lastly that &lt;strong&gt;second argument&lt;/strong&gt; is present to ensure this &lt;code&gt;useEffect()&lt;/code&gt; only runs at a specific time.&lt;/p&gt;

&lt;p&gt;There we provided &lt;code&gt;[]&lt;/code&gt; as the second argument. This is telling &lt;code&gt;useEffect()&lt;/code&gt; "Hey, I only want you to run your callback when this Component mounts for the first time and that's it." By using &lt;code&gt;[]&lt;/code&gt; we tell &lt;code&gt;useEffect()&lt;/code&gt; that there are no properties we want you to watch and then run your callback when they change. Just run once.&lt;/p&gt;

&lt;p&gt;There's a difference too between &lt;code&gt;[]&lt;/code&gt; as a second argument and &lt;em&gt;no second argument&lt;/em&gt;. As mentioned before without a second argument that &lt;code&gt;useEffect()&lt;/code&gt; will think it should run when the component mounts and then on every single update after regardless of what piece of state changes.&lt;/p&gt;

&lt;p&gt;If we wanted a function to run each time a specific piece of state changes you would simply put it inside the brackets like so &lt;code&gt;[somePropertyNameHere]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Article Will Unmount
&lt;/h2&gt;

&lt;p&gt;It took a little digging to find the way to combine &lt;code&gt;useEffect()&lt;/code&gt; and &lt;code&gt;async&lt;/code&gt; functions. It was definitely surprising the the React Docs has no examples of this situation at all that I could find. I referenced a few different 3rd party articles explaining this to be sure I was on the right path by what all of them said. &lt;a href="https://www.robinwieruch.de/react-hooks-fetch-data"&gt;Robin Wieruch wrote about this&lt;/a&gt;; it was the first one and one of the better articles I found on this topic.&lt;/p&gt;

&lt;p&gt;Definitely happy to figure it out because even though this was a weird one to figure out - I still like Hooks more!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>codenewbie</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Starting with React Hooks</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 03 Aug 2020 15:22:30 +0000</pubDate>
      <link>https://forem.com/stlnick/starting-with-react-hooks-2l0b</link>
      <guid>https://forem.com/stlnick/starting-with-react-hooks-2l0b</guid>
      <description>&lt;p&gt;For just over a year now we've had access to this shiny new feature React Hooks. I'm sure most of us have at least heard of it. &lt;/p&gt;

&lt;p&gt;For me I didn't know what it truly meant or how it compared to what was being done in React prior, honestly. So recently starting to work with React I've been learning about the &lt;strong&gt;Class-based approach&lt;/strong&gt; as well as &lt;strong&gt;Hooks&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Hooks?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks are functions that let you “hook into” React state and lifecycle features from function components.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is straight from &lt;a href="https://reactjs.org/docs/hooks-overview.html"&gt;ReactJS.org&lt;/a&gt;. Pretty simple, right? Seems so at least.&lt;/p&gt;

&lt;p&gt;For me it was good to learn the &lt;strong&gt;Class-based&lt;/strong&gt; approach first to understand where React was to move into using &lt;strong&gt;Hooks&lt;/strong&gt;. It also made me appreciate &lt;strong&gt;Hooks&lt;/strong&gt; that much more as I feel it's a little cleaner and more intuitive - that's personal preference though.&lt;/p&gt;

&lt;h3&gt;
  
  
  Class-based approach
&lt;/h3&gt;

&lt;p&gt;When utilizing this approach we would define a &lt;code&gt;class&lt;/code&gt; component that &lt;code&gt;extends React.Component&lt;/code&gt;, that way we get access to the React lifecycle methods. This approach in itself has come a long way from the start.&lt;/p&gt;

&lt;p&gt;Here's the traditional way we handled &lt;strong&gt;Class-based Components&lt;/strong&gt;, again straight from &lt;a href="https://reactjs.org"&gt;ReactJS.org&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Toggle&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&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="kd"&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;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="c1"&gt;// This binding is necessary to make `this` work in the callback&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="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;handleClick&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="k"&gt;this&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="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&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;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isToggleOn&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&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="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&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="nx"&gt;isToggleOn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ON&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OFF&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&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="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Toggle&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can shorten that first bit where we had to call &lt;code&gt;constructor(props)&lt;/code&gt; and &lt;code&gt;super(props)&lt;/code&gt; and &lt;code&gt;this.state = {}&lt;/code&gt; AND we had to &lt;em&gt;bind&lt;/em&gt; any event handler we defined. It was just a bit cumbersome.&lt;/p&gt;

&lt;p&gt;Here's the shorter &lt;strong&gt;Class-based Component&lt;/strong&gt; syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Toggle&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&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="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;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&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;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isToggleOn&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&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="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&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="nx"&gt;isToggleOn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ON&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OFF&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&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="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Toggle&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A little shorter and cleaner, right? All we have to do is just define the &lt;code&gt;state&lt;/code&gt; object, set our properties and the only other change was converting &lt;code&gt;handleClick&lt;/code&gt; into an arrow function - that way &lt;code&gt;this&lt;/code&gt; will still reference our component.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Side Note: Functional Components&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Functional Components&lt;/strong&gt; are in a simple explanation just function that will return some &lt;em&gt;jsx&lt;/em&gt;. A 'dumb component' if you will. They do not need to know state and just receive &lt;strong&gt;props&lt;/strong&gt; to render their &lt;em&gt;jsx&lt;/em&gt; correctly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Welcome&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;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Quite simple.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hooks
&lt;/h3&gt;

&lt;p&gt;When utilizing &lt;strong&gt;Hooks&lt;/strong&gt; our &lt;strong&gt;Functional Components&lt;/strong&gt; take over the role our &lt;strong&gt;Class-based Components&lt;/strong&gt; had. &lt;strong&gt;Hooks&lt;/strong&gt; &lt;em&gt;will not&lt;/em&gt; work inside a &lt;code&gt;class&lt;/code&gt; - we &lt;em&gt;must&lt;/em&gt; use our &lt;strong&gt;Functional Components&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For reference in the upcoming examples here is the previous &lt;code&gt;Toggle&lt;/code&gt; Component refactored to use React Hooks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Toggle&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;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsToggleOn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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;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="nx"&gt;setIsToggleOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;render&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="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isToggleOn&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;ON&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OFF&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&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="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Toggle&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Commonly Used Hooks
&lt;/h2&gt;

&lt;p&gt;In the &lt;strong&gt;Class-based&lt;/strong&gt; approach we would have access to certain &lt;em&gt;lifecycle methods&lt;/em&gt; that allowed us to update our application at the right times and perform operations at the right times.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;useState()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;This Hook took over for &lt;code&gt;setState()&lt;/code&gt; in &lt;strong&gt;Class-based components&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIsToggleOn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This &lt;code&gt;useState()&lt;/code&gt; allows us to simultaneously set the state of &lt;code&gt;isToggleOn&lt;/code&gt; with the argument provided to the method - here that is &lt;code&gt;true&lt;/code&gt; - as well as get back a setter to change that piece of state in the future - here it's called &lt;code&gt;setIsToggleOn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The 'set' part of that &lt;em&gt;is convention&lt;/em&gt; to make it clear that &lt;strong&gt;this function&lt;/strong&gt; will &lt;strong&gt;set&lt;/strong&gt; the piece of state called &lt;strong&gt;isToggleOn&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Call &lt;code&gt;useState()&lt;/code&gt; for as many pieces of state you have in different calls and you'll have setters for each piece of state. It can make it much more clear what we're trying to change.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Class-based setState()&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setState&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;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;isToggleOn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isToggleOn&lt;/span&gt;
    &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Hooks&lt;/span&gt;
&lt;span class="kd"&gt;const&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="nx"&gt;setIsToggleOn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;isToggleOn&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;It's shorter and arguably more intuitive than having to type out all of the &lt;strong&gt;Class-based&lt;/strong&gt; way.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;useEffect()&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now when we needed to fetch data, setup subscriptions or explicitly change DOM elements we would use the lifecycle methods &lt;code&gt;componentDidMount&lt;/code&gt;, &lt;code&gt;componentDidUpdate&lt;/code&gt; and &lt;code&gt;componentWillMount&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code&gt;useEffect&lt;/code&gt; we basically have those three methods wrapped up inside of just his one. Definitely check out the &lt;a href="https://reactjs.org/docs/hooks-effect.html"&gt;docs on useEffect&lt;/a&gt; as it goes way more in depth and gives you context on the best practices on how to split your logic.&lt;/p&gt;

&lt;p&gt;Here, since I'm still reading those docs and wrapping my own head around this, I'm only going to provide a simple example that involves data fetching. This example is from a small practice project I recently did:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// The 'api.index()' method used in the following block of code that makes the call for data&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;index&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;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://my-json-server.typicode.com/Claim-Academy-JS/products/products&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;  &lt;span class="c1"&gt;// Class-Based&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;componentDidMount&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;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&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;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;filteredProducts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// Hooks&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;products&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="nx"&gt;setFilteredProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;setProducts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;products&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// If the data hasn't been fetched then make the api call&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;products&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;fetchData&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;So in our &lt;strong&gt;Class-based Components&lt;/strong&gt; we would use &lt;code&gt;componentDidMount&lt;/code&gt; to ensure &lt;em&gt;before&lt;/em&gt; we made an attempt to fetch data the component itself was rendered &lt;em&gt;and then&lt;/em&gt; we would update it on receiving our data from the server. This lifecycle method runs just once as the Component only mounts once in its lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; will run when the Component mounts, each time it updates and right before it unmounts. That is why there's a piece of logic to ensure we don't attempt to re-fetch the data each time this Component updates. &lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Hooks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Custom Hooks&lt;/strong&gt; are basically what they sound like - your own defined Hooks that you can design to help you with application-specific tasks or maybe tasks you've found yourself doing a bunch and could be streamlined having your own Hook for that task.&lt;/p&gt;

&lt;p&gt;Once again, Hooks are something I've been working with just a week or so at this point. I'm not the one to tell you how to develop your own Custom Hook because I haven't tried myself yet! &lt;/p&gt;

&lt;p&gt;The &lt;a href="https://reactjs.org/docs/hooks-custom.html"&gt;docs on Custom Hooks&lt;/a&gt; do a good job explaining and have a great example to show a proper use case. It made sense to me reading but I'm not going to try and rehash that here as I haven't put it in practice yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  There Is Much More to Hooks...
&lt;/h2&gt;

&lt;p&gt;... than I can tell you! I'm only beginning to scratch the surface of how to use Hooks to their full potential.&lt;/p&gt;

&lt;p&gt;Not to mention the &lt;strong&gt;Custom Hooks&lt;/strong&gt; I've yet to experiment with if you check out the &lt;a href="https://reactjs.org/docs/hooks-reference.html"&gt;Hooks API Reference&lt;/a&gt; you'll see even more out-of-the-box Hooks like &lt;code&gt;useContext&lt;/code&gt;, &lt;code&gt;useReducer&lt;/code&gt;, &lt;code&gt;useLayoutEffect&lt;/code&gt; and more!&lt;/p&gt;

&lt;p&gt;These examples I have were just the first Hooks I've put to use and made me see the benefit and potential of utilizing Hooks moving forward.&lt;/p&gt;

</description>
      <category>codenewbie</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Callbacks vs. Promises</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 27 Jul 2020 15:48:43 +0000</pubDate>
      <link>https://forem.com/stlnick/callbacks-vs-promises-313a</link>
      <guid>https://forem.com/stlnick/callbacks-vs-promises-313a</guid>
      <description>&lt;p&gt;Basically whenever you need to get data from a server, do something with that data, maybe grab more data from another server you'll be using either &lt;strong&gt;callbacks&lt;/strong&gt; or &lt;strong&gt;Promises&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's a way to do something you need to do with that data once it comes back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Callbacks
&lt;/h2&gt;

&lt;p&gt;Callbacks are the parameter we use in many cases to provide functionality. A very common one is using a callback for &lt;code&gt;addEventListener()&lt;/code&gt; when in web development.&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This is where your logic goes in the callback!&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The Bad
&lt;/h3&gt;

&lt;p&gt;When we end up with a ton of &lt;code&gt;if else&lt;/code&gt; statements nested, resulting in &lt;em&gt;callback hell&lt;/em&gt;, we're again normally fetching data or manipulating data and need to ensure each step of the way an error hasn't occurred.&lt;/p&gt;

&lt;p&gt;This example is from &lt;a href="http://callbackhell.com/"&gt;callbackhell.com&lt;/a&gt; and shows pretty well this issue. &lt;/p&gt;

&lt;p&gt;This is going through a directory and attempting to resize files. It's not as important to understand the code here but simply the structure.&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;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;files&lt;/span&gt;&lt;span class="p"&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;err&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error finding files: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;files&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&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="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fileIndex&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;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;gm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;size&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&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;err&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error identifying file size: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;filename&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; : &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="nx"&gt;aspect&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;values&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="nx"&gt;widths&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&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="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;widthIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;aspect&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;resizing &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;filename&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;to &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;x&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;height&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;resize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;height&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dest&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;w&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;width&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;filename&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="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;Error writing file: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;})&lt;/span&gt;
          &lt;span class="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;Pyramid of Doom&lt;/em&gt; makes an appearance. On the left we see the indentation gradually getting further to the right and then closing with a bunch of &lt;code&gt;}&lt;/code&gt; and &lt;code&gt;})&lt;/code&gt;. Easy recipe to make simple errors based on one parenthesis or curly bracket.&lt;/p&gt;

&lt;p&gt;Each step of the way we nest another &lt;code&gt;if else&lt;/code&gt; and there is a total of &lt;strong&gt;4 callback functions&lt;/strong&gt; nested in there.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Good
&lt;/h3&gt;

&lt;p&gt;This is the only way as JavaScript developers we were able to do these asynchronous tasks prior to &lt;code&gt;Promise&lt;/code&gt;s. &lt;/p&gt;

&lt;p&gt;It's a good thing to know of, be able to read and understand what's happening and why. There will be code out there you may work on for a long time most likely that has this structure.&lt;/p&gt;

&lt;p&gt;Some people prefer it and that's totally fine! We all have our own preferences as devs and if the task is completed properly there's no problem truly. &lt;/p&gt;

&lt;p&gt;Not to mention the example above isn't written as well as it could be. Again visit &lt;a href="http://callbackhell.com/"&gt;callbackhell.com&lt;/a&gt; as it has this example and goes through steps on how to improve it while still using callbacks. &lt;/p&gt;

&lt;p&gt;Steps such as name your functions instead of using anonymous ones, try to keep it shallow when nesting and always handle each potential error.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Remember, only you can prevent callback hell and forest fires&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;--&lt;a href="http://callbackhell.com/"&gt;callbackhell.com&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Promises
&lt;/h2&gt;

&lt;p&gt;The &lt;code&gt;Promise&lt;/code&gt; in JavaScript was made official in the release of ES6. Since then they've become a widely supported and accepted feature of JavaScript. &lt;/p&gt;

&lt;p&gt;A &lt;code&gt;Promise&lt;/code&gt; is essentially an object that represents some asynchronous task that will eventually succeed or fail. If it 'suceeds' we say it is &lt;strong&gt;fulfilled&lt;/strong&gt; and we will use the &lt;code&gt;.then()&lt;/code&gt; method to handle success.&lt;/p&gt;

&lt;p&gt;If a &lt;code&gt;Promise&lt;/code&gt; fails it is said to have been &lt;strong&gt;rejected&lt;/strong&gt; and we will use the &lt;code&gt;.catch()&lt;/code&gt; method to handle that failure and error.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Bad
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Promise&lt;/code&gt;s are meant to be a asynchronous tasks. And since JavaScript lets us pretty much willy nilly write whatever we want we can end up with some bad code. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://dev.to/craigmichaelmartin"&gt;Craig Martin&lt;/a&gt; here on Dev.to has written &lt;a href="https://dev.to/craigmichaelmartin/the-problem-with-promises-in-javascript-5h46"&gt;a great article&lt;/a&gt; on the pitfalls of &lt;code&gt;Promise&lt;/code&gt;s in JavaScript.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Definitely&lt;/em&gt; read that article for a much better explanation on this than I could provide you. I'll quote him directly from the Intro of his article there with his three biggest issues with Promises:&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Promises have an API which encourages casually dangerous code&lt;/li&gt;
&lt;li&gt;Promises lack a convenient API to safely work with data.&lt;/li&gt;
&lt;li&gt;Promises co-mingle rejected promises and unintended runtime exceptions&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;We always have tradeoffs in writing code and a &lt;code&gt;Promise&lt;/code&gt; is no different.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Good
&lt;/h3&gt;

&lt;p&gt;Using a &lt;code&gt;Promise&lt;/code&gt; can result in some more succinct, clean code than nesting callbacks. Take that with a grain of salt because again we've all got our preferences and that may not be true for all!&lt;/p&gt;

&lt;p&gt;I also believe it's easier to debug and separate concerns as we can modularize our code a little more when converting to Promises.&lt;/p&gt;

&lt;p&gt;I took just the first part of the above &lt;strong&gt;callback&lt;/strong&gt; example and converted it to return a &lt;code&gt;Promise&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;readDirPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&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="c1"&gt;// Return a new Promise&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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="c1"&gt;// Do the actual file system read&lt;/span&gt;
    &lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;files&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="c1"&gt;// If there's a problem reject with error thrown&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;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="c1"&gt;// Else we succeed and resolve with the files&lt;/span&gt;
      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;files&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;readDirPromise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Do something with data */&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Handle error */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way we don't have to nest a whole lot. We place this piece of logic in a function that will either &lt;strong&gt;resolve&lt;/strong&gt; and move on or &lt;strong&gt;reject&lt;/strong&gt; and show us the error. &lt;/p&gt;

&lt;p&gt;If we continued to convert that &lt;em&gt;whole callback example&lt;/em&gt; we would simply take each part with a specific task and place that inside its own function like &lt;code&gt;readDirPromise&lt;/code&gt; and call those in the &lt;code&gt;then()&lt;/code&gt; statements.&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;readDirPromise&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* secondStep() */&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* thirdStep() */&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* fourthStep() */&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* Handle error */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each time we chain &lt;code&gt;then()&lt;/code&gt;s we're expecting the last step to &lt;strong&gt;resolve&lt;/strong&gt; or &lt;strong&gt;succeed&lt;/strong&gt;. If at any point &lt;em&gt;there is an error&lt;/em&gt; our single &lt;code&gt;catch()&lt;/code&gt; statement will handle it instead of having an &lt;code&gt;if (err)&lt;/code&gt; a whole bunch. Then we can place the code for handling each type of error in just the &lt;code&gt;catch()&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;I think &lt;code&gt;Promise&lt;/code&gt;s are great and want to do more with them. &lt;em&gt;That said&lt;/em&gt; I worked with &lt;strong&gt;callbacks&lt;/strong&gt; first and certainly have an appreciation for them. Especially in simpler tasks it might be easier just to use &lt;strong&gt;callbacks&lt;/strong&gt;. Once more they're present in &lt;strong&gt;a lot of code&lt;/strong&gt; because that was the way to achieve our asynchronous goals in the dark pre-ES6 days.&lt;/p&gt;

&lt;p&gt;Having a solid understanding of the foundational pieces of JavaScript, in my mind, helps us understand and appreciate all the awesome, new features we've gotten in just the last handful of years.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>JavaScript Array.flat()</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 20 Jul 2020 15:29:05 +0000</pubDate>
      <link>https://forem.com/stlnick/javascript-array-flat-4l8f</link>
      <guid>https://forem.com/stlnick/javascript-array-flat-4l8f</guid>
      <description>&lt;p&gt;Array methods in JavaScript can be &lt;em&gt;super&lt;/em&gt; helpful. This method is no exception!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Array.flat()&lt;/code&gt; is a method that will in short simplify an array for you. The two big things it can do is take an array that has nested elements and return a new array with less nesting and it can remove empty array elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Removing nested Arrays
&lt;/h3&gt;

&lt;p&gt;This is truly the biggest help of this method. The only argument that &lt;code&gt;flat()&lt;/code&gt; takes is the depth of which you'd like to go and reduce the nesting. The default, if you provide no argument since it's optional, is &lt;code&gt;flat(1)&lt;/code&gt; or depth 1.&lt;/p&gt;

&lt;p&gt;Take this array for 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;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We see it has two elements at indices &lt;code&gt;[3]&lt;/code&gt; and &lt;code&gt;[5]&lt;/code&gt; that are themselves array. What if you need the sum of these numbers? Well you could write your own function to test if the elements are arrays before adding to deal with the nesting.&lt;/p&gt;

&lt;p&gt;Or we can use &lt;code&gt;flat()&lt;/code&gt; and then you'd be able to use &lt;code&gt;reduce()&lt;/code&gt; to get your sum in a much simpler way.&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;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;9&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;flatNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&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;flatNums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: [1, 2, 3, 4, 5, 6, 7, 8, 9]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using &lt;code&gt;flat()&lt;/code&gt; gives a simple, one-level array. In the example above we didn't pass an argument because the default is &lt;code&gt;1&lt;/code&gt; and that is as far down we needed to go.&lt;/p&gt;

&lt;p&gt;To visualize the nesting levels check this out. Each comment will state the &lt;strong&gt;deepest&lt;/strong&gt; level each array has and the number of elements. Each time we nest an array we add a level to &lt;strong&gt;depth&lt;/strong&gt;. Also, just like array indices - we start at &lt;code&gt;0&lt;/code&gt; and can say the first level is equivalent to &lt;code&gt;depth = 0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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="mi"&gt;2&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="mi"&gt;4&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;// Depth: 0 - Elements: 5&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="mi"&gt;2&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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;// Depth: 1 - Elements: 4&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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;// Depth: 2 - Elements: 3&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="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="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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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;// Depth: 3 - Elements: 2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe a simpler example to look at is this one:&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="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="mi"&gt;2&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="mi"&gt;4&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;// Depth: 0 - Elements: 5&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;// Depth: 1 - Elements: 5&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;// Depth: 2 - Elements: 5&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;// Depth: 3 - Elements: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now lets talk more about...&lt;/p&gt;

&lt;h3&gt;
  
  
  Depth
&lt;/h3&gt;

&lt;p&gt;Each example above on the last line reaches a depth of &lt;code&gt;3&lt;/code&gt; because that is &lt;em&gt;the number of additional brackets we would need to use to reach the deepest element&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For us to reach the number &lt;code&gt;5&lt;/code&gt; in this array &lt;code&gt;const arr = [1, 2, 3, 4, [[[5]]]]&lt;/code&gt; we would need to use &lt;strong&gt;4 sets of brackets&lt;/strong&gt; in our notation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;One set&lt;/strong&gt; to access the element where the nested arrays are stored and &lt;strong&gt;three sets&lt;/strong&gt; to reach the &lt;code&gt;5&lt;/code&gt;. That would look like this &lt;code&gt;arr[4][0][0][0]&lt;/code&gt;. The &lt;code&gt;arr[4]&lt;/code&gt; grabs &lt;code&gt;[[[5]]]&lt;/code&gt; and then the next two &lt;code&gt;[0]&lt;/code&gt; accesses the nested arrays and finally the last &lt;code&gt;[0]&lt;/code&gt; will grab &lt;code&gt;5&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;// Depth: 3&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// output: [[[5]]]&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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: [[5]]&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="c1"&gt;// output: [5]&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;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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="mi"&gt;0&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: 5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like a headache to deal with all that bracket notation, right?&lt;/p&gt;

&lt;p&gt;That's where &lt;code&gt;flat()&lt;/code&gt; comes to the rescue. We can take those structures and &lt;em&gt;flatten them out&lt;/em&gt; to be a single level array and use one set of brackets to access any element we need.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;IF we know the exact depth of our deepest nested array&lt;/em&gt; we can simply pass in the appropriate argument.&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="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="mi"&gt;2&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="mi"&gt;4&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;// No flat() needed&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;// flat(1)&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;// flat(2)&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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;// flat(3)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each of those will return the one-level array &lt;code&gt;[1, 2, 3, 4, 5]&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now what if we're not sure of the depth needed to flatten an array? What if we're receiving this data randomly? Or if we just want a way to flatten all arrays &lt;em&gt;without&lt;/em&gt; worrying about 'what is the deepest level'?&lt;/p&gt;

&lt;p&gt;There is a simple way to achieve that:&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;const&lt;/span&gt; &lt;span class="nx"&gt;arr1&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr2&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arr3&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&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="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&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="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;Infinity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;Infinity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;span class="nx"&gt;arr1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;Infinity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// output: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Passing &lt;code&gt;Infinity&lt;/code&gt; will ensure the deepest level is always reached and return a one-level array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Removing empty elements
&lt;/h3&gt;

&lt;p&gt;One neat feature, that may not be intuitive from the name and it's other main function, of &lt;code&gt;flat()&lt;/code&gt; is it will remove any empty elements.&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;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&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="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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="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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// output: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will work also if a nested array contains or is simply empty elements.&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;const&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&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="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="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="p"&gt;[&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;4&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="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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;flat&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="c1"&gt;// output: [1, 2, 3, 4, 5]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why should we use &lt;code&gt;flat()&lt;/code&gt;?
&lt;/h3&gt;

&lt;p&gt;The biggest benefit of &lt;code&gt;flat()&lt;/code&gt; is that it &lt;strong&gt;normalizes&lt;/strong&gt; our array. We can use it to ensure that our array is in an easy to use format that won't give us any weird situations.&lt;/p&gt;

&lt;p&gt;Having data in a consistent format allows us to utilize them with more consistent results and our programs to behave as we expect. At least we hope!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Scope in JavaScript</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Fri, 17 Jul 2020 16:49:10 +0000</pubDate>
      <link>https://forem.com/stlnick/scope-in-javascript-26ji</link>
      <guid>https://forem.com/stlnick/scope-in-javascript-26ji</guid>
      <description>&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;p&gt;This one is definitely important to having your code run properly or as expected. If you're accessing a variable that's in a separate closure than the one you're trying to access from you won't get the result you need.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Scope?
&lt;/h2&gt;

&lt;p&gt;In my own simple words, &lt;strong&gt;scope&lt;/strong&gt; can be thought of as a 'field of view'.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pirate Ship Analogy
&lt;/h4&gt;

&lt;p&gt;Think of an old Pirate ship. HOLD ON - just stay with me.&lt;/p&gt;

&lt;p&gt;Let's say we have 3 Pirates named &lt;strong&gt;Jack&lt;/strong&gt;, &lt;strong&gt;Anne&lt;/strong&gt; and &lt;strong&gt;Bill&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anne&lt;/strong&gt; positioned on the Main Deck has a specific field of view. &lt;strong&gt;Jack&lt;/strong&gt; in the brig below looking through the open grate has his own view as well. &lt;strong&gt;Bill&lt;/strong&gt; way up in the Crow's Nest looking out for other ships has a different field of view.&lt;/p&gt;

&lt;p&gt;Now we could say that &lt;strong&gt;Jack&lt;/strong&gt; in the brig below has the most limited view. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anne&lt;/strong&gt; on the deck can see what &lt;strong&gt;Jack&lt;/strong&gt; sees and even more.&lt;/p&gt;

&lt;p&gt;Then &lt;strong&gt;Bill&lt;/strong&gt; in the crow's nest can see all of what &lt;strong&gt;Anne&lt;/strong&gt; and &lt;strong&gt;Jack&lt;/strong&gt; can see.&lt;/p&gt;

&lt;p&gt;Lastly, &lt;em&gt;they all can see&lt;/em&gt; the open sea - which for our purposes will be the &lt;strong&gt;global scope&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Field of view&lt;/strong&gt;: &lt;em&gt;Global&lt;/em&gt; &amp;gt; Bill &amp;gt; Anne &amp;gt; Jack&lt;/p&gt;

&lt;h4&gt;
  
  
  Pirate Ship Scenario #1
&lt;/h4&gt;

&lt;p&gt;Let's say the Captain is on the Upper Deck - which is above the Main Deck and below the Crow's Nest.&lt;/p&gt;

&lt;p&gt;What if &lt;strong&gt;Jack&lt;/strong&gt; wanted to know where the Captain of the ship was? He most certainly can't see him inside the brig - so he asks &lt;strong&gt;Anne&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Anne&lt;/strong&gt; on the Main Deck can't really see the Upper Deck from her field of view. So she's not sure either.&lt;/p&gt;

&lt;p&gt;Now they ask &lt;strong&gt;Bill&lt;/strong&gt; all the way up in the Crow's Nest can see the Captain on the Upper Deck so he passes that information down to &lt;strong&gt;Anne&lt;/strong&gt; and down to &lt;strong&gt;Jack&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Must be some nice Pirates to even pay attention to &lt;strong&gt;Jack&lt;/strong&gt; in the brig... Anyway, lets see what that could look like in code.&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;Bill&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;CaptainLocation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Upper Deck&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Anne&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Jack&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;CaptainLocation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;Jack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;Anne&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;Bill&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;Jack()&lt;/code&gt; is trying to log the Captain's Location but it's not within his scope or, in other words, his &lt;code&gt;{}&lt;/code&gt;. It's not it &lt;code&gt;Anne()&lt;/code&gt; either so that one &lt;code&gt;console.log()&lt;/code&gt; reaches all the way up to &lt;code&gt;Bill()&lt;/code&gt; to find the value.&lt;/p&gt;

&lt;h4&gt;
  
  
  Pirate Ship Scenario #2
&lt;/h4&gt;

&lt;p&gt;Now if any one of our friendly Pirates wanted to know if it's storming. They could easily determine that looking to the open sea or the &lt;strong&gt;global scope&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isStorming&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Bill&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;isStorming&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Anne&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;isStorming&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Jack&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;isStorming&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;Jack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;Anne&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;Bill&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;global scope&lt;/strong&gt; gives access to its information to all things in our code because each line of our code is &lt;em&gt;enclosed&lt;/em&gt; inside it.&lt;/p&gt;

&lt;p&gt;When we create closures, most commonly through declaring a function, we put a set of blinders on &lt;em&gt;whatever goes inside&lt;/em&gt; the &lt;code&gt;{}&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now let's return to a way more basic, less ridiculous set of examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some basic examples
&lt;/h2&gt;

&lt;p&gt;Let's roll through a couple scenarios to see how these closures affect what our super complex variable &lt;code&gt;x&lt;/code&gt; resolves to in each &lt;code&gt;console.log()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Global scope : x = 0&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func1&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// func1() scope : x = 5&lt;/span&gt;
    &lt;span class="nx"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func2&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// func2() scope : x = 8&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="cm"&gt;/* output : 0
              5
              8
  */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because each &lt;code&gt;console.log(x)&lt;/code&gt; has its own scope and as such will have a different value when accessed. &lt;/p&gt;




&lt;p&gt;If we modify that slightly to remove the &lt;code&gt;x&lt;/code&gt; declaration from &lt;code&gt;func2()&lt;/code&gt; then you'll see it goes to the &lt;strong&gt;global scope&lt;/strong&gt; and finds &lt;code&gt;x&lt;/code&gt; and therefore logs &lt;code&gt;0&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// x = 0&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func1&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// x = 5&lt;/span&gt;
    &lt;span class="nx"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func2&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/* x no longer declared here! */&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// x = 0&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="cm"&gt;/* output : 0
              5
              0
  */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why did &lt;code&gt;x&lt;/code&gt; resolve to &lt;code&gt;0&lt;/code&gt; instead of &lt;code&gt;5&lt;/code&gt;? &lt;code&gt;func2()&lt;/code&gt; called &lt;code&gt;func1()&lt;/code&gt;, right?!&lt;/p&gt;

&lt;p&gt;Even though &lt;code&gt;func2()&lt;/code&gt; is called &lt;em&gt;inside&lt;/em&gt; of &lt;code&gt;func1()&lt;/code&gt; it &lt;strong&gt;is not ENCLOSED&lt;/strong&gt; by &lt;code&gt;func1()&lt;/code&gt;. So &lt;code&gt;func2()&lt;/code&gt; tries to find x in &lt;em&gt;its own scope first&lt;/em&gt;, doesn't find it so it moves to its &lt;em&gt;enclosing scope&lt;/em&gt; which is the &lt;strong&gt;global scope&lt;/strong&gt;.&lt;/p&gt;




&lt;p&gt;Now if we move the declaration of &lt;code&gt;func2()&lt;/code&gt; inside of &lt;code&gt;func1()&lt;/code&gt; it will go like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Try to find &lt;code&gt;x&lt;/code&gt; in local scope (or &lt;code&gt;func2()&lt;/code&gt; scope basically)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; &lt;em&gt;not found&lt;/em&gt; -&amp;gt; move up in scope&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Try to find &lt;code&gt;x&lt;/code&gt; in the enclosing scope (or &lt;code&gt;func1()&lt;/code&gt; scope)

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x&lt;/code&gt; &lt;strong&gt;found&lt;/strong&gt; -&amp;gt; &lt;code&gt;x = 5&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Global scope : x = 0&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func1&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;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// func1() scope : x = 5&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func2&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;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// func2() scope : x = 5&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;func2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;func1&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="cm"&gt;/* output : 0
              5
              5
  */&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lastly, if we were to remove the &lt;code&gt;x&lt;/code&gt; declaration from &lt;code&gt;func2()&lt;/code&gt; then no matter if &lt;code&gt;func1()&lt;/code&gt; is declared &lt;em&gt;inside or outside&lt;/em&gt; of &lt;code&gt;func2()&lt;/code&gt; we will see &lt;code&gt;x = 0&lt;/code&gt; because both functions do not find &lt;code&gt;x&lt;/code&gt; in their respective scopes and must eventually move to the global scope to find an &lt;code&gt;x&lt;/code&gt; value.&lt;/p&gt;




&lt;p&gt;This can most definitely be a point of confusion. I know it was for me and I had to double check all of these outputs! &lt;/p&gt;

&lt;p&gt;What I recommend, simply because it helped me become more familiar and ensure I'm getting the right value, is to do what you've seen in these examples.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log()&lt;/code&gt; the heck out of your values if something doesn't seem right &lt;em&gt;or&lt;/em&gt; just to be safe.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log()&lt;/code&gt; is your friend! So is &lt;strong&gt;scope&lt;/strong&gt; but it's that one friend who can definitely get on your nerves sometimes.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>es6</category>
    </item>
    <item>
      <title>Arrow Function Expressions - The Basics</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 13 Jul 2020 16:59:56 +0000</pubDate>
      <link>https://forem.com/stlnick/arrow-function-expressions-the-basics-320n</link>
      <guid>https://forem.com/stlnick/arrow-function-expressions-the-basics-320n</guid>
      <description>&lt;p&gt;ES6, or ECMAScript 2015, brought us many great changes to the language of JavaScript. One such feature is &lt;strong&gt;Arrow Functions&lt;/strong&gt;. That's what we'll dive into in this article - get to reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an Arrow Function Expression?
&lt;/h2&gt;

&lt;p&gt;An Arrow Function Expression is simply a new syntax to write function expressions as we've done before. In a lot of cases it can be much shorter to write which is great. If your task requires less typing you'll get more done in the same amount of time! &lt;/p&gt;

&lt;p&gt;Granted you understand it - which I most certainly &lt;em&gt;did not&lt;/em&gt; in my first encounters with the mighty &lt;code&gt;=&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's a very basic example (which there will be more of):&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="cm"&gt;/* * These are equivalent * */&lt;/span&gt;
&lt;span class="c1"&gt;// ES5&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;func&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Function Expression example.&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="nx"&gt;func&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// ES6&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;arrow&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="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;Arrow Function Expression.&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="nx"&gt;arrow&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are multiple aspects to the usage of Arrow Functions such as when it's best to use and not use them and some small gotchas on when they behave differently than an normal Function Expression. We won't cover them all here but we will cover some of the bigger ones.&lt;/p&gt;

&lt;h2&gt;
  
  
  Which is better?
&lt;/h2&gt;

&lt;p&gt;Wrong question to ask. You can accomplish your task using either method. People have opinions on this subject all around - but lets face it that will never change.&lt;/p&gt;

&lt;p&gt;However, there are certain scenarios where a strong case can be made to use or &lt;em&gt;not use&lt;/em&gt; an Arrow Function.&lt;/p&gt;

&lt;h4&gt;
  
  
  When to Use
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;When your resulting code will be shorter and more concise&lt;/li&gt;
&lt;li&gt;When your code becomes more readable&lt;/li&gt;
&lt;li&gt;When you feel like it!&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  When &lt;em&gt;not&lt;/em&gt; to Use
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;As an object method directly&lt;/li&gt;
&lt;li&gt;When your resulting code isn't any shorter or concise&lt;/li&gt;
&lt;li&gt;When your code can become more confusing or hard to read and understand&lt;/li&gt;
&lt;li&gt;When you &lt;strong&gt;do not&lt;/strong&gt; feel like it!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep in mind it largely comes down to personal preference on what you believe is more readable, more concise, less confusing, etc. As mentioned there are a couple things to be aware of when choosing which route to take.&lt;/p&gt;

&lt;h4&gt;
  
  
  Arrow Function: To code or not to code?
&lt;/h4&gt;

&lt;p&gt;One big item to know is Arrow Functions are &lt;strong&gt;&lt;em&gt;lexically bound&lt;/em&gt;&lt;/strong&gt;. Let's take a look at a couple examples first:&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;// ES5&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Foo Bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setTimeout&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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="k"&gt;this&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order for the &lt;code&gt;logName()&lt;/code&gt; method to actually log out the &lt;code&gt;name&lt;/code&gt; property we must ensure the &lt;code&gt;setTimeout&lt;/code&gt; binds &lt;code&gt;this&lt;/code&gt;. We see that it is chained at the end of &lt;code&gt;setTimeout&lt;/code&gt; - if we did not do that then &lt;code&gt;this&lt;/code&gt; would actually be &lt;code&gt;undefined&lt;/code&gt; since the callback function of &lt;code&gt;setTimeout&lt;/code&gt; &lt;em&gt;does not have its own&lt;/em&gt; &lt;code&gt;this&lt;/code&gt;. We explicitly bind it to the &lt;code&gt;this&lt;/code&gt; of &lt;code&gt;logName&lt;/code&gt; which is of course &lt;code&gt;person&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ES6&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Foo Bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;logName&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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="c1"&gt;// No binding of 'this'&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;This time with Arrow Functions we do not have to ensure binding of &lt;code&gt;this&lt;/code&gt; to properly log it. IN THIS SCENARIO. (Gotcha comin' up...)&lt;/p&gt;

&lt;p&gt;The catch here is that &lt;strong&gt;Arrow Functions &lt;em&gt;cannot&lt;/em&gt; be bound to a &lt;code&gt;this&lt;/code&gt;&lt;/strong&gt; so it will go up in scope to find the value of &lt;code&gt;this&lt;/code&gt; in the context which it was called - AKA &lt;strong&gt;&lt;em&gt;lexically bound&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It's not recommended to use Arrow Functions &lt;strong&gt;directly&lt;/strong&gt; as object methods like the following:&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;var&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Foo Bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;logName&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;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  &lt;span class="c1"&gt;// =&amp;gt; instead of function expression&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="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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;this.name&lt;/code&gt; will return &lt;code&gt;undefined&lt;/code&gt;. It's a little confusing but a key thing to remember is that again an Arrow Function will not have a &lt;code&gt;this&lt;/code&gt; attached to itself. I like to think of Arrow Functions just passing the buck. &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;console.log&lt;/code&gt; tries to evaluate &lt;code&gt;this&lt;/code&gt; so it goes to &lt;code&gt;setTimeout&lt;/code&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it says "I don't know a &lt;code&gt;this&lt;/code&gt; maybe try to ask &lt;code&gt;logName&lt;/code&gt;"&lt;/li&gt;
&lt;li&gt;we get to &lt;code&gt;logName&lt;/code&gt; (which is also defined as an =&amp;gt; so it cannot bind a &lt;code&gt;this&lt;/code&gt;) and ask and it says "&lt;code&gt;this&lt;/code&gt; doesn't ring a bell, maybe ask &lt;code&gt;person&lt;/code&gt;?" &lt;/li&gt;
&lt;li&gt;now we ask &lt;code&gt;person&lt;/code&gt; and it's like "Hey I don't have any property called &lt;code&gt;this&lt;/code&gt; I can't help."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eventually we get all the way to the global &lt;code&gt;Window&lt;/code&gt; object because that's the default when &lt;code&gt;this&lt;/code&gt; isn't bound to anything in the context which we called it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use an Arrow Function?
&lt;/h2&gt;

&lt;p&gt;Okay, that was confusing. &lt;code&gt;this&lt;/code&gt; in relation to Arrow Functions is arguably the hardest thing to grasp. So let's talk about something easier to grasp and results in some clean looking code.&lt;/p&gt;

&lt;p&gt;As mentioned, Arrow Functions can result in much shorter, cleaner code for us and especially in short function definitions or when using &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;reduce&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's take a look.&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;// ES5&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;addTwoNums&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// ES6&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTwoNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Doesn't look any shorter in ES6, right? One great feature of Arrow Functions is if we have one statement and want to return we don't use the {} or the &lt;code&gt;return&lt;/code&gt; keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addTwoNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&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;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's an implicit return when no braces or &lt;code&gt;return&lt;/code&gt; is present with an Arrow Function. This really helps when it comes to the readability of a &lt;code&gt;map&lt;/code&gt; or &lt;code&gt;reduce&lt;/code&gt; usage.&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;const&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubledNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may have noticed this time when using the Arrow Function in &lt;code&gt;map&lt;/code&gt; we didn't put parenthesis around the parameter.&lt;/p&gt;

&lt;p&gt;When there is &lt;strong&gt;only one&lt;/strong&gt; parameter for an Arrow Function the parenthesis are &lt;em&gt;optional&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;So parenthesis can be optional and in certain scenarios the brackets and &lt;code&gt;return&lt;/code&gt; can be omitted providing us multiple ways to write the same code. Going for the shortest code is usually what I would do - but again, personal preference.&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="cm"&gt;/* * These are ALL equivalent * */&lt;/span&gt;
&lt;span class="c1"&gt;// ES5&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubledNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&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="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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&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;// ES6&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubledNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&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="o"&gt;=&amp;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;num&lt;/span&gt; &lt;span class="o"&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;// ES6: No brackets - implicit return&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubledNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&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="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&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;// ES6: Single parameter - no parameter parenthesis&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;doubledNums&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Arrow Functions have more characteristics and gotchas than listed here but these are the basics I focused on to get a grasp. Once you get an understanding of these concepts take a peek at the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;MDN Arrow Functions&lt;/a&gt; page. You'll find plenty more use cases and confusing things to learn! &lt;/p&gt;

&lt;p&gt;Don't get me wrong - I keep saying confusing because it's true! I learn more each day - new things I hadn't heard of or known and how my understanding of something wasn't quite right. Let me know in the comments if there's an explanation here that isn't quite right or if there's an interesting gotcha you know of.&lt;/p&gt;

&lt;p&gt;I still love JavaScript and this Tweet speaks to that pretty well!&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--XyyRSsrP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1255562113629802498/nTk-e7L5_normal.jpg" alt="Stefan Natter 🇦🇹👋🏻 profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Stefan Natter 🇦🇹👋🏻
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="mentioned-user" href="https://dev.to/natterstefan"&gt;@natterstefan&lt;/a&gt;
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ir1kO05j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      Dear Developers,&lt;br&gt;&lt;br&gt;One thing is for sure: I may drive you crazy sometimes; but in the end, you fall in love with me each and every day again.&lt;br&gt;&lt;br&gt;best regards,&lt;br&gt;JavaScript ❤️
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      16:16 PM - 11 Jul 2020
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1281985733079764993" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFnoeFxk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1281985733079764993" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k6dcrOn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1281985733079764993" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRQc9lOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


</description>
      <category>javascript</category>
      <category>codenewbie</category>
      <category>es6</category>
    </item>
    <item>
      <title>Listening for a Form Submit in JavaScript</title>
      <dc:creator>Nick</dc:creator>
      <pubDate>Mon, 06 Jul 2020 15:09:40 +0000</pubDate>
      <link>https://forem.com/stlnick/listening-for-a-form-submit-in-javascript-4n91</link>
      <guid>https://forem.com/stlnick/listening-for-a-form-submit-in-javascript-4n91</guid>
      <description>&lt;h2&gt;
  
  
  What is a Form Submit?
&lt;/h2&gt;

&lt;p&gt;Simply put it's submitting a form... Basically, when you fill the form out with whatever information it asks for you want to do something with that information. We can do all manner of operations with this data and that's dependent upon your application's tasks and the purpose of the form. &lt;/p&gt;

&lt;p&gt;A contact form should provide ways to get in touch with a specific person; a suggestion form should include an issue or improvement to be logged; an email subscription sign-up should have... well, an email. You get the point.&lt;/p&gt;

&lt;h2&gt;
  
  
  How do we listen for the submission?
&lt;/h2&gt;

&lt;p&gt;First, we're grabbing the element (the form in this case) that we want to listen to. Keep in mind, this is using the basic &lt;em&gt;form element selector&lt;/em&gt;. If you have multiple forms on your page this will only return the &lt;strong&gt;first occurrence&lt;/strong&gt; of a form.&lt;/p&gt;

&lt;p&gt;If you do have multiple forms, you'll want to add an &lt;code&gt;id&lt;/code&gt; tag to &lt;em&gt;each form&lt;/em&gt; specifying what it's for - that way you can safely select the form you want and attach the appropriate event listener.&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;const&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&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="nx"&gt;event&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="c1"&gt;// Add code for functionality you need here&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you can simply chain the method calls like so...&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;form&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;submit&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="nx"&gt;event&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="c1"&gt;// Add code for functionality you need here&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This chaining is especially useful when you know you'll only have one event to listen for. No need to grab the element in question and store it in a variable if you're only going to add one event listener.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why are we not listening to the submit button?
&lt;/h2&gt;

&lt;p&gt;For us to have access to the information the user has entered we listen &lt;code&gt;form&lt;/code&gt;. The &lt;code&gt;button&lt;/code&gt; will not actually be targeted by a submit event. The &lt;code&gt;button&lt;/code&gt; is the &lt;strong&gt;submitter&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This is apparent when you look in the &lt;code&gt;SubmitEvent&lt;/code&gt; object that is generated when a form is submitted; there is a &lt;code&gt;submitter&lt;/code&gt; property in which you'll see the element triggering the submission. So we listen for submission on the &lt;code&gt;form&lt;/code&gt; and the &lt;code&gt;button&lt;/code&gt; is the one that &lt;strong&gt;&lt;em&gt;triggers&lt;/em&gt;&lt;/strong&gt; that event. &lt;/p&gt;

&lt;p&gt;Our submit &lt;code&gt;button&lt;/code&gt; plays a pivotal role to get this all to work, however, our &lt;code&gt;form&lt;/code&gt; is the element on which the submit is actually fired and thus the element we should listen to.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>html</category>
    </item>
  </channel>
</rss>
