<?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: Alex Merecka</title>
    <description>The latest articles on DEV Community by Alex Merecka (@merecka).</description>
    <link>https://dev.to/merecka</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%2F998236%2F046a8705-b167-4f21-b5a9-6d6143ee13b6.png</url>
      <title>DEV Community: Alex Merecka</title>
      <link>https://dev.to/merecka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/merecka"/>
    <language>en</language>
    <item>
      <title>var, let, and const</title>
      <dc:creator>Alex Merecka</dc:creator>
      <pubDate>Fri, 06 Jan 2023 09:07:35 +0000</pubDate>
      <link>https://dev.to/merecka/var-let-and-const-4e9d</link>
      <guid>https://dev.to/merecka/var-let-and-const-4e9d</guid>
      <description>&lt;p&gt;&lt;strong&gt;var:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Prior to 2015, the only way to declare a variable in JavaScript was by using the &lt;em&gt;var&lt;/em&gt; keyword.  One would declare a variable such as:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var someNumber = 1;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;var&lt;/em&gt; way of declaring variables sufficed for all use cases but it proved to be a bit confusing at times.&lt;/p&gt;

&lt;p&gt;For instance, the &lt;em&gt;var&lt;/em&gt; keyword allowed you to declare a variable with the same name, multiple times like:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var num;&lt;br&gt;
num = 8;&lt;br&gt;
num = 10;&lt;br&gt;
console.log(num); // output : 10&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;This behavior led to unintended bugs and lots of unnecessary bug hunting.&lt;/p&gt;

&lt;p&gt;Secondly, &lt;em&gt;var&lt;/em&gt; is not blocked scoped.  What this means is that the same variable name can be unintentionally (or intentionally) used inside and outside a code block.  For instance:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var x = 5;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if (x === 5) {&lt;br&gt;
  var x = 7;&lt;br&gt;
  console.log(x); // expected output : 7&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(x); //expected output : 7&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Notice here that JavaScript allowed us to create a two variables with the same identifier &lt;em&gt;x&lt;/em&gt; but in actuality two separate variables with different values were not created, it simply reassigned the variable pointer to the new value since &lt;em&gt;var&lt;/em&gt; does not acknowledge block scoping.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let &amp;amp; const:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So now that we see the pitfalls of using &lt;em&gt;var&lt;/em&gt; we can now appreciate the introduction of the &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; variable identifiers introduced in JSES6 (ECMAScript 2015).  These new keywords addressed the pitfalls associated with &lt;em&gt;var&lt;/em&gt; and provide developers with more flexibility in terms of scoping and value assignment.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Scoping:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One of the biggest differences between &lt;code&gt;let / const&lt;/code&gt; and &lt;code&gt;var&lt;/code&gt; is scoping.  Variables declared with &lt;em&gt;let&lt;/em&gt; &amp;amp; &lt;em&gt;const&lt;/em&gt; are block scoped.  This means the following would occur:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;var a = 20;&lt;br&gt;
 console.log(a); // Expected output: 20&lt;br&gt;
{&lt;br&gt;
  let exe = 30;&lt;br&gt;
  const num = 10;&lt;br&gt;
  var a = 40;&lt;br&gt;
  console.log(exe); // Expected output: 30&lt;br&gt;
  console.log(num); // Expected output: 10&lt;br&gt;
}&lt;br&gt;
console.log(a) // Expected output: 40&lt;br&gt;
console.log(exe); // Uncaught ReferenceError: exe is not defined&lt;br&gt;
console.log(num); // Uncaught ReferenceError: num is not defined&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;As you can see here, variables declared by &lt;em&gt;var&lt;/em&gt; can be accessed (and re-declared) inside code blocks.  However, variables declared with &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; are scoped to the code block in which they were defined and cannot be accessed outside of that block.  Therefore variable scoping is one of the most noticeable changes with the introduction of &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;const:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The second biggest difference in the new variables declared in ES6 is with how &lt;em&gt;const&lt;/em&gt; handles value assignment.  A variable declared by &lt;em&gt;const&lt;/em&gt; MUST be initially declared and cannot be redeclared.  &lt;/p&gt;

&lt;p&gt;This means that the following would occur:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const num = 3;&lt;br&gt;
num = 5; // Uncaught TypeError: Assignment to constant variable.&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Besides this, variables declared with the &lt;em&gt;const&lt;/em&gt; keyword behave the same as &lt;em&gt;let&lt;/em&gt; variables in that they are both block (and function scoped).  &lt;/p&gt;

&lt;p&gt;These are the main differences introduced by the &lt;em&gt;let&lt;/em&gt; and &lt;em&gt;const&lt;/em&gt; variable declaration keywords introduced in JSES6 (ECMAScript 2015).  And now with additional options you can choose your variable declarations with more strategy and thought as to their intended functionality.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>variables</category>
      <category>scope</category>
      <category>jses6</category>
    </item>
    <item>
      <title>useContext() usage in React</title>
      <dc:creator>Alex Merecka</dc:creator>
      <pubDate>Sat, 31 Dec 2022 07:46:57 +0000</pubDate>
      <link>https://dev.to/merecka/usecontext-usage-in-react-1h2</link>
      <guid>https://dev.to/merecka/usecontext-usage-in-react-1h2</guid>
      <description>&lt;p&gt;During the development phase of my React project for my Flatiron Phase 2 project, I came across the need to use a very helpful React hook called &lt;code&gt;useContext()&lt;/code&gt;.  I found this hook very useful because I needed to share the same data across multiple components in my application and without it I would have been forced to use repeated props in all of the components where I needed to use the information.&lt;/p&gt;

&lt;p&gt;To start, the definition of &lt;code&gt;useContext()&lt;/code&gt; from the React Docs is the following:  "useContext is a React Hook that lets you read and subscribe to context from your component."  The key takeaway from this is that we need to create context FIRST in order to subscribe to it.  And in order to create context we have to use the &lt;code&gt;createContext()&lt;/code&gt; hook.  &lt;/p&gt;

&lt;p&gt;The best way to learn how to work with &lt;code&gt;useContext()&lt;/code&gt; is by going through an example.  So let's take a look at a use case of how it is implemented and for that I will show an example of how I implemented it in my React project.&lt;/p&gt;

&lt;p&gt;First, the issue at hand was that I had implemented a User login feature to the application and I needed a way to keep track of which User is currently logged in.  And this information was needed to be accessible across multiple components in the application, which is essentially what Context is used for.&lt;/p&gt;

&lt;p&gt;So in order to implement the logged in User, I created a UserContext as seen below.  As you can see, I've created a context called &lt;em&gt;UserContext&lt;/em&gt; and within that context I have a state variable called &lt;em&gt;loggedInUser&lt;/em&gt; and a setter function called &lt;em&gt;setLoggedInUser&lt;/em&gt; which are used to set &amp;amp; store the User that is logged in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BmoZWijV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nibsk5ha3m95ek6wif83.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BmoZWijV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nibsk5ha3m95ek6wif83.jpg" alt="Image description" width="851" height="437"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now that we have the Context created, we can utilize it in our React components wherever we need to access this information.  For instance, there is a component named &lt;em&gt;Login&lt;/em&gt; which is being used to handle the login functionality that includes setting the logged in User.  Since we have the UserContext created which contains the &lt;code&gt;setLoggedInUser()&lt;/code&gt; function, we can utilize it in the Login component as follows:&lt;/p&gt;

&lt;p&gt;Here we use object destructuring to access the setLoggedInUser function inside Context into our component.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qGde0d99--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2puhi90kcdh3xiaq3u9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qGde0d99--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o2puhi90kcdh3xiaq3u9.jpg" alt="Image description" width="703" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And with that we can now set the logged in User based on the login information entered into the user login form:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m1GEsrl3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5qg9r93bbrwixeaetno.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m1GEsrl3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5qg9r93bbrwixeaetno.jpg" alt="Image description" width="597" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Another use case for this functionality is in the Host component.  One of the features of the Host screen is to allow the logged in User to modify the start and end times that is associated with each individual user. &lt;/p&gt;

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

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

&lt;p&gt;By accessing the loggedInUser via the UserContext, we can modify the Start and End times associated with that user as seen below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XnbQOt0L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1q7rrz1zvwjw02gpw9y.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XnbQOt0L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i1q7rrz1zvwjw02gpw9y.jpg" alt="Image description" width="856" height="937"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So now that we understand what useContext() is used for and have seen a couple of examples of real-world use cases of it you may be wondering:  "What is the advantage of using it?  Can't we just accomplish the same thing with props?"  And the answer to that question is yes we can but it is not nearly as efficient and leads to a lot of repeated code.  &lt;/p&gt;

&lt;p&gt;To see an example of this let's look at a real example.  In the App class of the same project, there is a property called &lt;em&gt;isLoggedIn&lt;/em&gt; which stores a boolean value representing if there is a User logged in or not.  And similar to the loggedInUser data, the isLoggedIn information is needed in multiple components across the application.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mN1-dH-H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/boc1q54sxz1vo48inr3h.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mN1-dH-H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/boc1q54sxz1vo48inr3h.jpg" alt="Image description" width="657" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice here we are using the familiar &lt;code&gt;useState()&lt;/code&gt; hook to create a variable that will hold this information.  Then to pass this data to other components, we have to pass it as props as seen below:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LVJVUAbY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unk1em29100so0vmwr0g.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LVJVUAbY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/unk1em29100so0vmwr0g.jpg" alt="Image description" width="701" height="718"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice that here we are passing the &lt;em&gt;isLoggedIn&lt;/em&gt; property to three different components via props.  And while this may not save much code as we would need to subscribe to Context in these three components if we were using the &lt;code&gt;useContext()&lt;/code&gt; hook instead, the real advantage would come if we needed to use the &lt;em&gt;isLoggedIn&lt;/em&gt; property in nested components within these three components.  In that case, we would need to pass it via props to each of the three parent components and then pass it as props to any children components.  This is referred to as &lt;em&gt;prop drilling&lt;/em&gt; and this is where &lt;code&gt;useContext()&lt;/code&gt; can offer a real advantage to reducing repititous code.&lt;/p&gt;

&lt;p&gt;As you've seen, &lt;code&gt;useContext()&lt;/code&gt; can be a very useful to pass data between components especially in cases where there are many nested layers of components.  With that said, it is considered best practice to utilize &lt;code&gt;useContext()&lt;/code&gt; sparingly throughout the application and save it for cases where it offers a significant reduction in code repetition.  &lt;/p&gt;

</description>
      <category>react</category>
      <category>usecontext</category>
      <category>component</category>
      <category>hooks</category>
    </item>
  </channel>
</rss>
