<?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: AndrewCabana</title>
    <description>The latest articles on DEV Community by AndrewCabana (@andrewcabana).</description>
    <link>https://dev.to/andrewcabana</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1075752%2F40f99b31-a2e4-4eb7-abd5-7568643e84e1.png</url>
      <title>DEV Community: AndrewCabana</title>
      <link>https://dev.to/andrewcabana</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andrewcabana"/>
    <language>en</language>
    <item>
      <title>What is the UseEffect Hook and How to use it properly</title>
      <dc:creator>AndrewCabana</dc:creator>
      <pubDate>Sat, 17 Jun 2023 01:59:24 +0000</pubDate>
      <link>https://dev.to/andrewcabana/what-is-the-useeffect-hook-and-how-to-use-it-properly-4mna</link>
      <guid>https://dev.to/andrewcabana/what-is-the-useeffect-hook-and-how-to-use-it-properly-4mna</guid>
      <description>&lt;p&gt;In React, the useEffect hook has become a powerful tool that allows developers to perform side effects in functional components. useEffect is usefull in many scenarios whether you need to make a call to an API, subscribe to events, or clean up resources, useEffect provides a clean and concise way to handle these events.  this post is aimed to explain what the useEffect hook is and how to use it effectively in your React apps.&lt;/p&gt;

&lt;p&gt;Understanding useEffect:&lt;br&gt;
The useEffect hook is a built-in function in React that allows you to perform side effects after rendering a component. Side effects include things like data fetching, subscriptions, or manually changing the DOM. useEffect is called after every render by default, but you can control when useEffect is called using dependencies.&lt;/p&gt;

&lt;p&gt;Using useEffect:&lt;br&gt;
To start using the useEffect hook, you first need to import it from the 'react' library:&lt;/p&gt;

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

&lt;p&gt;The basic syntax of the useEffect hook looks like this:&lt;/p&gt;

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

&lt;p&gt;The first argument to useEffect is the function that will be executed as a side effect. The optional second argument, 'dependencies,' is an array of values. If any of the values in the dependency array change, the effect will re-run. If you want the effect to run only once, pass an empty dependency array.&lt;/p&gt;

&lt;p&gt;Example 1: Fetching data with useEffect&lt;/p&gt;

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

&lt;p&gt;In this example, we use the useEffect hook to fetch data from an API. The effect is triggered only once when the component mounts (empty dependency array). It sets the fetched data to the 'data' state using the 'setData' function.&lt;/p&gt;

&lt;p&gt;Example 2: Subscribing to events with useEffect&lt;/p&gt;

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

&lt;p&gt;In this example, the useEffect hook is used to subscribe to the scroll event. The effect runs once when the component mounts, adds an event listener for scroll events, and removes it when the component unmounts (cleanup code).&lt;/p&gt;

&lt;p&gt;Conclusion:&lt;br&gt;
The useEffect hook is a fundamental tool in React for handling side effects. By understanding this hook and using the provided examples, you can leverage this hooks fuctionality to fetch data, subscribe to events, and perform other necessary operations in your functional components. Remember to use the dependency array wisely to control when the useEffect hook should run. With useEffect, you can write cleaner, more concise code in your React applications.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Exploring JavaScript Fetch API</title>
      <dc:creator>AndrewCabana</dc:creator>
      <pubDate>Fri, 05 May 2023 19:27:18 +0000</pubDate>
      <link>https://dev.to/andrewcabana/using-javascript-fetch-api-for-get-and-post-request-2jb5</link>
      <guid>https://dev.to/andrewcabana/using-javascript-fetch-api-for-get-and-post-request-2jb5</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is Fetch?&lt;/strong&gt;  JavaScripts fetch API is powerful yet simple promised based interace that allows web browers to make HTTP request to servers. Fetch request are similar to XMLHTTPrequest, however with the use of promises Fetch request offers a simpler solution that avoids using callbacks. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sending a request&lt;/strong&gt;&lt;br&gt;
Below is a basic example of the fetch() method &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;  fetch('example url')&lt;/li&gt;
&lt;li&gt;   .then(response =&amp;gt; {&lt;/li&gt;
&lt;li&gt;   // server response
&lt;/li&gt;
&lt;li&gt;   console.log(response);&lt;/li&gt;
&lt;li&gt;   })&lt;/li&gt;
&lt;li&gt;  .then(data =&amp;gt; {&lt;/li&gt;
&lt;li&gt;    // data from url &lt;/li&gt;
&lt;li&gt;    console.log(data);&lt;/li&gt;
&lt;li&gt;  })&lt;/li&gt;
&lt;li&gt;  .catch(error =&amp;gt; {&lt;/li&gt;
&lt;li&gt;    //error handling&lt;/li&gt;
&lt;li&gt;  });&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This code uses the fetch method to make a network request to an example URL. It then uses the .then method to handle the response returned from the server and log it to the console.&lt;/p&gt;

&lt;p&gt;Afterwards, it uses another .then method to handle the data returned from the previous step and log it to the console.&lt;/p&gt;

&lt;p&gt;Lastly, it uses the catch method to handle any errors that may occur during the fetch request and log them to the console.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Init Options&lt;/strong&gt;&lt;br&gt;
the fetch() method has two paramaters that can be passed.The path to the resource is the first parameter and is required for the method to work properly. A second optinonal init paramater is used for futher options of the fetch() method. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;const initOptions = {&lt;/li&gt;
&lt;li&gt;method: "POST",&lt;/li&gt;
&lt;li&gt;headers: { 'content-Type' : 'application/json },&lt;/li&gt;
&lt;li&gt;body: JSON.stringify({ title: 'Example Fetch POST})&lt;/li&gt;
&lt;li&gt;};&lt;/li&gt;
&lt;li&gt;
&lt;/li&gt;
&lt;li&gt;const response = fetch('example URL', initOptions);&lt;/li&gt;
&lt;li&gt;const data = response.json();&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;in the code example above we are saving the options of our request to a const variable initOptions and passing this variable as the second paramater in our request. When creating request options there is plenlty of options to adjust a few are listed below  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Method - sets request method to GET or Post.&lt;/li&gt;
&lt;li&gt;Headers - add headers to the request.&lt;/li&gt;
&lt;li&gt;Mode - request mode (cors, no-cors, or same-origin).&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Cache - string that determines how a request will interact with a browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Credentals - what browsers do with credentials.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additional fetch() init options and how to use them can be found here&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/fetch"&gt;https://developer.mozilla.org/en-US/docs/Web/API/fetch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Response Error Handling&lt;/strong&gt;&lt;br&gt;
Errors with HTTP request are handled with the response object properties. Response.ok and Response.status are two propeties returned that are usufal in error handling. Response.ok represents a boolean value to represent a successful response. Response.status will display HTTP codes 200 for success and a 404 code if no resource was found. Heres a look at a response object from &lt;a href="http://www.boredapi.com"&gt;www.boredapi.com&lt;/a&gt;. Response.ok is true and Response.status is 200 showing a successful request &lt;/p&gt;

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

&lt;p&gt;JavaScript fetch API is an excellent tool for handling HTTP requests, and its simplicity makes it easy to use. By using fetch, we can fetch data from the server, send data to the server, and handle errors with ease.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
  </channel>
</rss>
