<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: adams mercy</title>
    <description>The latest articles on Forem by adams mercy (@m_adams1909).</description>
    <link>https://forem.com/m_adams1909</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%2F635868%2F47befce0-e42f-41e4-b39b-c410bf5063d7.JPG</url>
      <title>Forem: adams mercy</title>
      <link>https://forem.com/m_adams1909</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/m_adams1909"/>
    <language>en</language>
    <item>
      <title>useState or useReducer – which to choose for your application</title>
      <dc:creator>adams mercy</dc:creator>
      <pubDate>Tue, 04 Apr 2023 03:20:44 +0000</pubDate>
      <link>https://forem.com/m_adams1909/usestate-or-usereducer-which-to-choose-for-your-application-ia5</link>
      <guid>https://forem.com/m_adams1909/usestate-or-usereducer-which-to-choose-for-your-application-ia5</guid>
      <description>&lt;p&gt;&lt;code&gt;useReducer&lt;/code&gt; helps us manage state in a complex application. What exactly is &lt;code&gt;useReducer&lt;/code&gt;? Is it an alternative to &lt;code&gt;useState&lt;/code&gt;? &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useReducer&lt;/code&gt; are both used to handle state logic. It is thereby necessary to understand when to use these hooks. &lt;code&gt;useReducer&lt;/code&gt; is not replacing &lt;code&gt;useState&lt;/code&gt; in any way but it would be more efficient to use &lt;code&gt;useReducer&lt;/code&gt; in some complex applications.&lt;/p&gt;

&lt;p&gt;We are going to explore these hooks in detail showing the best hook to use in any given application and how we can convert one of these hooks to use the other. Let’s get started.&lt;/p&gt;

&lt;p&gt;The commonly used state hook – &lt;code&gt;useState&lt;/code&gt;, is a react hook that lets you add a state variable to your component. It is usually written like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const [state, setState] = useState(initialState)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; takes in an initialState which can be a string, boolean, number, array, object or function. It returns two values namely:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;state – This is the current state&lt;/li&gt;
&lt;li&gt;setState – This is a function that updates the state.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A simple example looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [country, setCountry] = useState('Nigeria');
const handleClick = () =&amp;gt; {
    setCountry('South Africa');
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above code snippet is a simple example of the &lt;code&gt;useState&lt;/code&gt; hook and how we can use it to update the state. If the &lt;code&gt;handleClick&lt;/code&gt; function is passed to a button, on clicking the button, the application re-renders to show the update – “South Africa” for the country state variable.&lt;/p&gt;

&lt;h3&gt;
  
  
  What exactly is useReducer?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;useReducer&lt;/code&gt; is a React hook that lets you add a reducer to your component. So, what is a reducer? A reducer is a function that allows us to specify all the state update logic in a single function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useReducer&lt;/code&gt; takes in two major parameters and an optional third parameter. It returns two values – state and dispatch. The state is the current state and the dispatch is a function that lets you update the state.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const [state, dispatch] = useReducer(reducer, initialState, initFunc?)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Same simple example with &lt;code&gt;useReducer&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const reducer = (state, action) =&amp;gt; {
    switch (action.type) {
        case 'change_country': {
            return {
                country: action.newCountry
            }
        }
        // other cases are written here
        default: {
            return state
        }
    }
}

const [state, dispatch] = useReducer(reducer, {country: 'Nigeria'});

const handleClick = () =&amp;gt; {
dispatch({
    type: 'change_country',
    newCountry: 'South Africa'
})
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An explanation of the above sample code snippet – we invoked our &lt;code&gt;useReducer&lt;/code&gt; which accepts a reducer that contains the logic for updating the state and an initial state of &lt;code&gt;{ country: ‘Nigeria’ }&lt;/code&gt;. We are calling the dispatch function in the click handler; passing in the type of action we want to perform with the value of the next state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Dispatch and Reducer Explained
&lt;/h3&gt;

&lt;p&gt;Dispatch Function: The dispatch function returned by &lt;code&gt;useReducer&lt;/code&gt; lets you update the state to a different value and trigger a re-render. You need to pass the action as the only argument to the dispatch function. An action is usually an object with a type property identifying it and, optionally, other properties with additional information.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dispatch({
    type: ‘change_country’,
    // other properties
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A reducer function is declared like the below code snippet. It accepts state, which is the current state and action which updates the state and returns the next state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reducer(state, action) {
    // state updates are made here
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have successfully updated a state using both &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useReducer&lt;/code&gt;. So, of what use is one hook over the other? We will cover this next in this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparing useState and useReducer
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;useReducer&lt;/code&gt; is similar to &lt;code&gt;useState&lt;/code&gt;. &lt;code&gt;useReducer&lt;/code&gt; enables us to move our state update logic into a single function which is more efficient for larger applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;useState&lt;/code&gt; is simple and easy to set up for smaller applications but when the application gets complex, it becomes difficult to read with many state logic in event handlers. &lt;code&gt;useReducer&lt;/code&gt; proves to be efficient for this use case as it helps organize our state logic in one place.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;useReducer&lt;/code&gt; is easier to debug in a complex application as you can easily find the action that is not dispatched, but with &lt;code&gt;useState&lt;/code&gt;, you would have to look through a lengthy code to identify the error.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Examining the above code sample, using &lt;code&gt;useState&lt;/code&gt; resulted in fewer lines of code. While this is good for smaller applications when the application gets larger, the lines of code also increase. Using &lt;code&gt;useReducer&lt;/code&gt; to separate the logic in a function is a better approach for larger applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If your application is handling multiple state logic in event handlers, then you should use &lt;code&gt;useReducer&lt;/code&gt; else &lt;code&gt;useState&lt;/code&gt; is fine.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to migrate from useState to useReducer.
&lt;/h3&gt;

&lt;p&gt;We are going to explore an example that uses both &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useReducer&lt;/code&gt;. You will, therefore, see how we can migrate from one to the other. Here is a simple application that gets the user’s &lt;code&gt;first_name&lt;/code&gt; and &lt;code&gt;last_name&lt;/code&gt; with a button to add the names to the existing name list and a button to reset the name list.&lt;/p&gt;

&lt;p&gt;Steps to follow:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Move from setting state to dispatching actions.&lt;/li&gt;
&lt;li&gt;Write a reducer function.&lt;/li&gt;
&lt;li&gt;Use the reducer from your component.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This contains the &lt;a href="https://codesandbox.io/s/usestate-and-usereducer-hook-example-9lmkm9?file=/src/App.js"&gt;code &lt;/a&gt;sample of our application. To see the implementation of our state update logic using &lt;code&gt;useState&lt;/code&gt;, comment out the FormUsingReducer component in App.js and uncomment the Form component in App.js and vice-versa for &lt;code&gt;useReducer&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We have explored both &lt;code&gt;useState&lt;/code&gt; and &lt;code&gt;useReducer&lt;/code&gt; to update state. We also looked at a real live example, when it is preferable to use these hooks and lastly a quick comparison of both. A lot of references are from the React documentation. Check it out &lt;a href="https://react.dev/reference/react"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Understanding the concepts we use and exploring new concepts is critical for growth as developers. I hope you have learned something and you are excited about applying what you have learnt in your next project.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthooks</category>
      <category>javascript</category>
      <category>frontend</category>
    </item>
    <item>
      <title>useEffect and useLayoutEffect: The Differences and Use Cases</title>
      <dc:creator>adams mercy</dc:creator>
      <pubDate>Tue, 14 Mar 2023 10:33:32 +0000</pubDate>
      <link>https://forem.com/m_adams1909/useeffect-and-uselayouteffect-the-differences-and-use-cases-3pcc</link>
      <guid>https://forem.com/m_adams1909/useeffect-and-uselayouteffect-the-differences-and-use-cases-3pcc</guid>
      <description>&lt;p&gt;A lot of developers are conversant with useEffect and how to use it but when asked about useLayoutEffect hooks and its use case, we become blank. I couldn’t spot the difference also as useLayoutEffect hooks are used in rare cases. I decided to check out the documentation to spot the difference.&lt;/p&gt;

&lt;p&gt;This article is focused on explaining the two hooks, their differences and their use cases. The pre-requisite for this article is to be comfortable with basic React principles.&lt;/p&gt;

&lt;p&gt;As developers, it is necessary we understand the technologies, tools, and languages we use to a large extent. This helps us apply the right principles to our project which in turn, would facilitate a better and more efficient application.&lt;/p&gt;

&lt;p&gt;This article covers the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;What is useEffect hook?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;What is useLayoutEffect hook?&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useEffect and useLayoutEffect - The Differences&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useEffect and useLayoutEffect - Example&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  What is useEffect hook?
&lt;/h3&gt;

&lt;p&gt;According to the React documentation, useEffect is a React Hook that lets you synchronize a component with an external system.&lt;/p&gt;

&lt;p&gt;Syntax: useEffect (setup, dependencies?)&lt;/p&gt;

&lt;p&gt;The setup in the above syntax represents the function with your effect’s logic. The setup function may also optionally return a clean-up function. &lt;strong&gt;When&lt;/strong&gt; the components are first added or painted to the DOM, React will run the setup function and after every re-render with changed dependencies, React will first run the clean-up function (if provided) with the old values and then run the setup function again with the new values. &lt;strong&gt;After&lt;/strong&gt; the component is removed from the DOM, React will run the clean-up function one last time.&lt;/p&gt;

&lt;p&gt;Dependencies (optional): This has three forms; empty dependency array, no dependency provided and reactive value dependency provided. If no dependency is provided, React will run the useEffect function upon every DOM change. If an empty dependency array is provided, React will run the useEffect after only the first render i.e. this runs just once. If values are provided in the dependency, React with run useEffect if any of these values changes.&lt;/p&gt;

&lt;p&gt;useEffect can be used in the following scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Connecting to an external system&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Fetching data with effects&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Controlling a non-react widget, etc.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Check out the official documentation for more detailed explanation.&lt;/p&gt;

&lt;p&gt;A sample of useEffect Hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
        // do something ...
        return () =&amp;gt; {
            // clean up happens here
        }
    }, []) // dependencies are added here in the array

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  What is useLayoutEffect Hook?
&lt;/h3&gt;

&lt;p&gt;useLayoutEffect is a version of useEffect that fires before the browser repaints the screen.&lt;/p&gt;

&lt;p&gt;Syntax: useLayoutEffect (setup, dependencies?)&lt;/p&gt;

&lt;p&gt;The setup in the above syntax represents the function with your effect’s logic. The setup function may also optionally return a clean-up function. &lt;strong&gt;Before&lt;/strong&gt; the components are first added or painted to the DOM, React will run the setup function and after every re-render with changed dependencies, React will first run the clean-up function (if provided) with the old values and then run the setup function again with the new values. &lt;strong&gt;Before&lt;/strong&gt; the component is removed from the DOM, React will run the clean-up function one last time.&lt;/p&gt;

&lt;p&gt;Dependencies (optional): This is the same as the above – reference to useEffect Hook.&lt;/p&gt;

&lt;h3&gt;
  
  
  useEffect and useLayoutEffect - The Differences
&lt;/h3&gt;

&lt;p&gt;One major difference between useEffect and useLayoutEffect is that useEffect runs after the browser has been painted i.e. the users must have seen the application before the code in useEffect runs while useLayoutEffect runs or blocks the browser from painting ensuring the code inside the useLayoutEffect runs before the browser displays or makes the application visible to the users.&lt;/p&gt;

&lt;p&gt;useLayoutEffect affects the performance of the application as the browser is blocked until the computation is done by useLayoutEffect. useEffect does not block the browser.&lt;/p&gt;

&lt;p&gt;It is advised to avoid this as much as possible except in rare cases where the application is dependent on a specific computation.&lt;/p&gt;

&lt;h3&gt;
  
  
  useEffect and useLayoutEffect - Example
&lt;/h3&gt;

&lt;p&gt;Below is a simple useEffect and useLayoutEffect function that increments &lt;code&gt;count&lt;/code&gt; and &lt;code&gt;num&lt;/code&gt; by 1 respectively. The below code illustrates the difference. When the code below is compiled, the &lt;code&gt;count&lt;/code&gt; displays 0 and then changes to 1 within a second while &lt;code&gt;num&lt;/code&gt; displays 1 as soon as the code is rendered to the screen. We can observe a flick with the &lt;code&gt;count&lt;/code&gt;. This behaviour could confuse the users when this happens in a more complex application. To prevent the behaviour, useLayoutEffect becomes useful. When we need to compute some measurement before rendering our application, then we can use useLayoutEffect otherwise we can use the useEffect hook.&lt;br&gt;
&lt;/p&gt;

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

const HooksUseCases = () =&amp;gt; {
    const [count, setCount] = useState(0)
    const [num, setNum] = useState(0)

    useEffect(() =&amp;gt; {
        console.log('excuting useEffect setup...')
        setCount(c =&amp;gt; c + 1)
        return () =&amp;gt; {
            console.log('running useEffect cleanup here..')
            setCount(0)
        }
    }, [])

    useLayoutEffect(() =&amp;gt; {
        console.log('excuting useLayoutEffect setup...')
        setNum(n =&amp;gt; n + 1)
        return () =&amp;gt; {
            console.log('running useLayoutEffect cleanup here..')
            setNum(0)
        }
    }, [])

    return (
        &amp;lt;&amp;gt;
            &amp;lt;p&amp;gt;useEffect Count {count}&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;useLayoutEffect Num {num}&amp;lt;/p&amp;gt;
        &amp;lt;/&amp;gt;
    )
}

export default HooksUseCases
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--eU4qSRKs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8hnbv3b5nhcb6byyt53.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--eU4qSRKs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d8hnbv3b5nhcb6byyt53.png" alt="useEffect vs useLayoutEffect" width="880" height="387"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XqIffhH7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jxoic8yxr3oy68otsx6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XqIffhH7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1jxoic8yxr3oy68otsx6.png" alt="useEffect vs useLayoutEffect" width="880" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above screenshots, we can see in the console that useLayoutEffect runs before useEffect. Here is a quick illustration of how the above code works when it is compiled:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;count&lt;/code&gt; and &lt;code&gt;num&lt;/code&gt; renders with their initial value of 0 respectively&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React places it in the DOM and runs the code in the useLayoutEffect&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;useLayoutEffect code updates the &lt;code&gt;num&lt;/code&gt; value by 1 and triggers a re-render. The value of &lt;code&gt;count&lt;/code&gt; still remains 0 while &lt;code&gt;num&lt;/code&gt; has incremented by 1 giving it the value of 1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React updates the DOM and the browser finally shows the update of &lt;code&gt;count&lt;/code&gt; = 0, &lt;code&gt;num&lt;/code&gt; = 1&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After the browser has been painted, the useEffect code runs, incrementing &lt;code&gt;count&lt;/code&gt; by 1 and trigger a re-render to update the DOM, &lt;code&gt;count&lt;/code&gt; = 1 and &lt;code&gt;num&lt;/code&gt; = 1. We can observe a quick change of &lt;code&gt;count&lt;/code&gt; changing from 0 to 1 within milliseconds.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React updates the DOM again and the browser finally shows the update of &lt;code&gt;count&lt;/code&gt; = 1, &lt;code&gt;num&lt;/code&gt; = 1&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, we covered what useEffect and useLayoutEffect is and a sample of each hook. We also had a look at the difference and best use case for each of the hooks with a provided example to better understand the essence of this article.&lt;/p&gt;

&lt;p&gt;It is recommended to look at the React documentation to better understand React hooks and how to use them. I would be writing more on these hooks. Now you should be able to differentiate between the two hooks and also identify when to apply each of the hook.&lt;/p&gt;

&lt;p&gt;Thanks for reading.&lt;/p&gt;

</description>
      <category>react</category>
      <category>reacthooks</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Git for Beginners</title>
      <dc:creator>adams mercy</dc:creator>
      <pubDate>Fri, 03 Jun 2022 23:52:17 +0000</pubDate>
      <link>https://forem.com/m_adams1909/git-for-beginners-4857</link>
      <guid>https://forem.com/m_adams1909/git-for-beginners-4857</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;As a developer at any level, knowledge of Git is really essential. This tutorial is beginner friendly and covers everything you need to know to get started with Git, Git commands, Git hosting services, etc. as a developer. In this tutorial, we will answer: what is Git? , why Git? , how to use Git, what is version control system and many more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Version Control
&lt;/h3&gt;

&lt;p&gt;Version control, also known as source control, is the practice of tracking and managing changes to software code. Version control systems are software tools that help software teams manage changes to source code over time. Version control software keeps track of every modification to the code in a special kind of database. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members. Examples of version control system are Git, SVN (Apache Subversion), CVS, mercurial, etc.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Git?
&lt;/h3&gt;

&lt;p&gt;Git is a free and open source distributed version control system designed to handle everything from small to very large projects. By far, the most widely used modern version control system in the world today is Git. Git has been designed with performance, security and flexibility in mind.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Git?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Git is good: Git has the functionality, performance, security and flexibility that most teams and individual developers need.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Git is a de facto standard: Git is the most broadly adopted tool of its kind. This makes Git attractive for many reasons. The predominance of Git also means that many third party software tools and services are already integrated with Git&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Git is a quality open source project&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installing Git
&lt;/h3&gt;

&lt;p&gt;Before you start using Git, you have to make it available on your computer. Even if it’s already installed, it’s probably a good idea to update to the latest version. Click &lt;a href="https://git-scm.com/book/en/v2/Getting-Started-Installing-Git"&gt;here &lt;/a&gt;to read more on installing Git and &lt;a href="https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup"&gt;here &lt;/a&gt;to set it up.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Git
&lt;/h3&gt;

&lt;p&gt;To ascertain you have Git installed, run “git --version” to see the version. Let’s get your identity set up on git. &lt;br&gt;
The first thing you should do when you install Git is to set your user name and email address. Run the following commands:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;[git config --global user.name “your username here”]&lt;br&gt;
[git config --global user.email “your email address here”]&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Git commands
&lt;/h3&gt;

&lt;p&gt;Here are the list of some common basic commands. Atlassian listed more basic git commands, see &lt;a href="https://www.atlassian.com/git/tutorials/svn-to-git-prepping-your-team-migration"&gt;here&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cAMzP8a2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1yvr9wuu3pqp9yf2cqvj.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cAMzP8a2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1yvr9wuu3pqp9yf2cqvj.PNG" alt="Basic Git commands" width="591" height="579"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YdUJfd32--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ebrfa3v2p7v15f0co7k.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YdUJfd32--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ebrfa3v2p7v15f0co7k.PNG" alt="Basic Git commands" width="591" height="499"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--f7UFI8Y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1zmyq1dtpzi23jcmetb.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--f7UFI8Y---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g1zmyq1dtpzi23jcmetb.PNG" alt="Basic Git commands" width="590" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Git hosting services
&lt;/h3&gt;

&lt;p&gt;There are quite a number of Git repository hosting services. Examples are Bitbucket, GitHub, GitLab, and many more. I have used Bitbucket and am using GitHub currently. In the next few paragraph, I will cover GitHub.&lt;/p&gt;

&lt;h3&gt;
  
  
  GitHub
&lt;/h3&gt;

&lt;p&gt;GitHub is the single largest host for Git repositories, and is the central point of collaboration for millions of developers and projects. A large percentage of all Git repositories are hosted on GitHub, and many open-source projects use it for Git hosting, issue tracking, code review, and other things.&lt;/p&gt;

&lt;h4&gt;
  
  
  Getting Started with GitHub
&lt;/h4&gt;

&lt;p&gt;Head over to &lt;a href="https://github.com/"&gt;GitHub &lt;/a&gt;and create an account and then update your information.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to use Git and GitHub
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Go to GitHub, and under Repository tab, click on “New” to create a repository.&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Give it a name&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make it public for other to access it / private if you don’t want others to access it&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;After creating the repo, a new page is displayed containing the commands to connect your remote repository with your local repository. Copy the all commands (using an icon displayed at the top of the commands) depending option best suited for you. The first commands is for a project you are just starting on in which you want to manage with git or a project you are working that isn’t managed with git while the second commands is for a project that exist on your local machine which is being managed by git locally only but you want to manage remotely on GitHub.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Eknah5XF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/znq5clx9i0n4n6gei195.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Eknah5XF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/znq5clx9i0n4n6gei195.png" alt="github" width="880" height="326"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open your project in a code editor and paste in the commands in the terminal (Visual Studio Code editor only)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open and Paste the commands in your command line interface (examples are git bash, command prompt, etc.) and press enter to run the commands. Note: Visual Studio Code has a built-in command line interface. You can also install git bash to use it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After this, your remote repository (GitHub) is connected to your local repository (local machine).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Make changes to your code, add those changes (&lt;code&gt;git add .&lt;/code&gt;) and commit (&lt;code&gt;git commit –m ‘your message’&lt;/code&gt;) and then push (&lt;code&gt;git push&lt;/code&gt;) to update the remote repository. The below image shows the state of the remote repository after pushing the local changes.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pSIFWAXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zfaom2cetaf2sswy33g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pSIFWAXv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0zfaom2cetaf2sswy33g.png" alt="remote files" width="880" height="308"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There is a lot we can do and achieve with Git and any Git hosting service, like collaborating with other developers on projects, contributing to open source projects, etc. &lt;br&gt;
This tutorial has guided you into what Git is, and has demonstrated how to use Git with GitHub. Always refer back to the commands above to see what they do and their usage. Happy coding and collaboration with developers.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Data fetching with Axios in React made simple</title>
      <dc:creator>adams mercy</dc:creator>
      <pubDate>Wed, 26 May 2021 09:27:46 +0000</pubDate>
      <link>https://forem.com/m_adams1909/data-fetching-with-axios-in-react-made-simple-2jei</link>
      <guid>https://forem.com/m_adams1909/data-fetching-with-axios-in-react-made-simple-2jei</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;I started a project on react which I was new to some months ago and I got to a point I needed to fetch data from the server and I encountered some difficulties trying to fetch data from API. After some research and practice, I understood the concept and was able to fetch data in some other projects. My aim is to work you through on how to get started with axios and understand the necessary and basic part so as not to get stuck. At the end of this article, you will be able to get and post data to a server.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;p&gt;This project app is built with Material UI and React. A little knowledge of react will be helpful.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Axios?
&lt;/h3&gt;

&lt;p&gt;Axios is a third party JavaScript library. Axios is a promise-based HTTP client easy to use for both browser and node.js.&lt;/p&gt;

&lt;p&gt;Axios is a Promised-based JavaScript library that is used to send HTTP requests. You can think of it as an alternative to JavaScript's native fetch() function.&lt;/p&gt;

&lt;p&gt;Axios is a modern, Promise-based HTTP client library. This means that Axios is used to send an HTTP request and handle their responses, all using JavaScript's promises. Axios supports both Node.js and JavaScript in the browser.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features of axios
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Make XMLHttpRequests from the browser&lt;/li&gt;
&lt;li&gt;Make http requests from node.js&lt;/li&gt;
&lt;li&gt;Supports the Promise API&lt;/li&gt;
&lt;li&gt;Intercept request and response&lt;/li&gt;
&lt;li&gt;Transform request and response data&lt;/li&gt;
&lt;li&gt;Cancel requests&lt;/li&gt;
&lt;li&gt;Automatic transforms for JSON data&lt;/li&gt;
&lt;li&gt;Client side support for protecting against XSRF&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Where do we make http request?
&lt;/h3&gt;

&lt;p&gt;In a &lt;strong&gt;class based component&lt;/strong&gt;, requests are made in &lt;strong&gt;componentDidMount()&lt;/strong&gt; lifecycle while in a functional component, requests are made in &lt;strong&gt;react lifecycle hooks i.e. useEffect.&lt;/strong&gt;&lt;br&gt;
 To use Axios, axios needs to be installed in your project and imported in the component you want to fetch data in. To install axios using npm which I used, run "&lt;em&gt;npm install axios&lt;/em&gt;" in your command prompt.&lt;br&gt;
Axios supports several request methods such as get, post, delete, put, etc. &lt;br&gt;
Our major focus will be on &lt;strong&gt;get and post method&lt;/strong&gt; which is commonly used.&lt;/p&gt;

&lt;h3&gt;
  
  
  Fetching data in Axios using the Get method
&lt;/h3&gt;

&lt;p&gt;Axios offers a get method with at least one argument (url).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example, let's see axios in action:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;axios.get(' ')&lt;br&gt;
   .then(response =&amp;gt; {&lt;br&gt;
    console.log(response);&lt;br&gt;
   });&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3xfsf5y82jp1kv1zo1ew.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3xfsf5y82jp1kv1zo1ew.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above code is a simple API fetch using axios. Now, let's explain:&lt;/p&gt;

&lt;p&gt;Axios uses promises and get returns a promise 'then' which is a method which takes a function as the input and the function will get executed once the promise resolves, that is when the data from the server is there.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F041i30bysdvgnmyyj6uc.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F041i30bysdvgnmyyj6uc.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the code, we create an arrow function where we fetched data from the server and passed in into a variable called getRepo and called it in the lifecycle hooks. The second parameter [ ] empty array was passed so that the lifecycle hooks runs just once.&lt;/p&gt;

&lt;p&gt;Among the response gotten back from the API, we only need to display the data, that is why we stored response.data inside myRepo container and then passed it to the state.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2kwxv2rwa20hshhr243y.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2kwxv2rwa20hshhr243y.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The above image shows how the data fetched is being consumed or used in my component. Since the data returned is an array, we map through the array and then get the data we want to display and display it at the appropriate element.&lt;/p&gt;

&lt;p&gt;The output will be:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh93v61kdadvd50m6r7r7.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh93v61kdadvd50m6r7r7.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqfpey8m4onntidcy4d2n.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqfpey8m4onntidcy4d2n.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Error Handling
&lt;/h3&gt;

&lt;p&gt;If we have a network failure or if we make mistakes in the url, how do we handle this error?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffmp24us9frc952a6tnbh.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffmp24us9frc952a6tnbh.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To handle this error, add a catch method which catches any error you get, after the then method.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;.catch ((error) {&lt;br&gt;
    console.log(error)&lt;br&gt;
});&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Output: Error handled properly&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlxdlyt41f8wzf8fz61y.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwlxdlyt41f8wzf8fz61y.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another way to get or fetch data from the server using the async/await function&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe33pc6jr4qvo3207czca.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe33pc6jr4qvo3207czca.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use the async/await syntax, we need to wrap the axios.get() function call within an async function. We encase the method call with a try…catch block so that we can capture any errors. The variable “response” that receives the http data had to use await to ensure the asynchronous data was received before continuing.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to Post data to the Server
&lt;/h3&gt;

&lt;p&gt;Post method takes url as the input but also needs a second argument which is the data you want to send. You can also pass a third argument to configure the request. You can listen to the request and print it in the console.&lt;br&gt;
The below code is another way to post data using the async/await function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72fmhzxicri76tjgtx2.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw72fmhzxicri76tjgtx2.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkuum4td5pkw5fc6klniw.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkuum4td5pkw5fc6klniw.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;In this article, you have learned how to make http requests to the server using axios (an alternative to fetch) and we have been able to get data from server and also post data to the server using both promise and async/await which is a great start. I am sure this article has made you axios journey a nice one. Feel free to practice what you have learnt and exploit other axios request methods.&lt;/p&gt;

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