<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kevin O'Shaughnessy</title>
    <description>The latest articles on Forem by Kevin O'Shaughnessy (@keoshaug).</description>
    <link>https://forem.com/keoshaug</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%2F1040732%2F597c9758-28ed-4cab-b1c6-8fd851872e2d.jpg</url>
      <title>Forem: Kevin O'Shaughnessy</title>
      <link>https://forem.com/keoshaug</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/keoshaug"/>
    <language>en</language>
    <item>
      <title>React: useState and useEffect</title>
      <dc:creator>Kevin O'Shaughnessy</dc:creator>
      <pubDate>Sun, 08 Oct 2023 15:34:03 +0000</pubDate>
      <link>https://forem.com/keoshaug/react-usestate-and-useeffect-55fk</link>
      <guid>https://forem.com/keoshaug/react-usestate-and-useeffect-55fk</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;React is a way of using JavaScript in an efficient and straightforward way, separating the code into different component files. It is used to build web applications. Officially, it is defined as "A JavaScript library for building user interfaces."[1] Two of the most important basic React hooks are useState and useEffect. useState allows one to add and update a state variable.[2] useEffect allows for performing side effects and also allows one to synchronize a component with an external system.[3]&lt;/p&gt;

&lt;h2&gt;
  
  
  useState
&lt;/h2&gt;

&lt;p&gt;State in React is dynamic and can change overtime. It is effectively the default value when a component mounts. Each component maintains its own state but only sets the initial state for its children.[4] In React, there is a clear basic hierarchy of parent component(s) and children. The parents pass props - properties - to the children. Props cannot be changed by the components themselves but are passed to children by parent components.&lt;/p&gt;

&lt;p&gt;from:&lt;br&gt;
import React from 'react';&lt;/p&gt;

&lt;p&gt;to:&lt;br&gt;
import React, { useState } from 'react';&lt;/p&gt;

&lt;p&gt;In order to actually use useState, it has to be imported into the component in which it is being used, as show above. useState will be imported in parentheses (so will useEffect, as we shall see). &lt;br&gt;
The setup in the actual function will look something like this:&lt;/p&gt;

&lt;p&gt;function App() {&lt;/p&gt;

&lt;p&gt;const [quotes, setQuotes] = useState([])&lt;/p&gt;

&lt;p&gt;//rest of code&lt;br&gt;
  }&lt;/p&gt;

&lt;p&gt;The convention here is to start with the initial state, then the way to update that initial state: ex: state, setState. These 2 will go inside brackets and be set equal to the useState, which will have empty brackets. So useState is equal to the initial state and a function that updates the initial state.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  useEffect
&lt;/h2&gt;

&lt;p&gt;The above image is actually a great segue from useState to useEffect. Here, the useEffect is being used with a fetch and is using the function from our useState example. useEffect is great for doing things like fetching data or performing some side function. useEffect accepts 2 arguments and the second is actualyl optional.[5] Here, our useEffect looks like:&lt;/p&gt;

&lt;p&gt;useEffect(()=&amp;gt;{&lt;br&gt;
      fetch('&lt;a href="http://localhost:4000/data'"&gt;http://localhost:4000/data'&lt;/a&gt;)&lt;br&gt;
      .then(r =&amp;gt; r.json())&lt;br&gt;
      .then(setQuotes)&lt;br&gt;
  }, [])&lt;/p&gt;

&lt;p&gt;But we can simplify it to useEffect(()=&amp;gt;{}, []) in order to clearly see the structure. The first part is the set up and the latter part has the optional dependencies. In my example, I just have an empty array. useEffect is used when interacting with something external, for side effects, and it cannot be used inside loops.&lt;/p&gt;




&lt;ol&gt;
&lt;li&gt;&lt;a href="https://legacy.reactjs.org/"&gt;https://legacy.reactjs.org/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://react.dev/reference/react/useState"&gt;https://react.dev/reference/react/useState&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://react.dev/reference/react/useEffect"&gt;https://react.dev/reference/react/useEffect&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/uberVU/react-guide/blob/master/props-vs-state.md"&gt;https://github.com/uberVU/react-guide/blob/master/props-vs-state.md&lt;/a&gt;
5.&lt;a href="https://www.w3schools.com/react/react_useeffect.asp"&gt;https://www.w3schools.com/react/react_useeffect.asp&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript Arrays - Index Values and Adding to an Array</title>
      <dc:creator>Kevin O'Shaughnessy</dc:creator>
      <pubDate>Thu, 07 Sep 2023 14:02:33 +0000</pubDate>
      <link>https://forem.com/keoshaug/javascript-arrays-index-values-and-adding-to-an-array-27ol</link>
      <guid>https://forem.com/keoshaug/javascript-arrays-index-values-and-adding-to-an-array-27ol</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Arrays are common JavaScript variables which hold more than 1 value rather than just using separate variables. For example, using basic JavaScript, we could list each value independently or we could list them in an array.&lt;/p&gt;

&lt;p&gt;const fruit1 = "apple";&lt;br&gt;
const fruit2 = "orange";&lt;br&gt;
const fruit3 = "grape";&lt;br&gt;
const fruit4 = "strawberry";&lt;/p&gt;

&lt;p&gt;const fruit = ["apple", "orange", "grape", "strawberry"];&lt;/p&gt;

&lt;p&gt;Arrays can be manipulated -added to, subtracted from, or render only certain values. Arrays can also be listed vertically, for clarity, when needed:&lt;/p&gt;

&lt;p&gt;const fruit = [&lt;br&gt;
    "apple",&lt;br&gt;
    "orange",&lt;br&gt;
    "grape",&lt;br&gt;
    "strawberry"&lt;br&gt;
];&lt;/p&gt;

&lt;h2&gt;
  
  
  Index Values
&lt;/h2&gt;

&lt;p&gt;Each array item has its own index number corresponding to its place in the array.[1] This numbering system starts at 0 and goes up to as many objects are in the array. For example, the index of "apple" is 0, so fruit[0] = "apple". Other values include:&lt;/p&gt;

&lt;p&gt;fruit[1] = "orange"&lt;br&gt;
fruit[2] = "grape"&lt;br&gt;
fruit[3] = "strawberry"&lt;/p&gt;

&lt;p&gt;So, this array has 4 values but only goes up to the number 3 because the index value of the first item is always 0. &lt;/p&gt;

&lt;h1&gt;
  
  
  Adding to an Array
&lt;/h1&gt;

&lt;p&gt;There are a couple methods for adding items to an array. We can use push() to add something to the end of an array. To add something to the beginning, we would use the unshift method.[2] For our example, this would look like the following:&lt;/p&gt;

&lt;p&gt;Adding to the End&lt;br&gt;
const fruit = ["apple", "orange", "grape", "strawberry"];&lt;br&gt;
fruit.push("dragon fruit")&lt;br&gt;
console.log(fruit) would show the value:&lt;br&gt;
["apple", "orange", "grape", "strawberry", "dragon fruit"]&lt;/p&gt;

&lt;p&gt;Adding to the beginning&lt;br&gt;
const fruit = ["apple", "orange", "grape", "strawberry"];&lt;br&gt;
fruit.unshift("dragon fruit")&lt;br&gt;
console.log(fruit) would show the value:&lt;br&gt;
["dragon fruit", "apple", "orange", "grape", "strawberry"]&lt;/p&gt;

&lt;p&gt;In each of these cases, the original array is being modified, we are not creating new arrays.&lt;/p&gt;




&lt;p&gt;Sources Consulted:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://www.w3schools.com/js/js_arrays.asp"&gt;https://www.w3schools.com/js/js_arrays.asp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Fluke, Joshua. '5 Must Know Interview Questions for Javascript!' &lt;a href="https://www.youtube.com/watch?v=6Wzj7kxfRdI"&gt;https://www.youtube.com/watch?v=6Wzj7kxfRdI&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>arrays</category>
    </item>
    <item>
      <title>Asynchronous JavaScript</title>
      <dc:creator>Kevin O'Shaughnessy</dc:creator>
      <pubDate>Wed, 28 Jun 2023 14:09:27 +0000</pubDate>
      <link>https://forem.com/keoshaug/asynchronous-javascript-44p9</link>
      <guid>https://forem.com/keoshaug/asynchronous-javascript-44p9</guid>
      <description>&lt;p&gt;When learning the basics of JavaScript, you should quickly understand that it is a single-threaded language. This means that only one line of code can run at once. In order to run complex code, it is necessary to understand how to run code beyond the basic sequential flow.&lt;/p&gt;

&lt;p&gt;Asynchronous JavaScript is particularly useful when dealing with code which may take longer to run. &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing"&gt;MDN Web Docs&lt;/a&gt; defines it as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Examples of where this can be useful include callback functions, fetching data from an API, promises, and events with event listeners.&lt;/p&gt;

&lt;p&gt;If we look at an example, we can see how this works. The following, setTimeout() is a method that sets a timer for a specified piece of code. Notice that the number of seconds, rather than the order, dictate what the output will be.&lt;/p&gt;

&lt;p&gt;setTimeout(() =&amp;gt; {&lt;br&gt;
  console.log("message 1");&lt;br&gt;
}, 7000);&lt;br&gt;
setTimeout(() =&amp;gt; {&lt;br&gt;
  console.log("message 2");&lt;br&gt;
}, 5000);&lt;br&gt;
setTimeout(() =&amp;gt; {&lt;br&gt;
  console.log("message 3");&lt;br&gt;
}, 1000);&lt;/p&gt;

&lt;p&gt;// Output:&lt;/p&gt;

&lt;p&gt;// message 3&lt;br&gt;
// message 2&lt;br&gt;
// message 1&lt;/p&gt;

&lt;p&gt;Message 3 only takes 1 second, message 2 5 seconds, and message 1 7 seconds. The code is read in order, line by line, but the output is asynchronous, based on the time it takes to process the three different messages.&lt;/p&gt;

&lt;p&gt;Asynchronous operations are handled by an event loop. The code in the console will be read and invoked line by line but certain operations in the stack will take more time and be placed in a task queue, from which the event loop will manage their execution in a single-threaded language like JavaScript. &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
      <category>help</category>
    </item>
    <item>
      <title>Error: listen EADDRINUSE: address already in use (JSON)</title>
      <dc:creator>Kevin O'Shaughnessy</dc:creator>
      <pubDate>Wed, 24 May 2023 22:34:05 +0000</pubDate>
      <link>https://forem.com/keoshaug/error-listen-eaddrinuse-address-already-in-use-json-548f</link>
      <guid>https://forem.com/keoshaug/error-listen-eaddrinuse-address-already-in-use-json-548f</guid>
      <description>&lt;p&gt;THE PROBLEM&lt;/p&gt;

&lt;p&gt;I began the Ramen Rater assignment for Flatiron School (essentially a lab focused around using GET, POST, PATCH for Postman) by running json-server --watch db.json     and I got the following error:&lt;/p&gt;

&lt;p&gt;Some error occurred Error: listen EADDRINUSE: address already in use 127.0.0.1:3000&lt;br&gt;
    at Server.setupListenHandle &lt;a href="https://dev.tonode:net:1740:16"&gt;as _listen2&lt;/a&gt;&lt;br&gt;
    at listenInCluster (node:net:1788:12)&lt;br&gt;
    at GetAddrInfoReqWrap.doListen &lt;a href="https://dev.tonode:net:1937:7"&gt;as callback&lt;/a&gt;&lt;br&gt;
    at GetAddrInfoReqWrap.onlookup &lt;a href="https://dev.tonode:dns:109:8"&gt;as oncomplete&lt;/a&gt; {&lt;br&gt;
  code: 'EADDRINUSE',&lt;br&gt;
  errno: -98,&lt;br&gt;
  syscall: 'listen',&lt;br&gt;
  address: '127.0.0.1',&lt;br&gt;
  port: 3000&lt;/p&gt;

&lt;p&gt;With this error, I am not able to continue working on the lab (Postman). I researched this issue on Google, Stack Overflow, Slack, and Piazza. &lt;/p&gt;

&lt;p&gt;HOW TO FIX THIS&lt;/p&gt;

&lt;p&gt;I booked a help session and was able to get help fixing this issue in 2 ways. The core issue was that port 3000 was already in use due to a previous lab still running. One of the ways to fix this is to run npx kill-port in the terminal.&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%2Fnbxyccahbxd8m5t9vv47.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%2Fnbxyccahbxd8m5t9vv47.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This frees up the port and makes it available for running json-server --watch db.json and getting the API set up in Postman. &lt;/p&gt;

&lt;p&gt;A second way to solve this problem would be to find the process ID (PID) through entering lsof -i tcp:3000 in the terminal, then running kill -9 [process id number]. This will free up the port and allow one to run db.json.&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%2Fbkyjz9reo95c74pls2mv.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%2Fbkyjz9reo95c74pls2mv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>help</category>
      <category>webdev</category>
      <category>json</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Mapping Arrays with JavaScript</title>
      <dc:creator>Kevin O'Shaughnessy</dc:creator>
      <pubDate>Sat, 13 May 2023 02:04:20 +0000</pubDate>
      <link>https://forem.com/keoshaug/mapping-arrays-with-javascript-1c9h</link>
      <guid>https://forem.com/keoshaug/mapping-arrays-with-javascript-1c9h</guid>
      <description>&lt;p&gt;When learning the basics of JavaScript, one will come across arrays quite quickly. Arrays are useful and very malleable. They can be filtered through, reduced, expanded, and mapped. When mapping an array, one is accepting a callback function and passing each element so that it returns a new array that is the same length as the original with the data modified in the result but not permanently altering the original array.&lt;/p&gt;

&lt;p&gt;When one comes across the map() method, it is often referred to as Array.prototype.map(), as in the case of freeCodeCamp [1].&lt;/p&gt;

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

&lt;p&gt;The map() method is great for scanning through data for specific pieces of information in each array and creating a new array with it. I have an example below:&lt;/p&gt;

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

&lt;p&gt;Using the above data related to some of the most prominent Japanese leaders of the Meiji Restoration, I can use the map() method to pull the names of these people and create a new array with them.&lt;/p&gt;

&lt;p&gt;I hope this helps beginning students of front end or full stack web development in understanding an essential part of JavaScript. Map() is widely applicable for numerous types of data in arrays, from retrieving names to product prices.&lt;/p&gt;




&lt;p&gt;Sources:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;[&lt;a href="https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array"&gt;https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/functional-programming/use-the-map-method-to-extract-data-from-an-array&lt;/a&gt;]&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;[&lt;a href="https://www.w3schools.com/jsref/jsref_map.asp"&gt;https://www.w3schools.com/jsref/jsref_map.asp&lt;/a&gt;] W3 Schools is another great resource for learning about arrays. They succinctly list the 3 major points when it comes to map():&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;"map() creates a new array from calling a function for every array element.&lt;/p&gt;

&lt;p&gt;map() does not execute the function for empty elements.&lt;/p&gt;

&lt;p&gt;map() does not change the original array."&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;[&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map&lt;/a&gt;]&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;[&lt;a href="https://www.youtube.com/watch?v=G6J2kl1aVao"&gt;https://www.youtube.com/watch?v=G6J2kl1aVao&lt;/a&gt;]&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>The Journey Begins</title>
      <dc:creator>Kevin O'Shaughnessy</dc:creator>
      <pubDate>Thu, 09 Mar 2023 21:25:45 +0000</pubDate>
      <link>https://forem.com/keoshaug/the-journey-begins-1o97</link>
      <guid>https://forem.com/keoshaug/the-journey-begins-1o97</guid>
      <description>&lt;p&gt;"Wisdom is the daughter of experience." -Leonardo da Vinci (1452-1519)&lt;/p&gt;

&lt;p&gt;My path towards software engineering has begun. Actually, I briefly studied HTML and CSS on freeCodeCamp several years back. JavaScript is a stern teacher and certainly instills a sense of humility in the student. From perseverance, however, comes the necessary knowledge to move forward.&lt;/p&gt;

&lt;p&gt;I come to coding with a background in liberal arts (Rutgers: MA and BA in history), warehouse leadership, and a basic understanding of cybersecurity acquired via Kenzie Academy last year. I'm incredibly grateful to Flatiron for being able to cultivate a passion for web development with this opportunity. Flatiron has a tremendous benefit over other schools in that it is very flexible for those who work full-time.&lt;/p&gt;

&lt;p&gt;Learning to code opens so many doors and allows one to thrive in the dynamic work environment of the 21st century. The future looks bright! Happy Coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>design</category>
    </item>
  </channel>
</rss>
