<?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: Shujaat Azim</title>
    <description>The latest articles on DEV Community by Shujaat Azim (@shujaatazim).</description>
    <link>https://dev.to/shujaatazim</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%2F496950%2F9f2d8642-8985-42b8-bff2-7f3a75ead58f.png</url>
      <title>DEV Community: Shujaat Azim</title>
      <link>https://dev.to/shujaatazim</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shujaatazim"/>
    <language>en</language>
    <item>
      <title>Feeling Bloaty? File Cleanup After Create-React-App</title>
      <dc:creator>Shujaat Azim</dc:creator>
      <pubDate>Sun, 29 May 2022 05:51:00 +0000</pubDate>
      <link>https://dev.to/shujaatazim/feeling-bloaty-file-cleanup-after-create-react-app-23dm</link>
      <guid>https://dev.to/shujaatazim/feeling-bloaty-file-cleanup-after-create-react-app-23dm</guid>
      <description>&lt;p&gt;The create-react-app command is a great way to start a new React project; everything just works out of the box like magic. In fact, I don't think I've ever &lt;em&gt;not&lt;/em&gt; used it to start any of my React projects. However if you're anything like me, the number of never-touched files generated once CRA is done working its magic - index.css, robots.txt, manifest.json, and others - is bothersome. &lt;/p&gt;

&lt;p&gt;When starting out it in your coding journey it can be difficult to discern which files are actually &lt;em&gt;needed&lt;/em&gt; since the inner workings of CRA are not (yet) obvious to you. It was one of the first issues I grappled with when I was new, and one for which I still don't easily find answers. So here I am.&lt;/p&gt;

&lt;p&gt;Here's what my sample project's file structure looks like after running create-react-app and opening it in VS Code (I'm assuming you already know how to do this):&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dcbhbZeF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ye8j4pr4bpctrurq3e0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dcbhbZeF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ye8j4pr4bpctrurq3e0.png" alt="file structure image" width="446" height="1158"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;First, let's outright delete the following files in their entirety:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;favicon.ico (this is the icon for the browser tab)&lt;/li&gt;
&lt;li&gt;logo192.png (react's default logo)&lt;/li&gt;
&lt;li&gt;logo512.png (react's default logo)&lt;/li&gt;
&lt;li&gt;manifest.json (describes the app)&lt;/li&gt;
&lt;li&gt;robots.txt (for search engine crawlers)&lt;/li&gt;
&lt;li&gt;App.test.js (boilerplate test file)&lt;/li&gt;
&lt;li&gt;index.css (boilerplate stylesheet)&lt;/li&gt;
&lt;li&gt;logo.svg (another default logo)&lt;/li&gt;
&lt;li&gt;reportWebVitals.js (for measuring user experience metrics)&lt;/li&gt;
&lt;li&gt;setupTests.js (imports Jest-DOM testing library)&lt;/li&gt;
&lt;li&gt;yarn.lock (if you're using NPM, which I do)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's 11 files deleted right off the bat. All of these files can be re-added later, if needed, rather than cluttering your view for much of your app's development.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Oos20BzI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipnzbued518gv2whiegi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Oos20BzI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ipnzbued518gv2whiegi.png" alt="cleaned up file structure" width="328" height="582"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's move on to some cleanup within the remaining files. Let's start with index.html, which is the root of your project:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KjFptgJa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik203urj7vx1a1lv137e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KjFptgJa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ik203urj7vx1a1lv137e.png" alt="default index.html file" width="800" height="740"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's a lot of lines of code, and much of it isn't needed. Go ahead and delete lines 5, 12, 17, 30. Then delete all the comments, and consolidate lines 8-10 into one line. You should be left with something like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tSsM5E4i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbwo5cfq6e34heq0a0y5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tSsM5E4i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbwo5cfq6e34heq0a0y5.png" alt="cleaned up index.html file" width="800" height="293"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Navigate into App.css next. This file isn't needed until you actually start styling, so you can remove this file in its entirety and re-add it later if you prefer. But if you want to keep it, go ahead and delete everything within it.&lt;/p&gt;

&lt;p&gt;Now it's time for index.js, which should look like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dI41V-aK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6u8oj9cgzcfh3ppysy85.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dI41V-aK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6u8oj9cgzcfh3ppysy85.png" alt="default index.js file" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go ahead and delete lines 3, 5, and 14-17. You should be left with a barebones file looking like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cK1db2lN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65thair7gjlqorn1cwzn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cK1db2lN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/65thair7gjlqorn1cwzn.png" alt="cleaned up index.js file" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Finally, let's check out App.js. I prefer the relatively newer arrow functions for my components, so let's convert it. Then delete lines 1, 2, everything within the div, and the classname for the div. Finally, even though we no longer &lt;em&gt;need&lt;/em&gt; to import React for simple components, you will need to do so if you plan on using Hooks or other features React provides. Add the import at the top. Your file should look like this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--84DPAVxy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjpb82vjs4dlgyx39bxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--84DPAVxy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjpb82vjs4dlgyx39bxy.png" alt="cleaned up App.js file" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it! You've removed much of the unused boilerplate clutter, and are ready to start your project with a blank(er) slate. Clutter can be distracting, so I hope you've found this guide useful. &lt;/p&gt;

&lt;p&gt;"The real problem is that we have far more than we need or want.” -Marie Kondō&lt;/p&gt;

&lt;p&gt;:)&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>RegEx - A Teeny, Tiny Taster</title>
      <dc:creator>Shujaat Azim</dc:creator>
      <pubDate>Mon, 19 Apr 2021 21:22:22 +0000</pubDate>
      <link>https://dev.to/shujaatazim/regex-a-teeny-tiny-taster-59go</link>
      <guid>https://dev.to/shujaatazim/regex-a-teeny-tiny-taster-59go</guid>
      <description>&lt;p&gt;Since beginning my coding journey, few topics have confused me the way RegEx has. I saw them as little more than gibberish, random symbols in between slashes that meant little to nothing. Thankfully, I wasn’t alone in my bewilderment, and I was able to eventually learn how to make them bend to my will (...kinda)!&lt;/p&gt;

&lt;p&gt;First off, let me just clarify that RegEx is pronounced "reg-ex" and not “ree-jex” as some trolls have tried to perpetuate. It stands for “&lt;strong&gt;Reg&lt;/strong&gt;ular &lt;strong&gt;Ex&lt;/strong&gt;pressions,” with “regular” referring to their origins in mathematically regular languages. This basis is what allows RegExs to be used across programming languages (“language-agnostic”), lending to their usefulness. It also means that they are almost “purely” logical. However, as mere mortal humans, we are not logical beings; therefore, RegEx’s tend to be exceedingly confusing.&lt;/p&gt;

&lt;p&gt;But confusing for &lt;em&gt;what&lt;/em&gt;, exactly? Simply put, RegEx’s are used for describing patterns in strings. A pattern can be certain words, the ordering of letters, strange characters, spaces, and just about anything else you can think of that can go into a string. They allow us to specifically target certain  “points of interest” in string data. For example, how would we target the underscore characters for removal in the following JavaScript and Ruby strings?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;// javascript&lt;/code&gt; &lt;br&gt;
&lt;code&gt;let string = "Hello_my_name_is_Shujaat"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;# ruby&lt;/code&gt;&lt;br&gt;
&lt;code&gt;string = "Hello_my_name_is_Shujaat"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Well, we could use some higher-order functions to accomplish this:&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&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;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello_my_name_is_Shujaat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;splitString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&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="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;splitString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Logs ["Hello", "my", "name", "is", "Shujaat"]&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;splitString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;join&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="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;newString&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Logs "Hello my name is Shujaat"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Ruby
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello_my_name_is_Shujaat"&lt;/span&gt;
&lt;span class="n"&gt;split_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&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="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;split_string&lt;/span&gt; 

&lt;span class="c1"&gt;# Outputs [Hello, my, name, is, Shujaat]&lt;/span&gt;

&lt;span class="n"&gt;new_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;split_string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&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="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;new_string&lt;/span&gt;

&lt;span class="c1"&gt;# Outputs "Hello my name is Shujaat"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a purposefully simple example; it can all be done in fewer lines by omitting the variables and chaining the methods together. If the string was more complicated, perhaps with a bunch of different symbols, spaces, numbers, and capital letters all over the place, it would be significantly harder to make it readable to human eyes. HTML data, for instance, is just an insanely complicated string at the end of the day. But for brevity’s sake, let’s take a look at the following, slightly-more-complicated string:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;"Hello_my!name&amp;amp;is8Shujaat"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;It would take several different higher order functions (one for each of the weird characters in between the words) to remove the nonsense characters and replace them with spaces. That isn't practical, even in this one sentence string; so imagine how cumbersome it would be in an HTML doc!&lt;/p&gt;

&lt;p&gt;The solution? We can use RegEx to filter the string of all the non-letter characters and return the simple string:&lt;/p&gt;

&lt;h3&gt;
  
  
  JavaScript
&lt;/h3&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;complexString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello_my!name&amp;amp;is8Shujaat&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;0-9_!&amp;amp;&lt;/span&gt;&lt;span class="se"&gt;\s]&lt;/span&gt;&lt;span class="sr"&gt;/g&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;complexString&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&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="c1"&gt;//logs "Hello my name is Shujaat"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All I did here was create a set of conditions (called a "literal creation") in between two slashes, and added a global flag (g) at the end. The &lt;code&gt;/[0-9_!&amp;amp;\s]/g&lt;/code&gt; translates as "any integer between 0 and 9, AND any underscores, AND any exclamations, AND any ampersands, AND any whitespaces, across the WHOLE string."&lt;/p&gt;

&lt;p&gt;The .replace() method takes two arguments, the "target" and the "replacement." Without RegEx, we would have to use a separate .replace() for EACH target, which quickly bloats and obfuscates our code. However, storing all the conditions in a variable using RegEx allows us to target everything at once! The global flag outside the slashes indicates that we would like to identify the targets across the whole string and replace them all with spaces (" ") - without it, we would stop at the first match by default.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ruby
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;complex_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello_my!name&amp;amp;is8Shujaat"&lt;/span&gt;
&lt;span class="n"&gt;new_string&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;complex_string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;gsub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/[0-9_!&amp;amp;\s]/&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="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;new_string&lt;/span&gt;

&lt;span class="c1"&gt;# Outputs "Hello my name is Shujaat"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is very similar to the JavaScript solution above, but it has a couple important differences. We still need to create the set of conditions, and because RegEx is language agnostic, it's the same as the conditions we used before: &lt;code&gt;/[0-9_!&amp;amp;\s]/&lt;/code&gt; However, instead of .replace, we're using the .gsub method, which means "global substitution." Therefore, we don't need the g flag in the RegEx.&lt;/p&gt;

&lt;h3&gt;
  
  
  Phew!
&lt;/h3&gt;

&lt;p&gt;You might be wondering if you have to memorize all the ridiculous conditions, the varying syntaxes, and all the different flag names. I have good news - you don't! There are many resources available that will help you set up your RegEx options, let you input your strings, and spit out a result. Two commonly used ones are:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;JavaScript:&lt;/strong&gt; &lt;a href="https://regex101.com/"&gt;https://regex101.com/&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Ruby:&lt;/strong&gt; &lt;a href="https://rubular.com/"&gt;https://rubular.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's a chart of common RegEx options too: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7dThejLG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ioy8t7bdrfftczchi3yq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7dThejLG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ioy8t7bdrfftczchi3yq.png" alt="Common RegEx"&gt;&lt;/a&gt;Commonly used RegEx for most situations.&lt;/p&gt;

&lt;p&gt;Yes, Regular Expressions are inherently unintuitive. The mere fact that conditions are chained together &lt;em&gt;without spaces&lt;/em&gt; drove me nuts when I first learned about them. This in turn leads to programmers, especially aspiring ones, to completely ignore them as a tool. In fact, I found many “how can I do this WITHOUT RegEx” questions on StackOverflow when I was poking around. But had I taken to RegEx earlier in my learning, I'd have solved many of my early coding challenges far more easily! So definitely don't ignore them, use all the tools available!&lt;/p&gt;

&lt;p&gt;:)&lt;/p&gt;

</description>
      <category>programming</category>
      <category>javascript</category>
      <category>ruby</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Just the Tip — Building a Simple Tip Calculator in React</title>
      <dc:creator>Shujaat Azim</dc:creator>
      <pubDate>Mon, 19 Apr 2021 00:36:21 +0000</pubDate>
      <link>https://dev.to/shujaatazim/just-the-tip-building-a-simple-tip-calculator-in-react-5ei4</link>
      <guid>https://dev.to/shujaatazim/just-the-tip-building-a-simple-tip-calculator-in-react-5ei4</guid>
      <description>&lt;p&gt;My strategy for learning anything new? Repetition and practice. Studying? Flashcards, online tests, cramming. Competitive gaming? Training mode, tons of back-to-back matches. Learning Japanese for my next solo trip? Apps, podcasts, shows. All of these are forms of practice, and I applied this same, simple strategy to the development of my programming skills post-bootcamp graduation.&lt;/p&gt;

&lt;p&gt;Now that I've convinced you that practice rocks, I am going to walk you through one of my earliest practice projects that I built after completing my Flatiron bootcamp program -- a tip calculator! Yaaaaay! No? Not convincing you of my app's amazingness with my enthusiasm? Well, in fact, we don’t &lt;em&gt;need&lt;/em&gt; to come up with a super-cool, ground-breaking, worldview-shattering idea in order to practice. If you sit and wait for that "AHA!" idea, the one that spawned the likes of Google or Facebook or Instagram, you'll end up wasting time in procrastination land. I would know. I've been there. And I still visit from time to time.&lt;/p&gt;

&lt;p&gt;Instead, we can simply take an app or idea that someone else has built and try to copy it using our own skills and knowledge. This will give us a deeper understanding of our current ability and will open up multiple opportunities improvement. You've probably already seen many to-do list tutorials for every language, library, and framework. The reason there are so many is that people have figured out it's a good idea to re-build things for practice.&lt;/p&gt;

&lt;p&gt;Oh, and that whole "someone else must have already thought of this” attitude shouldn’t ever stop you. After all, should Mr. Dorsey and friends have shied away from creating Twitter just because we already had AOL Instant Messenger and Facebook statuses? I think not! Let’s get started!&lt;/p&gt;

&lt;p&gt;Here's a link to my github repo if you'd like to follow along: &lt;a href="https://github.com/ShujaatAzim/tipblog" rel="noopener noreferrer"&gt;https://github.com/ShujaatAzim/tipblog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Disclaimer: I'm assuming everyone reading this has working knowledge of React, create-react-app, and what "state" is/does. I also want to point out that I made this project when I was still a bit new, very soon after I graduated from my Flatiron Bootcamp. Therefore, I'm sticking to basic class components with state.&lt;/p&gt;

&lt;p&gt;Go into your terminal, and within whichever directory you choose, type &lt;code&gt;create-react-app [name of your app]&lt;/code&gt;. I named mine "Just the Tip" -- I know, so clever. After everything is finished setting up, open the file in whichever code editor you use. I use VS code, so while in the root of the project in my terminal I just type &lt;code&gt;code .&lt;/code&gt; to get started.&lt;/p&gt;

&lt;p&gt;Then navigate to your src → App.js file. This is where all the work is going to be done. I decided to only use this component since it’s a small app and doesn’t &lt;em&gt;need&lt;/em&gt; to pass information around via props.&lt;/p&gt;

&lt;p&gt;You will see that App.js is a functional component. We're going to change this to a class component, keeping it simple for the new-to-React folks. You should end up with something like this, after removing some of the superfluous code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdfp1qirhqqygnoetd647.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdfp1qirhqqygnoetd647.png" alt="conversion of functional to class component" width="800" height="434"&gt;&lt;/a&gt;First, small step!&lt;/p&gt;

&lt;p&gt;Now let’s take a moment to think about what would be needed for this app. In order to calculate tip, we’re going to need a few values — the total bill, the tip percentage, and the size of the party (if splitting the bill). These will allow us to get both the total tip and the tip per person numbers after a little basic math. Because we’re going to be storing and updating these values, we’re going to put them in state as empty strings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc8hni25p31lnyaccyn5e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc8hni25p31lnyaccyn5e.png" alt="pieces of state" width="800" height="542"&gt;&lt;/a&gt;State can be made like this nowadays, without a constructor.&lt;/p&gt;

&lt;p&gt;You might be wondering, “Why empty strings? Aren’t we dealing with integers?” No? You weren't? Well, bonus points if you &lt;em&gt;were&lt;/em&gt; wondering that, because it's a good question. While we could use integers, the empty strings would be a bit more flexible down the road. JavaScript is a weakly-typed language, which means it sort of just “knows” that the integer 42 is the same as the string “42” for &lt;em&gt;most&lt;/em&gt; intents and purposes. This leads a bit more flexibility while sacrificing a bit of clarity. As you'll see below, I ended up using words ("Subpar", "Excellent", etc) to define a certain level of service versus using tip percentages. This was for UX purposes, and you might not elect to do the same thing - but JavaScript offers such flexibility by being weakly-typed.&lt;/p&gt;

&lt;p&gt;Next, let’s build out some of the form so that we have a place for the users to enter some information. It’s important that we make sure that this form is "controlled," meaning that the inputs change their associated state values. We don’t yet have the functions that will do that change, yet, but we will take care of them in the next step:&lt;/p&gt;

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

&lt;p&gt;We set the values of the inputs to a piece of state, making this a controlled form. We’ve also already put in the function names that will be called when something is typed into the field (ie “on change”). They’re all named something similar, but they are &lt;em&gt;descriptive&lt;/em&gt; — anyone looking over the code instantly know which piece of the state the fields are responsible for. This is useful for many reasons, including debugging, peer review, and future refactoring.&lt;/p&gt;

&lt;p&gt;Let’s go ahead and create those functions. They’re all going to take the value of the input that they are associated with, and set that piece of state using the setState() function. Remember, do not directly manipulate state with stuff like &lt;code&gt;state.value = newValue&lt;/code&gt;! Use setState()! Additionally, they’re all going to take the event object as an argument so that we have access to &lt;code&gt;event.target.value&lt;/code&gt;, which will be whatever's typed into the input box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqr0gj8ou232nonkqnv4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqr0gj8ou232nonkqnv4w.png" alt="event handler functions" width="800" height="368"&gt;&lt;/a&gt;Not very DRY… It might be possible to combine these into one handler.&lt;/p&gt;

&lt;p&gt;You can check if the state is being updated by looking in your React Dev Tools or by using console.log() on a piece of state and watching it change in the console in real time. It’s important to note that, for now, we don’t have any validations for the input fields. This means we’re able to type whatever we want into them and the eventual submission of the form will still go through. We’re just going to assume that the users will know exactly what to put into the fields for now; the main goal here is to practice using React and getting something up and running. &lt;/p&gt;

&lt;p&gt;If you're a little more advanced in your dev know-how, you'll also notice that these functions are all pretty similar - they're not very DRY ("don't repeat yourself"). Is it possible to combine these into one handler function? Maybe! Again, I'm going for explicitness here, but I'm hoping that you'll take away these little tidbits too!&lt;/p&gt;

&lt;p&gt;Now, it’s time for some tip-generating logic. We need to create a function that will take all the state values and do some math on them to create our two desired values: a total tip value and a tip per person. Since we know a tip is the bill total multiplied by a certain decimal, we’re just going to make it (this.state.total) * (this.state.service). Then, after storing this value in the state, we’re going to simply divide it by the party size to get the tip per person value. Finally, we’re going to make sure that this function is called upon the form’s submission. Sounds simple enough:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze4kqhqhtq909ungonvt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fze4kqhqhtq909ungonvt.png" alt="tip calculation function" width="800" height="215"&gt;&lt;/a&gt;The parseFloat().toFixed(2) ensures that we round to two decimal places!&lt;/p&gt;

&lt;p&gt;We must remember that setting state is asynchronous, so putting the tip calculations directly into the setState function will correctly post the totalTip, but will come up as 0 or undefined for tipPP. This is because tipPP depends on the totalTip, and is calculated at the same time as totalTip is calculated (which is still an empty string at the time). To remedy this, we simply put the calculations into variables, which are synchronous, and set the state fields as those variables.&lt;/p&gt;

&lt;p&gt;We can now update our rendering a bit, such that the form will call genTip on submission and to display the tip values under the form:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0dbdzjypnu3uqen92j7g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0dbdzjypnu3uqen92j7g.png" alt="adding tip to display" width="800" height="360"&gt;&lt;/a&gt;Just two small changes, plus adding “required” to each field.&lt;/p&gt;

&lt;p&gt;And we’re done! A fully functional app for calculating tip. You should have something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazf759kvl78nqmd1133f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazf759kvl78nqmd1133f.png" alt="the barely-styled final result" width="800" height="174"&gt;&lt;/a&gt;Simple, elegant, useful!&lt;/p&gt;

&lt;p&gt;Pretty awesome! But why stop there? There’s lots of ways to make this simple app even better. For example, the user experience is a bit lacking — who wants to type 0.20 for 20% tip? And right now, the user can type in any string, such as words, into the fields and get $NaN as a result. Maybe we need some form validations, like &lt;code&gt;&amp;lt;input type="number" /&amp;gt;&lt;/code&gt;. Also, in order to clear the form, we need to reload the page — sounds like we need a “Clear” button. And why have the fields all in a line? It’s an eyesore! &lt;/p&gt;

&lt;p&gt;These are just a few of the tasks that can help refine some more skills using React. Perhaps you’d also want to add on a couple styling libraries? Or throw on a backend to be able to save your tips and keep track of how much a user tips generally, for research purposes? That last one might be a big one, but it illustrates that the possibilities for refactoring even this &lt;em&gt;super-simple&lt;/em&gt; app are endless. Here’s what mine looks like now:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffufbavjit2tag2dc91ck.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffufbavjit2tag2dc91ck.png" alt="one possible styling route" width="800" height="547"&gt;&lt;/a&gt;Very simple styling makes a big difference!&lt;/p&gt;

&lt;p&gt;And after submitting….&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftuqdje4sgu5rd3jxh7za.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftuqdje4sgu5rd3jxh7za.png" alt="adding sweet alert library" width="800" height="733"&gt;&lt;/a&gt;I used a package called Sweet Alert for this display.&lt;/p&gt;

&lt;p&gt;Cool, right? So your next steps could be styling, form validations, some libraries, and perhaps a backend (not necessarily in that order).&lt;/p&gt;

&lt;p&gt;I hope you've found this guide useful, whether you’re a fresh beginner or a seasoned developer in need of a quick review. It was certainly useful for me to write it up as it forced me to redo and rethink many of the steps I had taken previously, therefore cementing the knowledge even more. I will be revisiting this project and some of my other (slightly) more complicated ones in the future!&lt;/p&gt;

&lt;p&gt;:)&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
