<?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: peytono</title>
    <description>The latest articles on DEV Community by peytono (@peytono).</description>
    <link>https://dev.to/peytono</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%2F1226750%2F22e0cbba-b682-42dc-837d-8d86fdb2b302.jpeg</url>
      <title>DEV Community: peytono</title>
      <link>https://dev.to/peytono</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/peytono"/>
    <language>en</language>
    <item>
      <title>Intro to React Hooks</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Mon, 29 Apr 2024 01:59:41 +0000</pubDate>
      <link>https://dev.to/peytono/intro-to-react-hooks-397h</link>
      <guid>https://dev.to/peytono/intro-to-react-hooks-397h</guid>
      <description>&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;In React you've always had access to state, a tool that allowed you to hold on to temporary values, non persisted changes, with easy ways to rerender your components to reflect those changes. At first, if needing state in a component, you’d need to take advantage of class components and lifecycle methods. With class components the constructor, render, componentDidMount, etc. control the functionality of a certain lifecycle. Giving you control of what functionality happens over stages of the component. On more complicated components, this gets messy and doesn’t allow for a real separation of concerns. For example, a componentDidUpdate() method would require every functionality needed for any updates to the state. In this article we’ll go over the most common React Hooks.&lt;/p&gt;

&lt;p&gt;Introducing hooks! With React Hooks, all components should be functional. This rids the need for all lifecycle methods, including the constructor and render. Hooks allow for statefulness in functional components, as well as more control over your logic flow. All hooks must be imported into the file from React.&lt;/p&gt;

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

&lt;p&gt;The first hook to get acquainted with is useState, instead of adding values to the state object inside of the constructor and using setState, you’ll add useState inside your function statement, but before your return. When calling useState, you’ll use array destructing syntax to declare, the first being the name of your new state, then the update function, conventionally preceding the state name with ‘set’. Then you’ll pass in the initial state value inside the useState invocation like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;br&gt;
 Now anytime you need to change the value of the state you’ll use the update function, passing in the new value of state, or a function that resolves to the new value of state. Using the updater function is the same as setState, except the updater function is specific to the updated state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;startState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;hello&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;addMessage&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;startState&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isWaiting&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setWaiting&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the below example I'll also use axios, a great technology to help make HTTP requests.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;onSend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;setWaiting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;axios&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;chatbot&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;allMessages&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setWaiting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nf"&gt;addMessage&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;curMessages&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;curMessages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;
      &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;chatbot&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;db&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;})&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;failed&lt;/span&gt; &lt;span class="nx"&gt;sending&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

  &lt;span class="nf"&gt;setMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘’&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The second hook to get acquainted with is useEffect, which can take two parameters: the first, a callback function, most commonly an anonymous arrow function, being required, and the second, an array, to include the effects dependencies. Intuitively, whenever useEffect is triggered, the function passed in will be invoked. The dependencies array should consist of any reactive values from the callback. Reactive values include stateful values or other hooks. Meaning, anytime a dependency value changes, that useEffect will be called. useEffect is super helpful in splitting up your component functionality into much more readable and modularized code than using React class components lifecycle methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;bottomScroll&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;messages&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, useEffect, calling bottomScoll, will be invoked when the component renders, as well as anytime the value of messages changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  useMemo
&lt;/h2&gt;

&lt;p&gt;useMemo mixes functionality of useState, useEffect and a memoize function. If you’ve used a memoized function before, useMemo has a similar purpose, holding onto cached values for optimization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;EventList&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;eventsNow&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;filterEvents&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;events&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;In this first example, we’re creating a function EventList, which is destructuring events and time. Then we’re creating a variable from useMemo called eventsNow. Since we want to call our function filterEvents passing in arguments, we need to pass it into the body of an arrow function, and because events and time are reactive values, they belong in the dependency array. This allows eventsNow to hold onto the result from filterEvents, then any time one of the dependencies changes, eventsNow will keep the cached value until the filterEvents calculation is completed, then any rerendering will take place.&lt;/p&gt;

&lt;p&gt;The second example may seems less useful off the bat, but we’ll see how it comes into play.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we’re creating a state of user, an updater function of setUser, and an initial state of null.&lt;/p&gt;

&lt;p&gt;useMemo requires a function with no arguments, as well as a dependencies array. Dependency rules are the same as useEffect.&lt;/p&gt;

&lt;p&gt;We’ll see soon why this was useful, but our userState is now an object containing user, currently null, and setUser, which can change the value of user.&lt;/p&gt;

&lt;h2&gt;
  
  
  useContext
&lt;/h2&gt;

&lt;p&gt;React’s useContext is similar to useState, except it is accessible to any component, including children, inside its provider. This becomes more useful with deeply nested components or stateful values needed throughout the entire app as you won’t have to pass state down the entire tree of React nodes.&lt;/p&gt;

&lt;p&gt;Wanting to having the value of the user throughout the app, we’ll reuse the useMemo example above alongside useContext to create a UserContext and provider.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useMemo&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;UserContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createContext&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;UserContextProvider&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;children&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserContext&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;userState&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;children&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/UserContext.Provider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now inside App.jsx we’ll make the UserContextProvider a wrapper for all components. Making the context available everywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;react&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// imports of all components in return&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;UserContextProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Login&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HomePage&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Profile&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Events&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/UserContextProvider&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to use and update the context inside other components we’d have to do something like the example below. If you don’t need to update the context you can access is with just the first two lines, not needing to extract setUser from userContext.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;userContext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;UserContext&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userContext&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;axios&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;‘&lt;/span&gt;&lt;span class="nx"&gt;failed&lt;/span&gt; &lt;span class="nx"&gt;setting&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="err"&gt;’&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;setUser&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Thank you!
&lt;/h3&gt;

&lt;p&gt;Hopefully after this overview of the most common React Hooks you’ll be excited to see all you can do with this super helpful tool. If you don’t find the one that’s quite right for your needs, you can just create your own! This is just one of the amazing tools React has for us.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://react.dev/reference/react/hooks"&gt;React Hooks Docs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Learn Web Scraping with Cheerio</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Sun, 14 Apr 2024 21:44:06 +0000</pubDate>
      <link>https://dev.to/peytono/learn-web-scraping-with-cheerio-29c8</link>
      <guid>https://dev.to/peytono/learn-web-scraping-with-cheerio-29c8</guid>
      <description>&lt;h2&gt;
  
  
  An overview
&lt;/h2&gt;

&lt;p&gt;Haven’t heard of web scraping before? If you want to traverse web pages through Node.js, you’re in the right place! Web scraping is a process by which you load or connect to an external web page and extract data or HTML from the page. While an old practice, is now commonly used in machine learning and still very useful in innovation! Most web scraping libraries have their own API service. When it comes to Cheerio, its syntax is very similar to jQuery. For example, to grab all img tags from your loaded HTML:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const $img = $(‘img’);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Cheerio is a more simple solution to web scraping, but there are situations that it won’t work and you’ll need to actually interact with the page or get content not accessible through Cheerio.&lt;/p&gt;

&lt;h2&gt;
  
  
  Is Cheerio right for you?
&lt;/h2&gt;

&lt;p&gt;There are many more options out there than just Cheerio, but if needing HTML from a web page, this is a great one. If all you need is to select elements and some DOM manipulation, go with Cheerio. It’s super fast and has a very smooth learning curve. Unfortunately, if the site you need uses dynamic content or pagination, this is a sign that Cheerio might not work. You’ll absolutely want to check out your intended site with your Chrome Dev Tools to see what you’re working with! While you’re doing some digging, check for a robot.txt file to see if they have web scraping guidelines to follow. If this is your case, you may want to check out Puppeteer, built to be a browser animation tool, it allows you to interact with a site and get any fetched data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Getting started with Cheerio is quick and easy! Cheerio has support for yarn, npm, and pnpm. To install through npm:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install cheerio&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Then you can import cheerio to your file. If using ES6 or newer, you can then use:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import * as cheerio from 'cheerio';&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;or for commonJS:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const cheerio = require('cheerio');&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now you’re ready to use Cheerio!&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;There are several different loading methods in Cheerio, but the most supported and only one available out of the box is load. This method has one required argument, HTML in a string. Here’s a very basic example call...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cheerio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`
  &amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Heading&amp;lt;/h1&amp;gt;
    &amp;lt;p&amp;gt;Some website content&amp;lt;/p&amp;gt;
  &amp;lt;/body&amp;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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;p&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;text&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Receiving log:&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;Some website content&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Now you know the Cheerio basics!&lt;/p&gt;

&lt;h2&gt;
  
  
  Functional Usage
&lt;/h2&gt;

&lt;p&gt;So, this is all that’s needed if you already have the HTML, but you likely won’t. Now let’s say you wanted to know the most popular Reddit communities. First, you should go to Reddit and inspect the page. You can select the element you’d like by clicking the icon with a dotted line box and a cursor. Now you can see the tag name with all the classes of this element. And we can go to writing some code!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92orw5ob36sthylza94m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F92orw5ob36sthylza94m.png" alt="Inspecting Reddit Popular Communities" width="800" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since the load method takes a string of the HTML, we’ll first need to get the site's HTML. This can very easily be done with Axios. Once getting the response, you can use the load method on the response data. Then we can initialize a popular communities variable set to the return of the selector, passing in our tag name and classes, each separated by a period(Noticing the jQuery?).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://www.reddit.com/?feed=home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cheerio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$popCommunities&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;span.text-neutral-content.block.text-ellipsis.whitespace-nowrap.overflow-hidden&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$popCommunities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;text&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;r/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
   &lt;span class="p"&gt;})&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Failed getting reddit HTML&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Logs to the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DestinyTheGame&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;anime&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;destiny2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;         &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;FortNiteBR&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dndnext&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;          &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;buildapc&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;techsupport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jailbreak&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LivestreamFail&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;legaladvice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;LifeProTips&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AskCulinary&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Philippines&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;      &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;memes&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rainbow6&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;         &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Sneakers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;learnprogramming&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;RedDeadOnline&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jobs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you’re trying to get data unavailable through Cheerio&lt;/p&gt;

&lt;p&gt;We discussed before that sometimes Cheerio is not good for your case, wanted to show an example. It can be hard to know when you’re using a new technology incorrectly or it just can’t do what you need! Let’s check out &lt;a href="http://www.neworleans.com"&gt;www.neworleans.com&lt;/a&gt; to check out some events going on.&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40uq638bdw3agy31hc95.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F40uq638bdw3agy31hc95.png" alt="Inspecting Events on the New Orleans website" width="800" height="530"&gt;&lt;/a&gt;&lt;br&gt;
Let’s try the same thing we tried before to find what we need. Doing that, we’d want to find the div with classes ‘shared-item’ and ‘item’, spoiler, the query isn’t working! So, I’ll back out to the parent, with the class ‘shared-items-container’ to find the HTML inside.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
     &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://www.neworleans.com/events/upcoming-events/?skip=0&amp;amp;categoryid=40&amp;amp;startDate=04%2F11%2F2024&amp;amp;endDate=05%2F11%2F2024&amp;amp;sort=title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cheerio&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;$sharedItems&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div.shared-items-container&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$sharedItems&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;html&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
   &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The log we see is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"shared-items"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; 
    &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"container"&lt;/span&gt; 
    &lt;span class="na"&gt;data-sv-items-wrapper=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; 
    &lt;span class="na"&gt;data-sv-items=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Uh oh, the container div is empty! Unfortunately, this is data that Cheerio does not have access to. In this case, you may want to check out Puppeteer. &lt;/p&gt;

&lt;h2&gt;
  
  
  Outro
&lt;/h2&gt;

&lt;p&gt;If you had been familiar with API’s, but weren’t finding the information you needed, just wanted to learn more about a new topic, or were confused you weren’t getting the data you were expecting, I hope this helped you out! Now you know when to use Cheerio and how to get started. I’m pretty amazed at how quickly Cheerio can load and traverse the entire DOM of a site. Happy web scraping!&lt;/p&gt;

&lt;h2&gt;
  
  
  Resources
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://cheerio.js.org/docs/intro"&gt;Cheerio Docs&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.scraperapi.com/blog/cheerio-vs-puppeteer/"&gt;Cheerio vs Puppeteer&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.linkedin.com/advice/0/what-best-practices-ensuring-accurate-reliable-web"&gt;What are the best practices for ensuring accurate and reliable web scraping results?&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.scrapingbee.com/blog/web-scraping-javascript/"&gt;Web Scraping With JavaScript And NodeJS&lt;/a&gt;&lt;br&gt;
&lt;a href="https://pptr.dev/guides/what-is-puppeteer"&gt;Puppeteer Docs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webscraping</category>
      <category>cheerio</category>
      <category>programming</category>
      <category>node</category>
    </item>
    <item>
      <title>All About Code Reviews</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Mon, 01 Apr 2024 02:58:00 +0000</pubDate>
      <link>https://dev.to/peytono/all-about-code-reviews-1bh6</link>
      <guid>https://dev.to/peytono/all-about-code-reviews-1bh6</guid>
      <description>&lt;h2&gt;
  
  
  Overview
&lt;/h2&gt;

&lt;p&gt;A popular way to cut down on bugs and make sure proposed changes should be made is through code reviews. When working with teams and wanting to add your code to a repository, you open a pull request to that repo. Then other developers on your team can review your code, make comments, request changes, or, fingers crossed, approve your PR(pull request).&lt;br&gt;
Reviewing a PR can be a daunting task, especially if you’re new to the process. And of course, there are things to keep in mind when making a pull request. We’ll go through both sides of the process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Making a PR
&lt;/h2&gt;

&lt;p&gt;The title of the PR should be relevant and concise. When using verbs, use the imperative form. Add a description that lets the reviewer know the what, why, and how this code is a positive addition to the feature. Keeping pull requests small helps to not overwhelm reviewers, as well as continuous deployment helps trim down on “the second 90%”. An article from SmartBear, &lt;a href="https://smartbear.com/learn/code-review/best-practices-for-peer-code-review/"&gt;Best Practices for Code Review&lt;/a&gt;, saw that efficiency in a code review greatly diminishes after exceeding 400 lines of code. To get a bit more specific, they found, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“In practice, a review of 200-400 LOC over 60 to 90 minutes should yield 70-90% defect discovery.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Comments giving context to changes add to efficiency both by helping the developer spot their bugs and helping the reviewer understand the code’s purpose. &lt;/p&gt;

&lt;h2&gt;
  
  
  Streamlining
&lt;/h2&gt;

&lt;p&gt;Code reviews should not be used to nitpick formatting. Use formatters and linters, these save large amounts of time. These will have to be set up at the beginning of a project, then developers no longer need to spend time arguing about spacing or other minor syntactical issues. As long as you remember to use your formatter! In this same vein, avoid wasting time arguing about variable names. Focus on the important stuff. It’ll be easier and more tempting to point out the easy things, like syntax and naming, but they won’t impact the performance or how the new feature works.&lt;br&gt;
Tests should only stay in the codebase if they are helpful. Good tests help ensure merges won’t break the production instance. Don’t keep tests that just slow the team down.&lt;/p&gt;

&lt;h2&gt;
  
  
  Most effective uses for code reviews
&lt;/h2&gt;

&lt;p&gt;A developer is new to the company or still a green developer. Code reviews in this case will teach the newbie the company’s styles and conventions. This can also be used the opposite way. A developer new to the repository can review a pull request to better understand the conventions or the new feature.&lt;br&gt;
If making changes to integral or sensitive parts of the code base. Examples would be changing the architectural structure or dealing with sensitive data. While these instances should be less frequent, you’ll want extra eyes on the pull request to double-check it won’t take down the site or add vulnerabilities.&lt;br&gt;
When it seems like a whole discussion is breaking out in the comments, that’s a sign to schedule a meeting. What could end up being a giant thread of unproductive messages, could be more efficient by a quick meetup.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reviewing Code
&lt;/h2&gt;

&lt;p&gt;Be nice, and add encouragement.&lt;/p&gt;

&lt;p&gt;In &lt;a href="https://www.freecodecamp.org/news/a-zen-manifesto-for-effective-code-reviews-e30b5c95204a"&gt;A zen manifesto for effective code reviews&lt;/a&gt;, Jean-Charles Fabre wrote, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Did you know that in written communication, neutral content looks more negative than it actually is? Beware of this bias and include emojis when needed to get the tone right in your comments.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There’s no reason to lower morale by not considering other’s &lt;br&gt;
feelings. When seeing areas for improvement, use language &lt;br&gt;
that promotes a conversation. If you have an idea to improve &lt;br&gt;
something, ask what they think about that idea, instead of &lt;br&gt;
asking why they haven’t already implemented it. This is &lt;br&gt;
kinder and gives the reviewer more context to the decisions &lt;br&gt;
made. Both parties can learn more from questions than &lt;br&gt;
requests.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Be engaging, if you’re confused about something, ask questions. If you have other ideas, feel free to suggest them and weigh out the pros and cons. Always reach out with helpful resources!&lt;/li&gt;
&lt;li&gt;Junior developers should still feel comfortable giving code reviews to their senior developers. Letting someone know you found something confusing might point them towards refactoring to something better. &lt;/li&gt;
&lt;li&gt;Keep up with pull requests; getting to a review late will make it more difficult for the reviewee to remember exactly what they were doing.&lt;/li&gt;
&lt;li&gt;Make sure the outcome of the code is in line with the purpose of the pull request.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Things to avoid
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Your style opinions are not fact, and should not be presented as such. If you’ve got documentation or style guides that back you up, you can give a reference to the documentation. &lt;/li&gt;
&lt;li&gt;Don’t overwhelm the contributor with comments, if they’re following a poor pattern, reference the poor pattern instead of leaving a comment on every single instance of it.&lt;/li&gt;
&lt;li&gt;Don’t comment on code they didn’t write. If they worked on a feature that had some bad code in it, don’t try and force them to fix it. What you can do is review what they worked on and say you’d like to come back to the previous poorly written section. &lt;/li&gt;
&lt;li&gt;Judgmental phrasing. You want to build up your peers and help improve the overall product as a team. Pay attention to phrasing. Instead of asking why they didn’t do it the way you would’ve or being sarcastic, let them know another way, and what the benefit would be. &lt;/li&gt;
&lt;li&gt;Don’t use emojis for comments, unless they’re positive. The positive emojis can be encouraging and fun, but if leaving an emoji to point out something negative; you aren’t explaining why and it can come off as mean. &lt;/li&gt;
&lt;li&gt;Ignoring comments. Someone took the time to read your pull request and try to help you improve it. Be respectful to your other developers. If it’s advice you don’t want to implement, you should still respond and maybe let them know why not.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Outro
&lt;/h2&gt;

&lt;p&gt;If you were like me and overwhelmed when first tasked with reviewing a peer’s code, hopefully, you should now feel confident to get out and start reviewing! Regardless, with some new techniques for code reviews, you’ll know how to get at them more effectively! Thanks for reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  More Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://kellysutton.com/2018/10/08/8-tips-for-great-code-reviews.html"&gt;8 Tips for Great Code Reviews&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.giladpeleg.com/blog/better-code-review/"&gt;A Better Code Review&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@sandya.sankarram/unlearning-toxic-behaviors-in-a-code-review-culture-b7c295452a3c"&gt;Unlearning Toxic Behaviors in a Code Review Culture&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>devops</category>
      <category>productivity</category>
    </item>
    <item>
      <title>A React Native Intro</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Mon, 26 Feb 2024 00:08:37 +0000</pubDate>
      <link>https://dev.to/peytono/a-react-native-intro-3h92</link>
      <guid>https://dev.to/peytono/a-react-native-intro-3h92</guid>
      <description>&lt;h3&gt;
  
  
  React Native Overview
&lt;/h3&gt;

&lt;p&gt;If you have enjoyed the React JavaScript library, you should check out &lt;a href="https://reactnative.dev/"&gt;React Native&lt;/a&gt;. It is now one of the most popular frameworks for mobile app development. The framework is open source and like React, was created by Facebook. (If you don’t know anything about React, I have another blog post comparing &lt;a href="https://dev.to/peytono/react-vs-angular-p5a"&gt;React vs Angular&lt;/a&gt;.) React Native is a cross-platform framework, that allows the creation of an app to run on iOS,  Android, and, with the use of &lt;a href="https://necolas.github.io/react-native-web/docs/setup/"&gt;React Native for Web&lt;/a&gt;, browsers. Kumar Harsh has an in depth article, "&lt;a href="https://necolas.github.io/react-native-web/docs/setup/"&gt;The complete guide to React Native for Web&lt;/a&gt;". React Native uses &lt;a href="https://expo.dev/"&gt;Expo&lt;/a&gt; to view your progress as you go. Their app, &lt;a href="https://docs.expo.dev/get-started/expo-go/"&gt;Expo Go&lt;/a&gt;, gives you the ability to experience what you’re building from your mobile device.&lt;/p&gt;

&lt;p&gt;Even though “native” is in the name. React Native is not considered &lt;a href="https://mdevelopers.com/blog/what-is-a-native-mobile-app-development"&gt;native app development&lt;/a&gt;. Native app development involves focusing the app on one platform. They offer very high performance and typically offer better &lt;a href="https://dev.to/peytono/what-makes-for-good-uxui-2egf"&gt;UX/UI&lt;/a&gt; than web development. Performance is increased since the code is compiled directly into the device’s core language. In native app development, it’s very easy to follow the specific OS guidelines, allowing for optimal user experience. While these are some great advantages, it also means you’d have to build two separate apps to work on both operating systems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Getting Started
&lt;/h3&gt;

&lt;p&gt;Either npm or yarn can be used to start a new React Native Project, below will be the npm example. If you follow these steps, be sure to be in your code editor’s terminal in the directory you’d like the project to be inside.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-expo-app YourNewExcitingProject

cd YourNewExcitingProject
npx expo start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After those three lines, you’re ready to use Expo Go and take a first look at your project! As long as you’ve installed Expo Go, Apple users can scan the QR code in their terminals from their phone’s camera app, and this will take you to the live view of the project! Android users must first open the Expo Go app and then use the app to scan the same QR code. &lt;br&gt;
Now, as you edit YourNewExcitingProject or whatever you named it, the edits will be reflected. Note: I sometimes have to close and reopen the app to reflect changes after fixing errors.&lt;/p&gt;

&lt;p&gt;Having prior experience in React will give you a major head start in working with this framework, you may not understand React Native until you have an understanding of React. The fundamental building blocks of React, components, JSX, props, and state, will all be used in React Native projects.&lt;/p&gt;

&lt;p&gt;You’ll be able to import all the React modules you’ve used, and of course, React Native has built upon these. Giving access to new modules built specifically for iOS or Android views. &lt;/p&gt;

&lt;p&gt;Using only built-in modules I was able to very quickly put together this little app. These screenshots were taken from my phone while using the Expo Go app to preview my work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2me51am7er6gts6vfhq.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn2me51am7er6gts6vfhq.jpeg" alt="My little React Native App" width="750" height="1334"&gt;&lt;/a&gt;&lt;br&gt;
And after clicking the Do Something button...&lt;br&gt;
&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiocjfxw99mlqn0jq3u1m.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fiocjfxw99mlqn0jq3u1m.jpeg" alt="My little React Native App after button click" width="750" height="1334"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I created a new Home component in a separate file. Then all I did in App was import it and added it to the App’s return, in typical react fashion.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { StatusBar } from 'expo-status-bar';
import { StyleSheet, View } from 'react-native';

import Home from './components/Home.js';

export default function App() {
  return (
    &amp;lt;View style={styles.container}&amp;gt;
      &amp;lt;Home /&amp;gt;
      &amp;lt;StatusBar style="auto" /&amp;gt;
    &amp;lt;/View&amp;gt;
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#fff',
    alignItems: 'center',
    justifyContent: 'center',
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Home, I imported several React Native components, and added them to my return, just with an addition of some simple attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { View, Text, Button, Alert, Image } from 'react-native';

export default function Home() {
  return (
    &amp;lt;View&amp;gt;
      &amp;lt;Text style={{ fontSize: 20 }}&amp;gt;Here is the Home View&amp;lt;/Text&amp;gt;
      &amp;lt;Button
        title='Do Something'
        onPress={() =&amp;gt; Alert.alert('You did something!')}
      /&amp;gt;
      &amp;lt;Image
        source={{ uri: 'https://reactnative.dev/docs/assets/p_cat2.png' }}
        style={{ width: 200, height: 200 }}
      /&amp;gt;
    &amp;lt;/View&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example did not show using any use of props or state, but hopefully gets you excited about how quickly you can get started! &lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap up!
&lt;/h3&gt;

&lt;p&gt;No matter what app you’re trying to build, React Native is a framework that will give you many possibilities and tools to get you there. Take advantage of all the other React libraries and frameworks, such as &lt;a href="https://react-redux.js.org/"&gt;React Redux&lt;/a&gt; or &lt;a href="https://react-bootstrap.netlify.app/"&gt;React Bootstrap&lt;/a&gt;. If you’re new to programming, there will be many opportunities for deep dives, but you should have an easy enough time starting the project and seeing some nice results!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>opensource</category>
      <category>reactnative</category>
      <category>programming</category>
    </item>
    <item>
      <title>What makes for good UX/UI?</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Mon, 19 Feb 2024 04:44:28 +0000</pubDate>
      <link>https://dev.to/peytono/what-makes-for-good-uxui-2egf</link>
      <guid>https://dev.to/peytono/what-makes-for-good-uxui-2egf</guid>
      <description>&lt;h2&gt;
  
  
  The Basics
&lt;/h2&gt;

&lt;h3&gt;
  
  
  User Experience (UX)
&lt;/h3&gt;

&lt;p&gt;Refers to the users’ interactions with the application. What the user benefits from while on the app.About the feel or flow of the entire app. Making the overall product intuitive and enjoyable is a part of UX.&lt;/p&gt;

&lt;h3&gt;
  
  
  User Interface (UI)
&lt;/h3&gt;

&lt;p&gt;Refers to the visual aspects of the application. The appearance or animations of individual components or elements are parts of UI. The User Interface(UI) contributes to the User Experience(UX).&lt;/p&gt;

&lt;p&gt;These concepts have overlapping similarities, but both make up your users' enjoyment and likelihood of returning. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fypvpylo95uzi5jyjb9to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fypvpylo95uzi5jyjb9to.png" alt="Key differences between UX and UI Design" width="800" height="659"&gt;&lt;/a&gt;&lt;br&gt;
Image from &lt;a href="https://bootcamp.cvn.columbia.edu/blog/what-is-ux-design/"&gt;What is UX Design? Differences Between UX and UI Design&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  A positive User Experience (UX)
&lt;/h2&gt;

&lt;p&gt;Looking into some super successful companies we can see what works.&lt;/p&gt;

&lt;h3&gt;
  
  
  Airbnb
&lt;/h3&gt;

&lt;p&gt;The founders of Airbnb stumbled upon an idea that was not widely available. There was a consumer want for non-hotel lodging. The founders stayed focused on the user experience. Keeping every listing personalized, lets the user feel a connection. They have many interactive features. One is a map that includes many filter options. Another is an “Experience” feature that allows Airbnb to develop further a connection with the user and the user to their environment. Public reviews and user verifications allow hosts and travelers to feel confident in their decisions. It’s hard to trust just the seller! This app has become so successful that many landlords have tried switching to running Airbnbs and even made renting less affordable in many areas.&lt;/p&gt;

&lt;h3&gt;
  
  
  Google
&lt;/h3&gt;

&lt;p&gt;Google took over as a search engine because of its far superior UX. It started with being faster, prettier, and giving more relevant results than its competitors. Their About Page has an article, &lt;a href="https://about.google/intl/ALL_us/our-story"&gt;From the Garage to the Googleplex&lt;/a&gt; about the founders' original mission, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“to organize the world’s information and make it universally accessible and useful.” &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So successful, that it was the first technology company that’s usage became a verb. Now, Google is so much more. You can easily change your search between websites, images, shopping, and more. Currently, the search engine even gives snippets or relevant question drop-downs that should be useful to you based on your search, not even needing to leave the search page. The company did not stop there and created many sharing-based apps that allowed people to work together for free on the internet like never before. Logged into your Google account, you can switch seamlessly between the search engine, mail, calendar, etc...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikggfqmcj539807q74od.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fikggfqmcj539807q74od.png" alt="What makes for good ux google result" width="800" height="217"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above image shows the first thing shown from search &lt;a href="https://www.google.com/search?q=What+makes+for+good+ux%3F&amp;amp;sca_esv=f662d5406997c69b&amp;amp;sxsrf=ACQVn0-n-EZ4rkAQF366V-lhbfyy9lRkgQ%3A1708313522933&amp;amp;ei=ssvSZby_OMSnmtkP_9ChqAc&amp;amp;ved=0ahUKEwi84Mvlu7aEAxXEkyYFHX9oCHUQ4dUDCBA&amp;amp;uact=5&amp;amp;oq=What+makes+for+good+ux%3F&amp;amp;gs_lp=Egxnd3Mtd2l6LXNlcnAiF1doYXQgbWFrZXMgZm9yIGdvb2QgdXg_MgQQIxgnSLR2UKBzWKBzcAN4AZABAJgBWaABWaoBATG4AQPIAQD4AQHCAgcQIxiwAxgnwgIKEAAYRxjWBBiwA4gGAZAGCQ&amp;amp;sclient=gws-wiz-serp#ip=1"&gt;What makes for good UX?&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When thinking about improving your user experience, think about ways your user can feel more connected with your product as a whole. Keep them engaged with your site, whether through personalized ways or with features that could help them seamlessly accomplish your product’s goals.&lt;/p&gt;

&lt;h2&gt;
  
  
  A positive User Interface (UI)
&lt;/h2&gt;

&lt;p&gt;Some more on the big guys&lt;/p&gt;

&lt;h3&gt;
  
  
  Airbnb
&lt;/h3&gt;

&lt;p&gt;Hired professional photographers for listings when they noticed the photos were lacking. Shows helpful information about listings on the search page, including photos, cost, rating, location, and others depending on the search. Features like filters and the map are intuitive and easy to look at. Minimalistic design and visually pleasing spacing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Apple
&lt;/h3&gt;

&lt;p&gt;Their developer site has an article, “&lt;a href="https://developer.apple.com/design/tips/"&gt;UI Design Dos and Don’ts&lt;/a&gt;” with a nice four-section quick reference to help with mobile development. Keeping the following four topics from their article in mind should greatly impact the UI of your final product.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For interactivity, they suggest the layout to fit the size of the screen, users won’t enjoy having to scroll horizontally to read important content. Keep an appropriate size and spacing between clickable elements. Apple suggests at least 44 by 44 points(Similar to pixels, but points are slightly larger. Pixels are about 1/96th of an inch, while points are about 1/72nd). To add to user engagement, choose elements that allow for touch gestures in addition to clicking.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6x87ygxtqhhms4tguzsj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6x87ygxtqhhms4tguzsj.png" alt="Formatting Content" width="800" height="673"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As far as readability, “Don’t let text overlap.” Add extra spacing between lines so your component doesn’t look too chaotic. To avoid having the user zooming while reading, keep text at a minimum of 11 points. Only use font colors that greatly contrast the current background. Failing at any of those three things can make it impossible for a user to even read important information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd9u62nrpb9xvqi717i3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd9u62nrpb9xvqi717i3.png" alt="Touch gestures" width="512" height="241"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On the graphics side,  your product’s images should offer higher-quality options for today’s high-resolution screens. Don’t let your images become distorted. Pictures often look very strange if their original height-by-width ratio is changed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fysi6ejpxjbmp3zkymzcz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fysi6ejpxjbmp3zkymzcz.png" alt="Clarity in controls" width="512" height="263"&gt;&lt;/a&gt;&lt;br&gt;
The above 3 photos were also included in Apple's aritcle, "&lt;a href="https://developer.apple.com/design/tips/"&gt;UI Design Dos and Don’ts&lt;/a&gt;"&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For clarity, make sure your layout has controls that are intuitive and minimalistic. Proper alignment allows users to easily tell where they are and what elements on your page are related.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Have fun designing!
&lt;/h3&gt;

&lt;p&gt;You need both for users to want to keep returning. These major companies with publicly acclaimed UI and UX have kept their user’s experience as a priority and suggest you do the same, before even starting your project.&lt;/p&gt;

&lt;h2&gt;
  
  
  Some links if you'd like to dive deeper
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://bootcamp.cvn.columbia.edu/blog/what-is-ux-design/"&gt;What is UX Design? Differences Between UX and UI Design&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.coursera.org/articles/ui-vs-ux-design"&gt;UI vs. UX Design: What’s the Difference?&lt;/a&gt; &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://bootcamp.uxdesign.cc/the-power-of-user-centered-design-a-look-at-airbnbs-design-and-user-experience-3ddfddf82c7d"&gt;The Power of User-Centered Design: A Look at Airbnb’s Design and User Experience&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://digital.gov/resources/mobile-user-experience-guidelines/"&gt;Mobile UX Guidelines&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://about.google/intl/ALL_us/our-story/"&gt;From the Garage to the Googleplex&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/design/tips/"&gt;UI Design Dos and Don’ts&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.apple.com/design/human-interface-guidelines/"&gt;Human Interface Guidelines&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://neilpatel.com/blog/measure-website-ux/"&gt;7 Ways to Measure Your Website's UX&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>design</category>
      <category>frontend</category>
    </item>
    <item>
      <title>React vs Angular</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Mon, 05 Feb 2024 03:24:11 +0000</pubDate>
      <link>https://dev.to/peytono/react-vs-angular-p5a</link>
      <guid>https://dev.to/peytono/react-vs-angular-p5a</guid>
      <description>&lt;h3&gt;
  
  
  Introduction to the React Library and Angular Framework
&lt;/h3&gt;

&lt;p&gt;When starting a new JavaScript project you should use the best tools at your disposal. It can be overwhelming how many there are to choose from! React and Angular are two very popular choices, and for good reason. Both React and Angular are all about components, helping keep code smart and reusable. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React takes advantage of JSX for its markup syntax. JSX is pretty similar to HTML, but is a little more strict. One slight difference includes, using &lt;code&gt;className&lt;/code&gt; instead of &lt;code&gt;class&lt;/code&gt;. Keep in mind, if wanting to return multiple JSX tags in a return, they must be wrapped in a shared parent wrapper, it can just be an empty wrapper. This syntax also allows you to insert JavaScript wherever you’d like, just inside some curly braces. &lt;/li&gt;
&lt;li&gt;As far as updating components state, you’ll want to use React’s &lt;code&gt;useState&lt;/code&gt;. First you’ll need to import it into your file. Like, &lt;code&gt;import { useState } from ‘react’;&lt;/code&gt; You’re free to then declare state variables, &lt;code&gt;const [followers, setFollowers] = useState(0);&lt;/code&gt; This can be used to keep track of whatever states you need, but the naming convention is &lt;code&gt;variableName&lt;/code&gt; and &lt;code&gt;setVariableName&lt;/code&gt;. Another convention is to have all components separated into their own files.
&lt;/li&gt;
&lt;/ul&gt;

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

function SayHello(){
  const [exclamation, setExclamation] = useState('!');

  function handleClick(){
    setExclamation(exclamation + '!');
  }

  return (
    &amp;lt;&amp;gt;
      &amp;lt;p&amp;gt; Hello world{exclamation} &amp;lt;/p&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;
        Get Excited!
      &amp;lt;/button&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Angular&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;While both are all about components, that’s not all Angular is about. Being that Angular is a Framework, it has many more tools in its belt. Two major ones are its own Command Line Interface(CLI) and TypeScript, which adds upon the JavaScript language especially useful in errors. The &lt;a href="https://angular.io/guide/what-is-angular"&gt;documentation&lt;/a&gt; explains it eloquently, &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;"With Angular, you’re taking advantage of a platform that can scale from single-developer projects to enterprise-level applications."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;Peeking back into conventions, Angular’s is a bit different. Components also go into their files, but naming should follow &lt;code&gt;say-hello.component.ts&lt;/code&gt;. Each component is going to need a decorator for defining a selector, the tag (based on the file name), and an HTML template, for browser rendering.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'say-hello',
  template: ` &amp;lt;p&amp;gt; Hello World! &amp;lt;/p&amp;gt; `,
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Some major upsides
&lt;/h3&gt;

&lt;p&gt;Checking out the documentation for either, you’ll find some cool tutorials to help you get started. React will help you build a Tic Tac Toe game, while Angular allows you to choose between your first web app or their Tour of Heroes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://webmobtech.com/blog/angular-vs-react"&gt;Pramesh Jain&lt;/a&gt; compared React’s and Angular’s components, &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;“React is way ahead then Angular in terms of components, features, migration, updates, and architecture. So, it does consist [of a] more speedy and brighter future than Angular.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;When it comes to performance, React uses a virtual DOM and one-way binding instead of a real DOM and two-way binding that Angular does. Giving us some faster speeds at run time. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fpq0jvxtzi1rvrqppyn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4fpq0jvxtzi1rvrqppyn.png" alt="One-and-two-way-data-bind" width="719" height="443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Image from &lt;a href="https://www.altexsoft.com/blog/react-vs-angular-compared-which-one-suits-your-project-better/"&gt;altexsoft&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;On a personal note, I am a big fan of the React documentation’s pleasing UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Angular&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Angular has many libraries for your projects, allowing for more than its base framework. If you feel so inclined, you can even publish your own. There are many &lt;a href="https://www.biz4solutions.com/blog/top-angularjs-tools-for-architecting-exemplary-web-apps"&gt;development tools&lt;/a&gt; as well.&lt;/li&gt;
&lt;li&gt;Angular comes with architecture patterns, MVVM(Model–View–ViewModel) and MVC(Model-View-Controller). While the two have some differences, they allow for the separation of business logic and data with the UI.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1awr1olgixt1e2jo1bwq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1awr1olgixt1e2jo1bwq.png" alt="model-view-controller-light-blue" width="800" height="600"&gt;&lt;/a&gt;&lt;br&gt;
Image from &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/MVC"&gt;MDN&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Some potential downsides
&lt;/h3&gt;

&lt;p&gt;Both release frequent updates, leaving projects with more maintenance to keep up with the newest updates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;React&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React is still just a library, and as &lt;a href="https://www.knowledgehut.com/blog/web-development/pros-and-cons-of-react"&gt;Bala Krishna Ragala&lt;/a&gt; said, &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;“Developers do not enjoy what they can have in a fully featured framework such as Angular”. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;As far as Angular’s previously mentioned architecture patterns, React does not have these capabilities. React is controlling the view, and for everything else, you’ll need to research which other frameworks or libraries would be right for you. This adds to potential issues.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Angular&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Many bloggers refer to Angular’s learning curve as steep, &lt;a href="https://www.linkedin.com/pulse/benefits-downsides-angular-development-biz4solutions-tovxf"&gt;Biz4Solutions&lt;/a&gt; adds, &lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;“and this is more so for newbie developers. Angular developers must be well versed in concepts like TypeScript, reactive programming (RxJS), etc., and multiple aspects like templates, components, services, modules, dependency injection, etc”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;RxJS in itself can be hard to understand with unintuitive errors. New developers might have further confusion if new to classes or components, they may have no understanding of &lt;a href="https://angular.io/guide/dependency-injection-overview"&gt;dependency injection&lt;/a&gt;. The documentation can feel lackluster to people as well. Then likely, ending up with further delays in understanding Angular. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If you aren’t a newbie, installing Angular onto your project involves a lot of boilerplate code. Not a big deal for a large project, but for a quick idea, can be a bit of a drag.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How To Choose For Your Next Project
&lt;/h3&gt;

&lt;p&gt;This isn’t a one-fits-all situation here, like most choices programmers make, it depends on your situation and goals. If you’re new to programming, with no knowledge of either, you will almost definitely have an easier time starting a new project with React. As well as a smaller project, as it involves less setup and boilerplate code. If front and back end are a priority, then Angular might be the way to go. It’ll have support for it straight out of the box. For projects that need the support of a full framework, Angular does come with lots of tools. &lt;/p&gt;

&lt;p&gt;All in all, they are both well respected in the JavaScript community and either would be a great addition to your next project! Making these decisions can be difficult, but now you should be more informed to go and start coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>A Look Into Recursion</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Mon, 18 Dec 2023 04:25:47 +0000</pubDate>
      <link>https://dev.to/peytono/a-look-into-recursion-32dg</link>
      <guid>https://dev.to/peytono/a-look-into-recursion-32dg</guid>
      <description>&lt;p&gt;Recursion is a helpful process by which we can make code call itself. Any situation in JavaScript where you would need to iterate, even if you don’t know exactly how many times, you can take advantage of the recursion method. Besides loops, there are tons of other situations we can apply recursion. In this process, you use a function to call itself until you get to the desired &lt;em&gt;case&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;One simple example to show would be finding out if a number is divisible by five.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x % 5 === 0 ? true : false;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The same above expression could be written using recursion like so...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function divisibleBy5(num){
  if(num &amp;gt; 0 &amp;amp;&amp;amp; num &amp;lt; 5){
    return false;
  } else if(num === 5) {
    return true;
  }

  return divisibleBy5(num - 5);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Base Case and Recursive Case
&lt;/h3&gt;

&lt;p&gt;A recursive function consists of two major phases, the base case and the recursive case. It should always start with a base case, which allows for the function to return and exit if certain arguments are passed in or a certain condition has been met. Similar to a loop, this part is necessary if you don’t want to crash your program (In a little bit, I’ll talk more about the call stack). &lt;/p&gt;

&lt;p&gt;I would like to re-use the above example to show you that you can have more than one base case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function divisibleBy5(num){
  // base case
  if(num &amp;gt; 0 &amp;amp;&amp;amp; num &amp;lt; 5){
    return false;
  } else if(num === 5) {
    return true;
  } else if(num &amp;lt; 0){
    return 'try again with a positive number';
  }

  // recursive case
  return divisibleBy5(num - 5);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we wanted two base cases in this divisibleBy5 function because we had two potential outcomes. You can also add additional base cases if you want specific arguments passed in when the function is called.&lt;/p&gt;

&lt;p&gt;The second major part of recursion would be the recursive case. This is where you mutate the arguments that were passed in and call the function again. It is worthwhile to note, that if you are using recursion with a collection, you probably want to use nondestructive methods so as not to destroy your data! (i.e., use method .slice() instead of .splice()) You do not have to mutate your data inside the returned function call, but often you may want to. If when making a recursive function, you’re feeling lost as to where to start, it can often be easiest to begin with the recursive case because you know this will be done every time the function calls itself. Then you can come up with an idea of what will be happening to your data on every call, which will often make it clear as to when you need to exit the function(or the base case).&lt;/p&gt;

&lt;p&gt;Here we've got another example of recursion, this time getting the sum of all the elements in an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumOfArray(array, sum = 0){
  // base case
  if(array.length === 0){
    return sum;
  }

  // recursive case
  sum += array[0];
  return sumOfArray(array.slice(1), sum);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using recursion on an array, we always want to work with the first element of the array. Also as mentioned before, we're using the slice() method so as not to destroy the array we pass into the function. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Call Stack
&lt;/h3&gt;

&lt;p&gt;The JavaScript interpreter, which translates your JavaScript so that your computer understands it, has a mechanism called the call stack. When using loops, you must include an iterator, or your whole program crashes. This concept is similar when using recursion, except now you’d get a “stack overflow” error. Every function call in JavaScript gets added to the top of the call stack, and then anytime the function return is resolved, it gets removed from the stack. The call stack only has so much memory to work with, afterwards it “overflows” and your program will crash. The call stack does have enough memory to the point where this won’t often be an issue unless you forget to update your arguments before passing them through the recursion step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumOfArray(array, sum = 0){
  // base case
  if(array.length === 0){
    return sum;
  }

  // recursive case
  sum += array[0];
  return sumOfArray(array, sum);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be an example of exceeding the call stack because an iterator was left out. Calling this function, as is, with an array of any length greater than zero will result in an error.&lt;br&gt;
&lt;code&gt;RangeError: Maximum call stack size exceeded&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  In conclusion,
&lt;/h3&gt;

&lt;p&gt;Anytime you want to iterate through recursion, just create a base case and a recursive case. Also, keep in mind that the call stack does have a limit! Sometimes recursion can even make your code more readable and even potentially cut down the necessary code, now no one has to decipher that loop! &lt;/p&gt;

</description>
      <category>learning</category>
      <category>beginners</category>
      <category>programming</category>
      <category>javascript</category>
    </item>
    <item>
      <title>A Look Into Recursion</title>
      <dc:creator>peytono</dc:creator>
      <pubDate>Mon, 18 Dec 2023 03:43:38 +0000</pubDate>
      <link>https://dev.to/peytono/a-look-into-recusion-39ml</link>
      <guid>https://dev.to/peytono/a-look-into-recusion-39ml</guid>
      <description>&lt;p&gt;Recursion is a helpful process by which we can make code call itself. Any situation in JavaScript where you would need to iterate, even if you don’t know exactly how many times, you can take advantage of the recursion method. Besides loops, there are tons of other situations we can apply recursion. In this process, you use a function to call itself until you get to the desired &lt;em&gt;case&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;One simple example to show would be finding out if a number is divisible by five.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;x % 5 === 0 ? true : false;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The same above expression could be written using recursion like so...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function divisibleBy5(num){
  if(num &amp;gt; 0 &amp;amp;&amp;amp; num &amp;lt; 5){
    return false;
  } else if(num === 5) {
    return true;
  }

  return divisibleBy5(num - 5);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Base Case and Recursive Case
&lt;/h3&gt;

&lt;p&gt;A recursive function consists of two major phases, the base case and the recursive case. It should always start with a base case, which allows for the function to return and exit if certain arguments are passed in or a certain condition has been met. Similar to a loop, this part is necessary if you don’t want to crash your program (In a little bit, I’ll talk more about the call stack). &lt;/p&gt;

&lt;p&gt;I would like to re-use the above example to show you that you can have more than one base case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function divisibleBy5(num){
  // base case
  if(num &amp;gt; 0 &amp;amp;&amp;amp; num &amp;lt; 5){
    return false;
  } else if(num === 5) {
    return true;
  } else if(num &amp;lt; 0){
    return 'try again with a positive number';
  }

  // recursive case
  return divisibleBy5(num - 5);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Before we wanted two base cases in this divisibleBy5 function because we had two potential outcomes. You can also add additional base cases if you want specific arguments passed in when the function is called.&lt;/p&gt;

&lt;p&gt;The second major part of recursion would be the recursive case. This is where you mutate the arguments that were passed in and call the function again. It is worthwhile to note, that if you are using recursion with a collection, you probably want to use nondestructive methods so as not to destroy your data! (i.e., use method .slice() instead of .splice()) You do not have to mutate your data inside the returned function call, but often you may want to. If when making a recursive function, you’re feeling lost as to where to start, it can often be easiest to begin with the recursive case because you know this will be done every time the function calls itself. Then you can come up with an idea of what will be happening to your data on every call, which will often make it clear as to when you need to exit the function(or the base case).&lt;/p&gt;

&lt;p&gt;Here we've got another example of recursion, this time getting the sum of all the elements in an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumOfArray(array, sum = 0){
  // base case
  if(array.length === 0){
    return sum;
  }

  // recursive case
  sum += array[0];
  return sumOfArray(array.slice(1), sum);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using recursion on an array, we always want to work with the first element of the array. Also as mentioned before, we're using the slice() method so as not to destroy the array we pass into the function. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Call Stack
&lt;/h3&gt;

&lt;p&gt;The JavaScript interpreter, which translates your JavaScript so that your computer understands it, has a mechanism called the call stack. When using loops, you must include an iterator, or your whole program crashes. This concept is similar when using recursion, except now you’d get a “stack overflow” error. Every function call in JavaScript gets added to the top of the call stack, and then anytime the function return is resolved, it gets removed from the stack. The call stack only has so much memory to work with, afterwards it “overflows” and your program will crash. The call stack does have enough memory to the point where this won’t often be an issue unless you forget to update your arguments before passing them through the recursion step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function sumOfArray(array, sum = 0){
  // base case
  if(array.length === 0){
    return sum;
  }

  // recursive case
  sum += array[0];
  return sumOfArray(array, sum);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would be an example of exceeding the call stack because an iterator was left out. Calling this function, as is, with an array of any length greater than zero will result in an error.&lt;br&gt;
&lt;code&gt;RangeError: Maximum call stack size exceeded&lt;/code&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  In conclusion,
&lt;/h3&gt;

&lt;p&gt;Anytime you want to iterate through recursion, just create a base case and a recursive case. Also, keep in mind that the call stack does have a limit! Sometimes recursion can even make your code more readable and even potentially cut down the necessary code, now no one has to decipher that loop! &lt;/p&gt;

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