<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Eze Emmanuel]]></title><description><![CDATA[Eze Emmanuel]]></description><link>https://emmanuel-eze.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Sun, 20 Sep 2026 02:44:35 GMT</lastBuildDate><atom:link href="https://emmanuel-eze.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Understanding the useFetch and useAxios Hook in React]]></title><description><![CDATA[The useFetch and useAxios hooks are custom hooks that simplify making HTTP requests in React components. It takes away the repetitive tasks involved in making API calls, handling loading states, and error handling thereby reducing the amount of code ...]]></description><link>https://emmanuel-eze.hashnode.dev/understanding-the-usefetch-and-useaxios-hook-in-react</link><guid isPermaLink="true">https://emmanuel-eze.hashnode.dev/understanding-the-usefetch-and-useaxios-hook-in-react</guid><category><![CDATA[custom-hooks]]></category><category><![CDATA[React]]></category><dc:creator><![CDATA[Eze Emmanuel]]></dc:creator><pubDate>Sat, 27 May 2023 00:03:52 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1685147453395/d8388feb-ecd9-45bc-b1f1-2593b6ccfd57.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The useFetch and useAxios hooks are custom hooks that simplify making HTTP requests in React components. It takes away the repetitive tasks involved in making API calls, handling loading states, and error handling thereby reducing the amount of code you need to write, using these custom hooks promotes cleaner and more organized code.</p>
<p>In this article, I will be guiding you to understand and properly use these custom hooks in your next project. Enough talking, right? Let's jump straight into it.</p>
<h3 id="heading-what-is-a-custom-hook">What is a custom hook?</h3>
<p>Before you start using the useFetch and useAxios custom hooks, it's important to understand what a custom hook is.</p>
<p>Custom hooks in React are a way to reuse and share logic across different components. They allow you to extract common functionality into a separate function, which can be used by multiple components in your application. A custom hook is simply a JavaScript function whose name starts with "use" (a naming convention in React) and follows the rules of hooks.</p>
<h3 id="heading-api-call-using-axios">API call using Axios</h3>
<p>Axios is a popular JavaScript library used for making HTTP requests. API calls can be made using either promises or async/await to handle asynchronous operations. However, in this article, we will be utilizing async/await. we would also be using <a target="_blank" href="https://jsonplaceholder.typicode.com/">jsonplaceholder’s</a> posts API.</p>
<p>Let's make a get API call to get the list of posts.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">'axios'</span>;

axios.defaults.baseURL = <span class="hljs-string">'https://jsonplaceholder.typicode.com'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> getPosts = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.get(<span class="hljs-string">'/posts'</span>);
      <span class="hljs-built_in">console</span>.log(response.data)
    } <span class="hljs-keyword">catch</span>(err) {
        <span class="hljs-built_in">console</span>.log(err)
    }
  }

  useEffect(<span class="hljs-function">() =&gt;</span> {
    getPosts();
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'app'</span>&gt;</span>
      Hello World
    <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<p>From the above example, we created a baseURL so that we can simply pass the specific path to the Axios method. We utilized <code>Axios.get()</code> to make the API call, and by using async/await within a try-catch block, we can obtain the result or handle any errors that may occur.</p>
<h3 id="heading-adding-states-to-the-api-call">Adding states to the API call</h3>
<p>Let's add states to save our response and error if any, as well as a loading state for a better user experience and improved control and management of the API call lifecycle.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, {useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">'axios'</span>;

axios.defaults.baseURL = <span class="hljs-string">'https://jsonplaceholder.typicode.com'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">true</span>);
  <span class="hljs-keyword">const</span> [error, setError] = useState(<span class="hljs-literal">null</span>);

  <span class="hljs-keyword">const</span> getPosts = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.get(<span class="hljs-string">'/posts'</span>);
      setData(response.data);
      setLoading(<span class="hljs-literal">false</span>);
    } <span class="hljs-keyword">catch</span>(err) {
        setError(err);
        setLoading(<span class="hljs-literal">false</span>);
    }
  }

  useEffect(<span class="hljs-function">() =&gt;</span> {
    getPosts();
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'app'</span>&gt;</span>
      Hello World
    <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<h3 id="heading-creating-a-useaxios-custom-hook">Creating a useAxios custom hook</h3>
<p>I believe by now you already know what a custom hook is since we've discussed it earlier. However, if you want to learn more, you can visit <a target="_blank" href="https://react.dev/learn/reusing-logic-with-custom-hooks">here</a>. Now, let's dive straight into the code snippet.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, {useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">'axios'</span>;

axios.defaults.baseURL = <span class="hljs-string">'https://jsonplaceholder.typicode.com'</span>;

<span class="hljs-keyword">const</span> useAxios = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">true</span>);
  <span class="hljs-keyword">const</span> [error, setError] = useState(<span class="hljs-literal">null</span>);

  <span class="hljs-keyword">const</span> getPosts = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.get(<span class="hljs-string">'/posts'</span>);
      setData(response.data);
      setLoading(<span class="hljs-literal">false</span>);
    } <span class="hljs-keyword">catch</span>(err) {
        setError(err);
        setLoading(<span class="hljs-literal">false</span>);
    }
  }

  useEffect(<span class="hljs-function">() =&gt;</span> {
    getPosts();
  }, []);

  <span class="hljs-comment">// custom hook returns value</span>
  <span class="hljs-keyword">return</span> { data, error, loading };
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> useAxios
</code></pre>
<p>The code looks similar to the previous example, but now we have created a custom hook named useAxios that returns three values: data, error and loading.</p>
<p>We can make our custom hook dynamic by passing parameters as props to the hook. In our example, we can pass a variable representing the URL path.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, {useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> axios <span class="hljs-keyword">from</span> <span class="hljs-string">'axios'</span>;

axios.defaults.baseURL = <span class="hljs-string">'https://jsonplaceholder.typicode.com'</span>;

<span class="hljs-keyword">const</span> useAxios = <span class="hljs-function">(<span class="hljs-params">requestParam</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">true</span>);
  <span class="hljs-keyword">const</span> [error, setError] = useState(<span class="hljs-literal">null</span>);

  <span class="hljs-keyword">const</span> getPosts = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> axios.get(<span class="hljs-string">`/<span class="hljs-subst">${requestParam}</span>`</span>);
      setData(response.data);
      setLoading(<span class="hljs-literal">false</span>);
    } <span class="hljs-keyword">catch</span>(err) {
        setError(err);
        setLoading(<span class="hljs-literal">false</span>);
    }
  }

  useEffect(<span class="hljs-function">() =&gt;</span> {
    getPosts();
  }, [requestParam]);

  <span class="hljs-keyword">return</span> { data, error, loading };
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> useAxios
</code></pre>
<h3 id="heading-creating-a-usefetch-custom-hook">Creating a useFetch custom hook</h3>
<p>Let's repeat what we did previously, but this time we will be making our API call using the Fetch API.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, {useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-keyword">const</span> useAxios = <span class="hljs-function">(<span class="hljs-params">requestParam</span>) =&gt;</span> {
  <span class="hljs-keyword">const</span> url = <span class="hljs-string">`https://jsonplaceholder.typicode.com/<span class="hljs-subst">${requestParam}</span>`</span>

  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">true</span>);
  <span class="hljs-keyword">const</span> [error, setError] = useState(<span class="hljs-literal">null</span>);

  <span class="hljs-keyword">const</span> getPosts = <span class="hljs-keyword">async</span> () =&gt; {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(url)
      <span class="hljs-keyword">const</span> res = <span class="hljs-keyword">await</span> response.json()
      setData(res.data);
      setLoading(<span class="hljs-literal">false</span>);
    } <span class="hljs-keyword">catch</span>(err) {
        setError(err);
        setLoading(<span class="hljs-literal">false</span>);
    }
  }

  useEffect(<span class="hljs-function">() =&gt;</span> {
    getPosts();
  }, [requestParam]);

  <span class="hljs-keyword">return</span> { data, error, loading };
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> useAxios
</code></pre>
<h3 id="heading-using-the-useaxios-and-usefetch-hooks-in-various-component">Using the useAxios and useFetch hooks in various component</h3>
<p>Now that we have created these hooks, let me show you how you can use them in your different components.</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> { data, loading, error } = useAxios(<span class="hljs-string">"posts"</span>);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">className</span>=<span class="hljs-string">'app'</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Posts<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      {loading &amp;&amp; <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>loading...<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>}
      {error &amp;&amp; <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>{error.message}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>}
      {data &amp;&amp; data.map((posts, index) =&gt; <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>&gt;</span>{posts.title}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>)}
    <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span></span>
  )
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App
</code></pre>
<pre><code class="lang-javascript"><span class="hljs-comment">//when using the useFetch hook</span>
<span class="hljs-keyword">const</span> { data, loading, error } = useFetch(<span class="hljs-string">"posts"</span>);
</code></pre>
<p>In conclusion, the useFetch and useAxios hooks provide convenient and efficient ways to handle API calls in React applications.</p>
<p>I hope you found this article useful and thank you for reading.</p>
]]></content:encoded></item><item><title><![CDATA[Beginners Guide to  Coding]]></title><description><![CDATA[The Word coding can be intimidating and sometimes mysterious, especially to individuals thinking of learning to code. Coding is important to all individuals as it teaches skills such as critical thinking, problem solving, persistence and creativity. ...]]></description><link>https://emmanuel-eze.hashnode.dev/beginners-guide-to-coding</link><guid isPermaLink="true">https://emmanuel-eze.hashnode.dev/beginners-guide-to-coding</guid><category><![CDATA[General Programming]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Eze Emmanuel]]></dc:creator><pubDate>Tue, 25 Oct 2022 15:08:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1666706934172/iN4xezJLm.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>The Word coding can be intimidating and sometimes mysterious, especially to individuals thinking of learning to code. Coding is important to all individuals as it teaches skills such as critical thinking, problem solving, persistence and creativity. Definitely, just like in every aspect of life, coding also comes with some of its challenges such as frustration and so on. In this article I will be sharing some of the tips that helped me(and still helping me) when learning and becoming a better JavaScript programmer.</p>
<ol>
<li><p>Stop trying to memorize codes:  Memorization is a terrible technique when it comes to coding or programming, in programming there are lots and lots of methods, functions and names of the things to memorize so trying to memorize them all will be a wrong approach and also an impossible one. Instead the right approach will be focusing on the concept of programming, for instance in JavaScript you can understand how variables work, how objects work, how functions work, and event loops.  After really understanding these concepts then you will realize you've covered almost all to know in JavaScript. In JavaScript for instance there are tons of built in functions like split, join and so on, instead of memorizing these functions you can focus on how these functions work and the concept of functions as a whole, when you forget a built in function like a function for joining an array you can always google to get the right function required. One thing to realize as a newbie programmer is that its okay to forget sometimes, functions and methods that you use often will eventually stick and the ones you don't use often you don't need to memorize them because you can always look them up online.</p>
</li>
<li><p>Build projects, in as much as watching tutorials is one of the most easiest ways to learn to code it is important to know that you need not to follow tutorials as much as you may think. Don't spend loads of time watching tons of tutorials without taking what you've learnt so far to start building your own projects. Building projects is a good habit to practice what you've learnt and also improve yourself, building projects can not only improve your programming skills but also your creativity, time management, problem solving and it also helps with your confidence. When you don't put what you've learnt into practice by building projects you end up forgetting them. As a programmer you need to enjoy problem solving and building things.
Learning to code is a continuous process for a software developer, from my discovery I found out that even senior software developers sometimes still makes use of google and do researches when dealing with a task or problem, the truth is if you hate learning then you will hate programming.</p>
</li>
<li><p>As a new programmer you need to be persistent as you will sometimes run into bugs that may be frustrating and require lots of time to fix, but it is important to note that you are never going to be a good programmer, if you get stuck, search for the answer for like 5 minute and then give up.</p>
</li>
</ol>
<p>I believe you found this useful</p>
<p>Emmatechy.</p>
]]></content:encoded></item></channel></rss>