<?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: joshf500</title>
    <description>The latest articles on DEV Community by joshf500 (@joshf500).</description>
    <link>https://dev.to/joshf500</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%2F1060306%2Fce76db22-483c-4c00-af2c-4c13bc8df17f.png</url>
      <title>DEV Community: joshf500</title>
      <link>https://dev.to/joshf500</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/joshf500"/>
    <language>en</language>
    <item>
      <title>Use Cases for the useState, useContext, and useLocation Hooks in React</title>
      <dc:creator>joshf500</dc:creator>
      <pubDate>Tue, 02 May 2023 21:22:35 +0000</pubDate>
      <link>https://dev.to/joshf500/use-cases-for-the-usestate-usecontext-and-uselocation-hooks-in-react-o1p</link>
      <guid>https://dev.to/joshf500/use-cases-for-the-usestate-usecontext-and-uselocation-hooks-in-react-o1p</guid>
      <description>&lt;p&gt;In React, hooks allow us to conveniently access and update variables without using class components. Let's explore some of the differences between three common ones, useState, useContext, and useLocation.&lt;/p&gt;

&lt;p&gt;useState&lt;/p&gt;

&lt;p&gt;When we import useState from React, we can declare a variable as a local state, and update it with a specified callback function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import {useEffect, useState} from "react";

export default function CreateByAirline(){
    const [airlines, setAirlines] = useState([])
    const [searchInput, setSearchInput] = useState("");
    const navigate = useNavigate()
    useEffect(() =&amp;gt; {
        fetch(`http://localhost:6001/airlines`)
          .then(res =&amp;gt; res.json())
          .then(data =&amp;gt; {
            setAirlines(data)
           })
      }, []);
...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we initialize the "airlines" state as an empty array. We then fetch data from a local host and pass that data into the "airlines" state when we call &lt;code&gt;.then(data =&amp;gt; {setAirlines(data)})&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The useState hook is useful for updating variables that only apply to our local component. However, there are other hooks that allow us to share data between components. &lt;/p&gt;

&lt;p&gt;The useContext hook gives a component access to context data and re-renders the component when the data changes. &lt;br&gt;
It takes a context object created by the createContext method as its argument and returns the current context value. Then, when useContext is imported into another component, we can access data from the context. &lt;/p&gt;

&lt;p&gt;The useContext hook is useful when we would like to pass &lt;br&gt;
multiple data points between components. For example, when we call fetch requests inside of a context, we can set several state variables that we can now access in other components. This can greatly simplify the process of passing data between components, rather than passing props up and down the tree.&lt;/p&gt;

&lt;p&gt;useLocation&lt;/p&gt;

&lt;p&gt;When a component hosts a URL that links to another, we can pass data between them using useLocation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import {useNavigate} from 'react-router-dom'

function ViewAirlineList({airlines}){
const navigate = useNavigate()


return (
    &amp;lt;ul&amp;gt;
        {airlines.map((airline) =&amp;gt; {
        return(
        &amp;lt;span class="options" key={airline.id} onClick={()=&amp;gt;{navigate('/reviewslist',{state:{ airline }});}} &amp;gt;
            &amp;lt;img src={airline.logo}/&amp;gt;
            &amp;lt;p&amp;gt;{airline.name}&amp;lt;/p&amp;gt;
        &amp;lt;/span&amp;gt;
        )
        })}
    &amp;lt;/ul&amp;gt;
)
}
export default ViewAirlineList

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

&lt;/div&gt;



&lt;p&gt;In the example above, the &lt;code&gt;ViewAirlineList&lt;/code&gt; component has a navigation function to the &lt;code&gt;/reviewslist&lt;/code&gt; route. Inside this function, we can declare and assign a state. In this case, when a given element of the &lt;code&gt;airlines&lt;/code&gt; array is clicked, the &lt;code&gt;navigate&lt;/code&gt; function executes and the state is assigned to the clicked element.&lt;/p&gt;

&lt;p&gt;Then, when we navigate to the corresponding component of the &lt;code&gt;/reviewslist&lt;/code&gt; route, we can call on the state that we assigned in the &lt;code&gt;ViewAirlineList&lt;/code&gt; component by importing useLocation and calling the &lt;code&gt;useLocation()&lt;/code&gt; function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useEffect } from "react";
import React, {useState} from "react";
import { useLocation } from "react-router-dom";
function ReviewsList() {
  const [reviews, setReviews] = useState([])
  const location = useLocation()
...

return (
 &amp;lt;main&amp;gt;
      &amp;lt;h2&amp;gt;{location.state.airline.name}&amp;lt;/h2&amp;gt;
 ...
 &amp;lt;/main&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we call &lt;code&gt;{location.state.airline.name}&lt;/code&gt;, we access the location from which we navigated, as well as the state. This state was previously set as an object called &lt;code&gt;airline&lt;/code&gt;, which contains a &lt;code&gt;name&lt;/code&gt; key. &lt;/p&gt;

&lt;p&gt;The useLocation hook is useful for accessing information from the current URL location. This is helpful when the data we need in the current component was set within the component from which we navigated to get to the current one.&lt;/p&gt;

&lt;p&gt;Though the useState, useContext, and useLocation hooks are all used to pass and update variables, their use cases vary depending on the locations where the data is set and where we want to access or update it. If we want to set and update data within the same component, useState will suffice. If we want to manage a state globally, we can set a component as the context provider using createContext, and set the components which we want to access that data as consumers using useContext. If we want to manage a state that was set in the component from which we navigated, the useLocation hook is the best option.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Advantages of Javascript's Built-in Array Iterator Methods</title>
      <dc:creator>joshf500</dc:creator>
      <pubDate>Thu, 06 Apr 2023 21:04:55 +0000</pubDate>
      <link>https://dev.to/joshf500/advantages-of-javascripts-built-in-array-iterator-methods-55fe</link>
      <guid>https://dev.to/joshf500/advantages-of-javascripts-built-in-array-iterator-methods-55fe</guid>
      <description>&lt;p&gt;An array is a data structure that allows us to store and manipulate a set of values. In Javascript, there are several built-in array iterator methods that conveniently apply a function to each element of the array. These methods can serve several purposes, including the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Altering each element of an array&lt;/li&gt;
&lt;li&gt;Returning a new array that is comprised of those altered elements&lt;/li&gt;
&lt;li&gt;Shrinking an array to include only the elements that satisfy a certain condition&lt;/li&gt;
&lt;li&gt;Accumulating an array's elements as inputs and returning a result as a single value. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s walk through some examples of these methods to see how we can use them to simplify our code:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.forEach()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.forEach()&lt;/code&gt; method can be used to execute a function on each element of an array. For example, in an array of integers, we can use this method to add 50 to each element and print each sum. If we want to achieve this result using a traditional &lt;code&gt;for&lt;/code&gt; loop, it may look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numberSet = [1, 2, 3, 4, 5, 6]
for (let i=0; i&amp;lt;numberSet.length; i++){
numberSet[i]=numberSet[i]+50
console.log(numberSet[i])
}
//prints: 51 52 53 54 55 56
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's print the same result using the &lt;code&gt;.forEach()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numberSet = [1, 2, 3, 4, 5, 6]
numberSet.forEach(i =&amp;gt; console.log(i+50))
//prints: 51 52 53 54 55 56
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this comparison, we can see how the &lt;code&gt;.forEach()&lt;/code&gt; method applies modifiers to each element of an array in fewer lines of code than a traditional &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.map()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.map()&lt;/code&gt; method can be used to execute a function on each element of an array, and then return a new array with the modified result. For example, in an array of integers, we can use this method to print a new array comprised of each element multiplied by 5. If we want to achieve this result using a traditional &lt;code&gt;for&lt;/code&gt; loop, it may look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numberSet = [1, 2, 3, 4, 5, 6]
let timesFive=[]
for (let i=0; i&amp;lt;numberSet.length; i++){
timesFive[i]=numberSet[i]*5
}
console.log(timesFive)
// prints: [ 5, 10, 15, 20, 25, 30 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's print the same result using the &lt;code&gt;.map()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numberSet = [1, 2, 3, 4, 5, 6]
let timesFive = numberSet.map(i =&amp;gt; (i * 5))
console.log(timesFive)
// prints: [ 5, 10, 15, 20, 25, 30 ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this comparison, we can see how the &lt;code&gt;.map()&lt;/code&gt; method applies modifiers to each element of an array and returns the modified elements in a new array in fewer lines of code than a traditional &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.filter()&lt;/code&gt; method can be used to create a new array that includes only the elements which satisfy a specified condition. For example, in an array of strings, we can use this method to print a new array that includes only the elements which contain the "r" character. If we want to achieve this result using a traditional &lt;code&gt;for&lt;/code&gt; loop, it may look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruits = ["banana","fig","durian","papaya","raspberry","starfruit"]
let fruitsWithR=[]
for(let i=0; i&amp;lt;fruits.length; i++){
  if (fruits[i].includes("r")==true){
  fruitsWithR.push(fruits[i])
  }
}
console.log(fruitsWithR)
//prints [ 'durian', 'raspberry', 'starfruit' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's print the same result using the &lt;code&gt;.filter()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let fruits = ["banana","fig","durian","papaya","raspberry","starfruit"]
let fruitsWithR = fruits.filter(i=&amp;gt;i.includes("r")==true)
console.log(fruitsWithR)
//prints [ 'durian', 'raspberry', 'starfruit' ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this comparison, we can see how the &lt;code&gt;.filter()&lt;/code&gt; method returns a new array containing the elements that satisfy a specified condition in fewer lines of code than a traditional &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.reduce()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.reduce()&lt;/code&gt; method can be used to return a single value that is the cumulative result of each element in an array. For example, in an array of integers, we can use this method to print the total product of each element. If we want to achieve this result using a traditional &lt;code&gt;for&lt;/code&gt; loop, it may look like the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numberSet = [1, 2, 3, 4, 5, 6]
let product=1
for (let i=0; i&amp;lt;numberSet.length; i++){
product=product*numberSet[i]
}
console.log(product)
//prints 720
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's print the same result using the &lt;code&gt;.reduce()&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let numberSet = [1, 2, 3, 4, 5, 6]
let product= numberSet.reduce((i,j) =&amp;gt; i*j,1)
console.log(product)
//prints 720
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this comparison, we can see how the &lt;code&gt;.reduce()&lt;/code&gt; method returns a cumulative result using the elements of the initial array as inputs, and requires fewer lines of code than a traditional &lt;code&gt;for&lt;/code&gt; loop.&lt;/p&gt;

&lt;p&gt;These examples show how Javascript's built-in array iterator methods allow us to execute a function on each element of an array, and to do so more concisely than traditional &lt;code&gt;for&lt;/code&gt; loops.&lt;/p&gt;

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