<?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: Michael Mathias</title>
    <description>The latest articles on DEV Community by Michael Mathias (@michaelmathiaa).</description>
    <link>https://dev.to/michaelmathiaa</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%2F1036559%2F1c352fff-3844-4a15-9c4d-22a3093d912f.gif</url>
      <title>DEV Community: Michael Mathias</title>
      <link>https://dev.to/michaelmathiaa</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/michaelmathiaa"/>
    <language>en</language>
    <item>
      <title>How to use useState()</title>
      <dc:creator>Michael Mathias</dc:creator>
      <pubDate>Mon, 03 Jul 2023 04:03:33 +0000</pubDate>
      <link>https://dev.to/michaelmathiaa/how-to-use-usestate-14m6</link>
      <guid>https://dev.to/michaelmathiaa/how-to-use-usestate-14m6</guid>
      <description>&lt;p&gt;React's useState hook is an essential tool for managing state within functional components. This hook lets you add a state variable to your component to allow stateful behavior and update components in response to events or user interactions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage of useState
&lt;/h2&gt;

&lt;p&gt;To start using useState, import it from the 'react' package at the top level of your component.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { useState } from 'react';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, declare a state variable with array destructuring. The naming convention for state variables is [something, setSomething]. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const [age, setAge] = useState(24);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;useState returns an array with two items: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The current state, initially set to the initial state you provided. 24 in the example above.&lt;/li&gt;
&lt;li&gt;The set function that lets you change the current state to another value in response to a user interaction or other event.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Call the set function with some next state to change the current state. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;setAge(21);&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating State Based on Previous State
&lt;/h2&gt;

&lt;p&gt;To update state based on the previous state, pass an updater function instead of the next state. The set function only affects what useState will return starting from the next render; it does not change the state in the already running 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 handleAgeClick() {
  setAge(age + 1); // setAge(24 + 1)
  setAge(age + 1); // setAge(24 + 1)
  setAge(age + 1); // setAge(24 + 1)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each setAge(age + 1) call becomes setAge(24 + 1). Fix this by using an updater function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleAgeClick() {
  setAge(a =&amp;gt; a + 1); // setAge(24 =&amp;gt; 25)
  setAge(a =&amp;gt; a + 1); // setAge(25 =&amp;gt; 26)
  setAge(a =&amp;gt; a + 1); // setAge(26 =&amp;gt; 27)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The updater function takes the pending state and calculates the next state from it. I named the pending state argument 'a' which is the first letter of the state variable 'age'. This is a common naming convention you can follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  Avoid Recreating the Initial State
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function CustomerOrders() {
  const [orders, setOrders] = useState(createInitialOrders());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you invoke a function as your initial state, the result will only be used for the initial render but the function will be called on every render. This can be wasteful but can be solved by passing the function as an initializer function to useState instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function CustomerOrders() {
  const [orders, setOrders] = useState(createInitialOrders);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React only calls your initializer function during initialization and not on every render.&lt;/p&gt;

&lt;h2&gt;
  
  
  Objects and Arrays in State
&lt;/h2&gt;

&lt;p&gt;You should not mutate your existing objects since, in React, state is read-only.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;form.lastName = 'Smith';&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Rather than mutate your objects, replace the whole object and create a new one with the spread operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setForm({
  ...form,
  lastName: 'Smith'
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;By understanding and effectively utilizing useState, you can enhance the functionality and interactivity of your React components, making your applications more dynamic and responsive. To learn more about useState and everything React, please take a look at the &lt;a href="https://react.dev/reference/react"&gt;React reference&lt;/a&gt; pages.&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to Fix JavaScript Errors</title>
      <dc:creator>Michael Mathias</dc:creator>
      <pubDate>Mon, 15 May 2023 03:02:33 +0000</pubDate>
      <link>https://dev.to/michaelmathiaa/how-to-fix-javascript-errors-5078</link>
      <guid>https://dev.to/michaelmathiaa/how-to-fix-javascript-errors-5078</guid>
      <description>&lt;p&gt;Have you ever wondered why your code isn't working? What does that dreadful red message mean? Don't worry that message is there to help you. Let's begin with some of the common errors you will encounter or may have already seen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Error Types
&lt;/h2&gt;

&lt;p&gt;The errors you face will generally be a syntax error or a logic error.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Syntax Errors&lt;/strong&gt;&lt;br&gt;
These errors are quite easy to fix because you will receive a message stating where the error occurred and what the error is.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Es9w58M4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqrmuncgb1a1dhj4z4kt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Es9w58M4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqrmuncgb1a1dhj4z4kt.png" alt="Syntax Error" width="642" height="190"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The message starts off by providing the path to the error. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SW6vlIn1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v178u19ytqnaqd04liz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SW6vlIn1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v178u19ytqnaqd04liz7.png" alt="Path" width="504" height="52"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The path leads us to the 'index.js' file. The number '1' tells us that the error is on the first line of the 'index.js' file. Depending on which integrated development environment (IDE) you are using there may be a colon and a second number next to the first number indicating the character where the error starts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Y4olvClK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wdw852fui1uwe510w1y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Y4olvClK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wdw852fui1uwe510w1y.png" alt="Error Message" width="632" height="138"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome! Now we know for sure that we have a syntax error. Unexpected identifier? What does that mean? Recall what we are doing on this line. We want to declare a variable with 'const'. Usually the problem is a misspelled word so let's check our spelling.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pnrtJk2y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ium1pui9ilfo6by3uecj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pnrtJk2y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ium1pui9ilfo6by3uecj.png" alt="Line Containing the Error" width="726" height="46"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Looks like we misspelled 'const'. The 's' is missing! Let's fix it and see if that solves the problem.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mJwGpuFv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3m031vded3ngnggu9rrz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mJwGpuFv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3m031vded3ngnggu9rrz.png" alt="Error Free Array" width="688" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Nice now our program is working and the error message is gone! Unexpected identifier is just one of the many syntax errors. If you would like to learn about all of the different types of syntax errors including range errors and type errors, I recommend visiting mdn web docs. &lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--UHjFdr30--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/mdn-social-share.cd6c4a5a.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors" rel="noopener noreferrer" class="c-link"&gt;
          JavaScript error reference - JavaScript | MDN
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Below, you'll find a list of errors which are thrown by JavaScript. These errors can be a helpful debugging aid, but the reported problem isn't always immediately clear. The pages below will provide additional details about these errors. Each error is an object based upon the Error object, and has a name and a message.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://res.cloudinary.com/practicaldev/image/fetch/s--_bI64N_S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://developer.mozilla.org/favicon-48x48.cbbd161b.png" width="48" height="48"&gt;
        developer.mozilla.org
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Logic Errors&lt;/strong&gt;&lt;br&gt;
Now the errors that don't often involve a message. A syntax error stops the program part way through or prevents the program from running at all. Logic errors don't behave the same way. The program works but the results are not what we wanted or expected. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kpQbWg6k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zeetf3xwxf9qc7w5bkxm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kpQbWg6k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zeetf3xwxf9qc7w5bkxm.png" alt="Random Number Program" width="800" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pCc-aky0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/peb3mttny4qbi8vh5344.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pCc-aky0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/peb3mttny4qbi8vh5344.png" alt="Result" width="692" height="112"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We want to make a program that returns a random number from 1 to 50. The problem is the program always returns 1. We didn't get a error message so we know there aren't any syntax errors. Let's backtrack and look at each part of the program.&lt;/p&gt;

&lt;p&gt;Math.random() returns a random decimal number between 0 and 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--v_HIhCJg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1xjdqa9je1ikotyplrw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--v_HIhCJg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1xjdqa9je1ikotyplrw.png" alt="Math.random()" width="532" height="48"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C0ADcVHu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxt341ningfpjagyd5yw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C0ADcVHu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vxt341ningfpjagyd5yw.png" alt="Result" width="686" height="114"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Math.floor() rounds a number down.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s---kRM-9rC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t30675e519kw14m73ar.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s---kRM-9rC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1t30675e519kw14m73ar.png" alt="Math.floor()" width="734" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9a0Q2Lj4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue9do3p4fs0d4gegimg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9a0Q2Lj4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue9do3p4fs0d4gegimg0.png" alt="Result" width="692" height="114"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code will always return 0 since Math.random() returns a decimal number between 0 and 1. So let's add 1 to always have it return a value of 1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--obunrNCc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rzfccp5qt2wj6tw0zhg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--obunrNCc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8rzfccp5qt2wj6tw0zhg.png" alt="Added 1" width="800" height="44"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_FduCLNN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdilreda7v5cgzs0p0f7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_FduCLNN--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qdilreda7v5cgzs0p0f7.png" alt="Result" width="690" height="110"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we round the random number down we first need to multiply it by 50.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--grf1kOIF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57bgsiytz7b41gjtm42g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--grf1kOIF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/57bgsiytz7b41gjtm42g.png" alt="Multiply by 50" width="800" height="47"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bXU99DwO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ousy81ovyfa9510w54io.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bXU99DwO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ousy81ovyfa9510w54io.png" alt="Result" width="690" height="112"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Perfect, the program works! When you encounter a logic error you have to think about what you want your program to accomplish. If you aren't getting the expected results then dig into where you believe the error is and start dissecting your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;As a beginner you will run into many errors but don't let that discourage you. Error messages are not something to fear but rather something to be comfortable with. They are there to help you achieve the results you are looking for.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
