<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Barkley Santo</title>
    <description>The latest articles on DEV Community by Barkley Santo (@barksanto).</description>
    <link>https://dev.to/barksanto</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%2F860650%2F3078463e-0906-4ab2-8214-2b69139923a1.JPG</url>
      <title>DEV Community: Barkley Santo</title>
      <link>https://dev.to/barksanto</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/barksanto"/>
    <language>en</language>
    <item>
      <title>React Native Conditional Button styling for iOS.</title>
      <dc:creator>Barkley Santo</dc:creator>
      <pubDate>Thu, 14 Jul 2022 10:05:54 +0000</pubDate>
      <link>https://dev.to/barksanto/react-native-conditional-button-styling-for-ios-h32</link>
      <guid>https://dev.to/barksanto/react-native-conditional-button-styling-for-ios-h32</guid>
      <description>&lt;p&gt;React Native (RN) allows us to write code in one language for mobile apps instead of resorting to writing platform specific code.  &lt;/p&gt;

&lt;p&gt;However there are some things in RN that do require platform specific code. &lt;/p&gt;

&lt;p&gt;Below we have a custom button component, the effect we're looking at now is the button changing color when pressed 👆🏾. &lt;/p&gt;

&lt;p&gt;RN gives us a special property we can use for this effect, it's called &lt;code&gt;android_ripple&lt;/code&gt;. Once the button is pressed, the ripple property applies the color we provided to the button. &lt;/p&gt;

&lt;p&gt;Check it out!&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonOuterContainer&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt;
        &lt;span class="na"&gt;onPress&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pressHandler&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;android_ripple&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#540233&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonText&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;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/A90bG3AuvNLhUwpvbl/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/A90bG3AuvNLhUwpvbl/giphy.gif" alt="android touch gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But since this is an Android property 👽, it won't work on iOS devices, so we need to finesse some JS for this. &lt;/p&gt;

&lt;p&gt;The style attribute in a component can either take a style object directly or an arrow function, so we can run some JS directly inside of it.&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonOuterContainer&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt;
        &lt;span class="na"&gt;onPress&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;pressHandler&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;android_ripple&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#540233&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;//Android color change&lt;/span&gt;


        &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;pressed&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// iOS color change&lt;/span&gt;
          &lt;span class="nx"&gt;pressed&lt;/span&gt;
            &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonInnerContainer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pressed&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonInnerContainer&lt;/span&gt;
        &lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;buttonText&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;children&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Pressable&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's what we get. &lt;br&gt;
&lt;a href="https://i.giphy.com/media/m626PX0AP6KLJTk7AG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/m626PX0AP6KLJTk7AG/giphy.gif" alt="ios touch gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Pressable component automatically passes a &lt;code&gt;pressed&lt;/code&gt; value (which is a boolean) to the function, and then we can use that boolean in a ternary operator. &lt;/p&gt;

&lt;p&gt;We’re allowed to return/add &lt;u&gt;multiple styles objects&lt;/u&gt; in an array from this ternary operator, so we’re not limited to one ‘key’ from the stylesheet in this case. We're applying both the base button style &lt;em&gt;and&lt;/em&gt; the button press color change here. &lt;/p&gt;

&lt;p&gt;It ends up reading as “if pressed is true, apply both style objects from this array, if not, just apply a single style”. &lt;/p&gt;

&lt;p&gt;A quick one, but good to know, cheers 🥂&lt;/p&gt;

&lt;p&gt;A puppy for your troubles (&lt;a href="https://unsplash.com/@fattycorgi" rel="noopener noreferrer"&gt;source&lt;/a&gt;):&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tgjmczly3t404ofbmf7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4tgjmczly3t404ofbmf7.png" alt="a pupper"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cover image by &lt;a href="https://unsplash.com/@katya" rel="noopener noreferrer"&gt;Katya Ross&lt;/a&gt;&lt;/p&gt;

</description>
      <category>reactnative</category>
      <category>javascript</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Submit form data from client to Node.js (API)</title>
      <dc:creator>Barkley Santo</dc:creator>
      <pubDate>Thu, 30 Jun 2022 11:55:16 +0000</pubDate>
      <link>https://dev.to/barksanto/submit-form-data-from-client-to-nodejs-api-2p16</link>
      <guid>https://dev.to/barksanto/submit-form-data-from-client-to-nodejs-api-2p16</guid>
      <description>&lt;p&gt;This is a fun one! &lt;/p&gt;

&lt;p&gt;It's common practice to have a form or input on a website where visitors can enter something in a search field and then expect some data back. &lt;/p&gt;

&lt;p&gt;So let's take a look at how we can do that!&lt;/p&gt;

&lt;p&gt;Dependencies we'll use for this project:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Express (a Node.js framework that provides broad features for building web and mobile applications)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Nodemon (so our server automatically updates when we change the anything in our server file)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first thing we're going to want to do is start a project with a basic index.html file. You can add this to the body of your HTML page 😎!&lt;/p&gt;

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

  &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Welcome our test site! 👋🏾&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/find"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"POST"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"search-item"&lt;/span&gt; &lt;span class="na"&gt;placeholder=&lt;/span&gt;&lt;span class="s"&gt;"Search here!"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Search&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;And here are some basic styles for your stylesheet:&lt;/p&gt;

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

  &lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;79&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;204&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Arial&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Helvetica&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="nb"&gt;auto&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;white&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-weight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bold&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;rgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;79&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;204&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;6px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;4px&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;We'll go over the HTML portion in a few. Let's build our node server now!&lt;/p&gt;

&lt;p&gt;First, in your terminal, run the following command:&lt;br&gt;
&lt;code&gt;npm init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once you press enter, you'll be prompted to answer a few questions in the terminal, but you don't need to fill those out right now, you can always edit them later on directly in the file; just press enter until the questions go away.&lt;br&gt;
This will initiate a package.json file in your project where your dependencies will live. &lt;/p&gt;

&lt;p&gt;The package.json file should look like this after you run the command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 json
{
  "name": "blogcode",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" &amp;amp;&amp;amp; exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC"
}



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

&lt;/div&gt;

&lt;p&gt;Now we can add those dependencies mentioned in the beginning by running the following commands. &lt;br&gt;
&lt;code&gt;npm install express&lt;/code&gt;&lt;br&gt;
and&lt;br&gt;
&lt;code&gt;npm install --save-dev nodemon&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;NOTE 📝: We're saving nodemon as a dev dependency because it's solely for our benefit when testing locally, it's not needed in production, so we add the &lt;code&gt;--save--dev&lt;/code&gt; flag to tell our package.json that it's not needed past our testing phase.&lt;/p&gt;

&lt;p&gt;Here's what our package.json file looks like now after adding the dependencies. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpf1pu93l1gjbzm0gvbt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqpf1pu93l1gjbzm0gvbt.png" alt="packagejson"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's manually add one more line to the package.json file so when we're testing locally, we can use Nodemon to automatically update our server instead of manually needing to refresh it every time we make a change.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvfbfk0jze8pnxqtjhtl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjvfbfk0jze8pnxqtjhtl.png" alt="packagejson2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can create our server now! Go ahead and paste this code into your server file (I named mine server.js for convenience):&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;express&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&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;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3000&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;extended&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="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&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;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/index.html&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Example app listening on port &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;In the first three lines, we are setting up our server variables. &lt;br&gt;
&lt;code&gt;const express = require("express")&lt;/code&gt; : here we're importing the express package we installed earlier (&lt;code&gt;npm install express&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const app = express()&lt;/code&gt; - creates our express app. &lt;br&gt;
&lt;code&gt;const port = 3000&lt;/code&gt; - we'll use this variable to tell our app which port to listen to.&lt;/p&gt;

&lt;p&gt;The following code is middleware, it parses incoming requests with urlencoded payloads and is based on body-parser. It pretty much allows us to read request objects 🤓, nothing crazy. &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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;urlencoded&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;extended&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;Now for the good stuff 🔥&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;app.get()&lt;/code&gt; is saying "Hey server, when we're on the root path/route, &lt;strong&gt;send&lt;/strong&gt; our HTML page to be displayed."&lt;/p&gt;

&lt;p&gt;And &lt;code&gt;app.listen()&lt;/code&gt; is binding the app to the port on our machine, in this case it's listening to port 3000 which we defined as a variable at the top of the server file. &lt;/p&gt;

&lt;p&gt;Now in our terminal we can run that package.json script we added a few steps ago, go ahead and type&lt;code&gt;npm run dev&lt;/code&gt; in your terminal, and you should see this: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7s4uhygr90se2czkj0h5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7s4uhygr90se2czkj0h5.png" alt="start server"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This means our server is running, and our app is listening for events that happen on that port!&lt;/p&gt;

&lt;p&gt;Remember that form we added to the HTML page? We have 3 important pieces here for our server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fas1jtjo5arl5vhysje22.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fas1jtjo5arl5vhysje22.png" alt="formCode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;1.) When the form is submitted, hit the &lt;code&gt;/find&lt;/code&gt; route on our server (we haven't made this route yet, but it's coming).&lt;/p&gt;

&lt;p&gt;2.) Make a POST request to the server, so we can do something with the data we're sending to this route.&lt;/p&gt;

&lt;p&gt;3.) The key we'll use for our response object. &lt;/p&gt;

&lt;p&gt;Let's make the &lt;code&gt;/find&lt;/code&gt; route in our server real quick. Add this to your server file:&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;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/find&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&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;searchItem&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;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;searchForThis&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;searchItem&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`We can search for this item on our backend:  &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;searchItem&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;



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

&lt;/div&gt;

&lt;p&gt;Now that our server is running, we can type something into our HTML input field and submit (click search). We'll see our input logged to our server that's running in our terminal, and then our response to our website will be whatever is in the &lt;code&gt;res.send()&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;Client side: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/GZ6aUc5NJ4bfziq7u2/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/GZ6aUc5NJ4bfziq7u2/giphy.gif" alt="gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Server: &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjk0zaicrt2rjjezbjb7k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjk0zaicrt2rjjezbjb7k.png" alt="server req"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And there you have it! Passing data from the client side to your server is an every-day task; so it's nice to have an idea on how to do these things! &lt;/p&gt;

&lt;p&gt;Now that you have that data on your server side, you can make requests to APIs to upload and request data using that value we passed along!&lt;/p&gt;

&lt;p&gt;If you have any questions or feedback, let me know, and we can share ideas! ✌🏾💙&lt;/p&gt;

&lt;p&gt;Here's a puppy:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fimages.unsplash.com%2Fphoto-1583511655826-05700d52f4d9%3Fixlib%3Drb-1.2.1%26ixid%3DMnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8%26auto%3Dformat%26fit%3Dcrop%26w%3D688%26q%3D80" 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%2Fimages.unsplash.com%2Fphoto-1583511655826-05700d52f4d9%3Fixlib%3Drb-1.2.1%26ixid%3DMnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8%26auto%3Dformat%26fit%3Dcrop%26w%3D688%26q%3D80" alt="puppy"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://unsplash.com/@karsten116" rel="noopener noreferrer"&gt;doggy photo: Karsten Winegeart 🤘🏾&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://unsplash.com/@purzlbaum" rel="noopener noreferrer"&gt;cover photo: Claudio Schwarz 🙏🏽&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The React useEffect hook!</title>
      <dc:creator>Barkley Santo</dc:creator>
      <pubDate>Wed, 15 Jun 2022 13:10:42 +0000</pubDate>
      <link>https://dev.to/barksanto/react-useeffect-hook-35b0</link>
      <guid>https://dev.to/barksanto/react-useeffect-hook-35b0</guid>
      <description>&lt;p&gt;Typically, when building a web application, we're using an API of some sort. We use that API to perform CRUD operations (Create, Read, Update, Delete) in our app depending on what actions our user is taking. &lt;/p&gt;

&lt;p&gt;For example, if we want to fetch a random image of a dog from the DOG API 🐶, our typical fetch would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="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="s2"&gt;https://dog.ceo/api/breeds/image/random&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;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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="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="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;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, once we check our developer console, we can see that response object in JSON format after we've cleaned the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="err"&gt;message:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"https://images.dog.ceo/breeds/terrier-irish/n02093991_3243.jpg"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;status:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"success"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in React we use State which is a built-in React object that is used to contain data or information about the component.&lt;/p&gt;

&lt;p&gt;So now that we have our fetch API set up, let's store the response in our state! Check out the code below 😎&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dogData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDogData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&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;useState&lt;/span&gt;&lt;span class="p"&gt;({})&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="s2"&gt;https://dog.ceo/api/breeds/image/random&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;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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="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="nx"&gt;setDogData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;pre&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;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dogData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;null&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;pre&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool! So now it looks like we're making a fetch to the API, storing it as our new State and then re-rendering the component with its new state ♽! &lt;/p&gt;

&lt;p&gt;But there's an issue…check out this statement from the React.js docs. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ooy4QsH8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrhmow1d60jk8oog12r5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ooy4QsH8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrhmow1d60jk8oog12r5.png" alt="Image 1" width="880" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can get us into trouble right now. What's &lt;em&gt;actually&lt;/em&gt; happening in our code now is that our component is making a fetch from the API, setting the state of the component, and then renders an updated component with its new state. Once that updated component is rendered, it runs the same fetch API request again, updates the state renders an updated component that makes a new fetch again, so we're stuck in an infinite loop, check out the GIF below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/OjIiUuNbI9mr4HNvua/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/OjIiUuNbI9mr4HNvua/giphy.gif" alt="Gif1" width="480" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now for the crux of this guide, the &lt;code&gt;useEffect&lt;/code&gt; hook! Here it is implemented into our code:&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="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="s2"&gt;https://dog.ceo/api/breeds/image/random&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;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&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="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="nx"&gt;setDogData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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 hook takes in 2 arguments.&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="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dependency&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first one is a callback function, in this case it's our fetch request. The second argument (conceptually) is our dependencies, here, it's just an empty array because right now we don't have any dependencies. &lt;/p&gt;

&lt;p&gt;If we omit the empty array as our second argument, our component will get stuck in its infinite loop again, so let's keep it there!&lt;/p&gt;

&lt;p&gt;With this piece of code, we can ensure the fetch request happens only once, and we're successfully able to update our state with the data retrieved from the fetch, boom!&lt;/p&gt;

&lt;p&gt;There you have it, a simple use-case for this hook, but an important one nonetheless. &lt;/p&gt;

&lt;p&gt;Should you have any questions or any additional info to share, please reach out! Anything to help me improve these guides is helpful 💪🏾&lt;/p&gt;

</description>
    </item>
    <item>
      <title>The Basics of Web Development.</title>
      <dc:creator>Barkley Santo</dc:creator>
      <pubDate>Thu, 02 Jun 2022 15:56:45 +0000</pubDate>
      <link>https://dev.to/barksanto/the-basics-of-web-development-16f5</link>
      <guid>https://dev.to/barksanto/the-basics-of-web-development-16f5</guid>
      <description>&lt;p&gt;Getting started with coding for web development can be quite a rabbit hole. Like anything, there's so much information out there, it might seem like a journey that's difficult to start. &lt;/p&gt;

&lt;p&gt;The beauty is, the information you need &lt;strong&gt;is&lt;/strong&gt; out there!&lt;/p&gt;

&lt;p&gt;Learning to code is a useful skill, and if you find that it's something you'd like to learn, stick around 🕺🏽. So, if you'll allow me, I'd like to introduce you the basics of a modern website 😉.&lt;/p&gt;

&lt;p&gt;We can break down the core components of a website into 3 languages…&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;HTML&lt;/li&gt;
&lt;li&gt;CSS&lt;/li&gt;
&lt;li&gt;JavaScript&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;HTML&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dpqtEJDO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9405a2k7mf5kwkojs5eh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dpqtEJDO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9405a2k7mf5kwkojs5eh.png" alt="HTML Logo" width="512" height="512"&gt;&lt;/a&gt;&lt;br&gt;
 HTML (HyperText Markup Language) is the most basic building block of the Web.  It defines the meaning and structure of web content (&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML"&gt;MDN&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;I'd like you to consider the HTML code of a website to be equivalent to the frame of your home 🏠. It's the 'bones' that give it structure; without it, everything would just fall apart. &lt;/p&gt;

&lt;p&gt;The pieces of that foundation and structure in HTML are &lt;em&gt;tags&lt;/em&gt;, and they each serve a purpose. In general tags help improve SEO (Search Engine Optimization); so your website works better on internet search engines, they also help with accessibility for users with disabilities or assistive technologies, each tag behaves differently when you add it to your code as well.&lt;/p&gt;

&lt;p&gt;Here's an example of what a tag looks like:&lt;br&gt;
&lt;code&gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This particular tag has an opening tag &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; and a closing tag &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt;, however not all tags have a closing tag, some are self-closing like the image tag &lt;code&gt;&amp;lt;img/&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Websites have these &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; tags that hold all the other tags that help make up the rest of it. &lt;/p&gt;

&lt;p&gt;Here are examples of a few more tags:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&amp;lt;/body&amp;gt;&lt;/span&gt; ALL content goes in here
&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&amp;lt;/h1&amp;gt;&lt;/span&gt; Top level heading
&lt;span class="nt"&gt;&amp;lt;h2&amp;gt;&amp;lt;/h2&amp;gt;&lt;/span&gt; Less important heading
&lt;span class="nt"&gt;&amp;lt;h3&amp;gt;&amp;lt;/h3&amp;gt;&lt;/span&gt; Even less important heading
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;&lt;/span&gt; Paragraph
&lt;span class="nt"&gt;&amp;lt;a&amp;gt;&amp;lt;/a&amp;gt;&lt;/span&gt; Anchor (for links)
&lt;span class="nt"&gt;&amp;lt;button&amp;gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;img/&amp;gt;&lt;/span&gt; Images
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what our website would look like if we employed a few of those tags. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--So44vhMC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51tvobm3nr9fiw2a8jsw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--So44vhMC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51tvobm3nr9fiw2a8jsw.png" alt="Code example 1 " width="880" height="440"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each tag serves its purpose and behaves a bit differently on the page.  You can see our &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tags are a smaller than any of the heading tags (h1, h2, h3). Whereas, the &lt;code&gt;h1&lt;/code&gt; heading tag appears the largest on the page. The &lt;code&gt;h1&lt;/code&gt; is macho, it's just the largest heading on the page. Then, by order of importance, we can use the other heading tags.&lt;/p&gt;

&lt;p&gt;But the website looks pretty… plain 💩. Nowadays, not many businesses or people want a website that looks like it was made in 1982, so we spice it up a bit with some styling. That's where CSS comes into play. &lt;/p&gt;

&lt;h2&gt;CSS&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PnJuyJR3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzacazr8w0nitikdaelf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PnJuyJR3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zzacazr8w0nitikdaelf.png" alt="CSS logo" width="512" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's get fancy queens and kings 👑. CSS (Cascading Stylesheets), is the code that defines the &lt;strong&gt;appearance and presentation&lt;/strong&gt; of the different parts of your website. &lt;/p&gt;

&lt;p&gt;Remember that house we mentioned earlier on? Well, if HTML is the frame of your home, then you can consider CSS as the paint on the walls, the shape of the railings, the size of your garage. CSS dictates properties that modify the &lt;strong&gt;appearance&lt;/strong&gt; of what we're seeing. It can't add a second garage for you, but it can definitely change the color of your lawn. &lt;/p&gt;

&lt;p&gt;With CSS, we dictate a style rule in 3 parts. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Selector&lt;/li&gt;
&lt;li&gt;Property&lt;/li&gt;
&lt;li&gt;Value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example, if in our home we had a door and wanted to paint it green, we can say, “I want to change my door color to green.”, this reads very much like a CSS rule. &lt;/p&gt;

&lt;p&gt;Translated to code speak: “I want to change my h1 color to green.”&lt;/p&gt;

&lt;p&gt;We just changed a single word from the original sentence, and here's what it looks like in code now! Take a look at how we can change the &lt;code&gt;&amp;lt;h1&amp;gt;&lt;/code&gt; color below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--c4moUias--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97gpyztlj09djuj1z05z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--c4moUias--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97gpyztlj09djuj1z05z.png" alt="Code 2" width="880" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can also change other properties such as font-size, font type, background colors, and much more! Check out how we changed the size and background color of the tags below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--uefEfrEG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3mzh93txyo3lmkvp3le.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--uefEfrEG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3mzh93txyo3lmkvp3le.png" alt="Code 3" width="880" height="567"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is nice, of course, but there's something missing…&lt;/p&gt;

&lt;p&gt;When you visit a site or an app, you usually interact with it in some way. Things like filling out a form or survey, clicking a button, moving things around on the page that all happens with JavaScript!&lt;/p&gt;

&lt;h2&gt;JavaScript&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XCGh_Bnq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkdh73xzo4s93130s2nh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XCGh_Bnq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkdh73xzo4s93130s2nh.png" alt="JavaScript Logo" width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not to be confused with Java. Java is to JavaScript as pen is to pentagon. No relation at all, simply a mistaken case of identity.  &lt;/p&gt;

&lt;p&gt;Do you remember that HTML and CSS are the structure and appearance of our home 🏠? Well, JavaScript is what you can interact with. Things like turning on the faucet, adjusting the air conditioning, plugging something into a power outlet etc., it's what allows you to interact with things in the home! &lt;/p&gt;

&lt;h3&gt;Strictly defined:&lt;/h3&gt;

&lt;p&gt;“JavaScript is a scripting or programming language that allows you to implement complex features on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved.” - (&lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/What_is_JavaScript"&gt;MDN&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;With JavaScript (JS) we allow interactivity with our website or app. Let's see it in practice first, and then I'll explain how the code works:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--97-1nWtL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvkldi8wq65zkj3lbjzn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--97-1nWtL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvkldi8wq65zkj3lbjzn.png" alt="Code" width="521" height="504"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll see on line 25 I added a button element to the HTML code of the page with the  element. Then on line 35 I start my JavaScript code. &lt;/p&gt;

&lt;p&gt;First on line 35 we find the button tag on the page with document.querySelector() and store it in a variable (you can think of it as a storage box) called button, it's pretty much saying “Hey JS, go to the HTML page and find the button tag, &lt;br&gt;
 and then store it in this box, please.”.&lt;/p&gt;

&lt;p&gt;Then on like 37 we add an event listener which says “When the button is clicked, do whatever comes next.”. In our case, what comes next is a function named &lt;strong&gt;sayHello()&lt;/strong&gt;. Which sends an alert on our page. Here it is in action:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/MjVXMdDZuBDehzsLa3/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/MjVXMdDZuBDehzsLa3/giphy.gif" alt="GIF of code" width="480" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There you have it! We've only had a taste of what these three languages are capable of, but I hope it helped you understand a bit more about how modern websites work. With HTML/CSS and JS, you can build almost anything your heart desires! &lt;/p&gt;

&lt;p&gt;Programming for web development can be a fun hobby and rewarding career path if you decide it's right for you. If you're looking to get started, feel free to ask me a question to help you get started! &lt;/p&gt;

&lt;p&gt;Puppy of the week:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4bidC4xa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1583337130417-3346a1be7dee%3Fixlib%3Drb-1.2.1%26ixid%3DMnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8%26auto%3Dformat%26fit%3Dcrop%26w%3D1064%26q%3D80" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4bidC4xa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1583337130417-3346a1be7dee%3Fixlib%3Drb-1.2.1%26ixid%3DMnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8%26auto%3Dformat%26fit%3Dcrop%26w%3D1064%26q%3D80" alt="Puppy" width="880" height="1100"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>css</category>
      <category>html</category>
    </item>
    <item>
      <title>Managing Form state with React useState hook 🪝.</title>
      <dc:creator>Barkley Santo</dc:creator>
      <pubDate>Thu, 26 May 2022 15:43:18 +0000</pubDate>
      <link>https://dev.to/barksanto/managing-form-state-with-react-usestate-hook--54a6</link>
      <guid>https://dev.to/barksanto/managing-form-state-with-react-usestate-hook--54a6</guid>
      <description>&lt;p&gt;Est. reading time: 8 mins&lt;/p&gt;




&lt;p&gt;If you're starting to work with React, you'll come to learn about &lt;a href="https://reactjs.org/docs/state-and-lifecycle.html" rel="noopener noreferrer"&gt;State&lt;/a&gt;. If you're not quite familiar with state yet, we can summarize state as being a variable that stores data/information within your React component, this data can be updated/changed as users interact with your app.&lt;/p&gt;

&lt;p&gt;To help us manage state in our component, React gives us the &lt;a href="https://reactjs.org/docs/hooks-state.html" rel="noopener noreferrer"&gt;State Hook&lt;/a&gt;, which we can use to store and set that data.&lt;/p&gt;

&lt;p&gt;We'll be working on the code for this simple form with two inputs, first and last name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fysv6euu0akbs11nleokq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fysv6euu0akbs11nleokq.png" alt="code 1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's check out an example to try and get caught up to speed. Take a quick look at this code here, and I'll try to explain below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//React Code&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Form&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFirstName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLastName&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleFirstNameChange&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="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setFirstName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleLastNameChange&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="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setLastName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
                &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;First Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleFirstNameChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;
                &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;placeholder&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Last Name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleLastNameChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At the top of the React code you'll see we use the useState() hook two times. &lt;/p&gt;

&lt;p&gt;Once to change the state of the First Name input and another for the Last Name. We are storing our state (data) for the First Name in a variable called firstName, and then we use the setFirstName function to update what's stored in it. &lt;/p&gt;

&lt;p&gt;When we initialize state for both firstName and lastName variables, you'll see we're initializing state to equal an empty string value with &lt;strong&gt;useState("")&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Just below that, you'll see two other functions, handleFirstNameChange and handleLastNameChange. Each &lt;code&gt;&amp;lt;input/&amp;gt;&lt;/code&gt; element below that, has an &lt;strong&gt;onChange&lt;/strong&gt; property which listens for a change in that particular element and it executes the function it's holding as its value. &lt;/p&gt;

&lt;p&gt;In short, the function that will run when there are any changes to our first name input, is handleFirstNameChange. The Last Name input has its own handleLastNameChange.&lt;/p&gt;

&lt;p&gt;This code works really well 😊! When the First Name input is updated, its respective function will execute; the neat thing is that when the onChange is triggered, it automagically passes an &lt;strong&gt;event&lt;/strong&gt; object to our handler function. If you take a look at each change handler, you see that they accept an event parameter. &lt;/p&gt;

&lt;p&gt;We can break down that event to see the changes, by looking at &lt;code&gt;event.target.value&lt;/code&gt; (line 8 where we log the updated value) here's what it looks like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/DSvqKnwnBK8B54vPDL/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/DSvqKnwnBK8B54vPDL/giphy.gif" alt="code gif1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Though it works, this solution is not scalable. Imagine you have a form with 10+ inputs. It's not very practical to write a handleChange function for each and every input on the page. &lt;/p&gt;

&lt;p&gt;In programming, we try to keep our code as DRY 🌵 (Don't Repeat Yourself) as possible. So, let's fix this to ensure we aren't duplicating code we've already written. &lt;/p&gt;

&lt;p&gt;Check out the refactored code below!&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Form&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setFormData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;firstName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;lastName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;

    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleChange&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="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setFormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&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="p"&gt;{&lt;/span&gt;
                &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&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;form&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
                &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
                &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"First Name"&lt;/span&gt;
                &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
                &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"firstName"&lt;/span&gt;
            &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
                &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt;
                &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Last Name"&lt;/span&gt;
                &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
                &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"lastName"&lt;/span&gt;
            &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;form&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's make note of a few major differences. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;There is only one handleChange function now. We got rid of handleLastnameChange and handleFirstNameChange. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We added a name property to our inputs. One input has a &lt;strong&gt;name&lt;/strong&gt; value of firstName and the other has lastName. Keep this in mind for the cherry on top 🍒!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;3.) Our initial state at the top is named differently, as well as the state change function. (They're renamed so we know they're more 'general'. The names have nothing to do with their behavior) &lt;/p&gt;

&lt;p&gt;4.) Our handleChange function looks a bit different (see below).&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="nf"&gt;handleChange&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="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setFormData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&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="p"&gt;{&lt;/span&gt;
                &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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 way we can handle changes for both inputs with a single function. We call setFormData (our newly named state function). The useState hook gives us access to previous state which we can use to update the current state with the new one! &lt;/p&gt;

&lt;p&gt;If we console.log() prevState now (blue arrow), you'll see it logs the most recent state value, in this case, it's the initialized state that we saved in formData (green). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frexdumzlci1zj1a5821p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frexdumzlci1zj1a5821p.png" alt="Previous State"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cool, huh?&lt;/p&gt;

&lt;p&gt;Now, this last part is the cherry on top 🍒. The handleChange function is going to return an updated state (an object type). &lt;/p&gt;

&lt;p&gt;Here its return statement:&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="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&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="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're using the spread operator (it's the 3 dots), to make a copy of our prevState object and then after that comma, we are updating the [event.target.&lt;strong&gt;name&lt;/strong&gt;]. Remember that name property we added to our inputs? This is telling the handleChange to return the previous state BUT, to update this particular name property to be equal to the value of the target (input element) that received the event. &lt;/p&gt;

&lt;p&gt;So  it's saying; if the input with name="firstName" is targeted, let's take the previous state of this component, and update that key (name) with the new value. &lt;/p&gt;

&lt;p&gt;And there you have it! Not so short, but certainly a powerful tool that React gives us to keep our code clean and more maintainable. &lt;/p&gt;

&lt;p&gt;If you have any other feedback, please feel free to share! I'm always happy to learn more about how to improve my work 🤘🏾.&lt;/p&gt;

&lt;p&gt;Here's a puppy&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9k6rzfh95u6o3bdj7qps.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9k6rzfh95u6o3bdj7qps.png" alt="Puppy"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Callback functions?</title>
      <dc:creator>Barkley Santo</dc:creator>
      <pubDate>Fri, 20 May 2022 17:28:31 +0000</pubDate>
      <link>https://dev.to/barksanto/javascript-callback-functions-3kdc</link>
      <guid>https://dev.to/barksanto/javascript-callback-functions-3kdc</guid>
      <description>&lt;p&gt;Est. reading time: 5 mins&lt;/p&gt;




&lt;p&gt;I've worked with JavaScript (JS) callbacks for quite some time now without &lt;em&gt;really&lt;/em&gt; understanding how, or why they work the way that they do. So I'm here to hopefully help you learn a bit more about them!&lt;/p&gt;

&lt;p&gt;For those who are unfamiliar with what a JS callback is, it's defined as a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action 😱. &lt;/p&gt;

&lt;p&gt;JavaScript is a single-threaded programming language. Meaning it's synchronous by default, and processes one operation at a time. This can vary depending on how you're using it and on which environment it (JS) is being used in, client 💻 vs server side 🗄. &lt;/p&gt;

&lt;p&gt;Let's take a look at our first example of some browser code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fruitOne(){
    console.log('Apples are the best! 🍏')
}
function fruitTwo(){
    console.log('Bananas are the best! 🍌')
}
function fruitThree(){
    console.log('Cherries are the best! 🍒')
}

fruitOne()
fruitTwo()
fruitThree()

// RESUlT
Apples are the best!
Bananas are the best!
Cherries are the best!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Works a bit how we'd expect, right? The console logs each result in the order that their functions were called. &lt;/p&gt;

&lt;p&gt;But now what do we think might happen if we use the Web API Method, &lt;strong&gt;setTimeout()&lt;/strong&gt; inside one of the functions here in our second example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fruitOne(){
    console.log('Apples are the best! 🍏')
}
function fruitTwo(){
    setTimeout(() =&amp;gt; console.log('Bananas are the best! 🍌'), 3000)
}
function fruitThree(){
    console.log('Cherries are the best! 🍒')
}

fruitOne()
fruitTwo()
fruitThree()

// RESULT
Apples are the best!
Cherries are the best!
Bananas are the best!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can see the functions are being called in the same order as before, but see them return in a different order! Now, why is that 🤔? &lt;/p&gt;

&lt;p&gt;Well, JS is a non-blocking programming language, meaning that it won't wait for previous code to finish executing before moving on to the next piece of code 🤯. &lt;/p&gt;

&lt;p&gt;So in our most recent example, fruitOne() gets called, and returns immediately. Then we get to fruitTwo() which contains the setTimeout() web api method. &lt;/p&gt;

&lt;p&gt;The setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires, in this case it will &lt;strong&gt;execute&lt;/strong&gt; after 3 seconds. &lt;/p&gt;

&lt;p&gt;The fruitThree() method &lt;strong&gt;does not wait&lt;/strong&gt; for fruitTwo() to finish executing before it gets called, it want's to run asap! &lt;/p&gt;

&lt;p&gt;Now we're seeing some asynchronous behavior! Functions returning in a different order than which they were called. &lt;/p&gt;

&lt;p&gt;Let's look at a third example now!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function fruitOne(){
    console.log('Apples are the best! 🍏')
}
function fruitTwo(myCallback){
    setTimeout(() =&amp;gt; {
        console.log('Bananas are the best! 🍌')
        callback()
    }, 3000)
}
function fruitThree(){
    console.log('Cherries are the best! 🍒')
}

fruitOne()
fruitTwo(fruitThree)


// RESULT
Apples are the best!
Bananas are the best!
Cherries are the best!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An important distinction for our third example above is that we're no longer invoking the functions one after another. We're passing the &lt;strong&gt;fruitThree&lt;/strong&gt; function as an argument to fruitTwo().&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;👻 Sneaky Note: Notice that when we pass fruitThree into fruitTwo as an argument, we omit the "()" we normally use for functions. The () is a signal to invoke the function, and we don't want to do that just yet! We're just passing a reference to the fruitThree function with no intention telling that code to execute yet. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So inside fruitTwo we have access to the myCallback parameter. The way this example works, is that our fruitOne function runs first, and then we call the fruitTwo function. Our third function waits for the second to finish before it gets the go-ahead!&lt;/p&gt;

&lt;p&gt;Inside fruitTwo() there is a setTimeout that executes 3 seconds after it's invoked. Once it's called it logs the 'banana' statement, and then &lt;em&gt;finally&lt;/em&gt; it will run the callback function we named myCallback, which is really the fruitThree function in disguise 🥸. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Notice this time we &lt;strong&gt;do use&lt;/strong&gt; the () when invoking the myCallback function param our fruitTwo function has access to. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And that's a little fun with callbacks! Callbacks are great for small tasks like this, but can quickly get out of hand with multiple nested callbacks, there's a name for this, Callback Hell 👹! There are other solutions to this though! &lt;/p&gt;

&lt;p&gt;Things like Async/Await and Promises are great tools to help manage situations like these, they're outside the scope of this post, but we can get to them one day 😉&lt;/p&gt;

&lt;p&gt;If anybody has any tips or wants to add something I missed, please let me know!&lt;/p&gt;

&lt;p&gt;Much love, till next time!💙&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--p40vgX4N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1583511655826-05700d52f4d9%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fm%3Djpg%26ixlib%3Drb-1.2.1%26q%3D80%26raw_url%3Dtrue%26ixid%3DMnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8%26auto%3Dformat%26fit%3Dcrop%26w%3D988" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--p40vgX4N--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://images.unsplash.com/photo-1583511655826-05700d52f4d9%3Fcrop%3Dentropy%26cs%3Dtinysrgb%26fm%3Djpg%26ixlib%3Drb-1.2.1%26q%3D80%26raw_url%3Dtrue%26ixid%3DMnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8%26auto%3Dformat%26fit%3Dcrop%26w%3D988" alt="puppy for you :)" width="880" height="1317"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Happy Friday!&lt;/em&gt;&lt;/p&gt;

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