<?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: Collins Mutai </title>
    <description>The latest articles on Forem by Collins Mutai  (@collinsmutai).</description>
    <link>https://forem.com/collinsmutai</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%2F363063%2F49d3bc5e-e21d-4d4d-81ef-140eda79112c.jpeg</url>
      <title>Forem: Collins Mutai </title>
      <link>https://forem.com/collinsmutai</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/collinsmutai"/>
    <language>en</language>
    <item>
      <title>Upload image files with Multer</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Fri, 26 Jan 2024 05:56:24 +0000</pubDate>
      <link>https://forem.com/collinsmutai/upload-image-files-with-multer-346j</link>
      <guid>https://forem.com/collinsmutai/upload-image-files-with-multer-346j</guid>
      <description>&lt;p&gt;Multer is a &lt;a href="https://expressjs.com/en/resources/middleware/multer.html"&gt;nodejs middleware&lt;/a&gt; for uploading files in a nodejs application (Express Multer Middleware, n.d.).&lt;br&gt;
To use multer install the package using&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install multer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once it is installed, import the package using this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const multer = require("multer")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next is configuring the files' storage destination and preferred filenames. A callback function is used to create the filenames for each of the file uploads.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const storage = multer.diskStorage({
  destination: "./images",
  filename: (req, file, cb) =&amp;gt; {
    return cb(
      null,
      `${file.fieldname}_${Date.now()}`
    );
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After configuring the storage location and the filename, add the storage object to a multer instance.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const upload = multer({ storage: storage });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The middleware is then added to the request to process any incoming file from the request body. A post request to the route &lt;strong&gt;"/upload"&lt;/strong&gt; will upload a single image file from a multi-part form field named product and save it inside a folder called images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.post("/upload", upload.single("product"), (req, res) =&amp;gt; {
  res.json({
  file_info: req.file,
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To access the file details, we can use the output of the &lt;strong&gt;res.file&lt;/strong&gt; object and return it in json format.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "file_info": {
    "fieldname": "product",
    "originalname": "img3.jpeg",
    "encoding": "7bit",
    "mimetype": "image/jpeg",
    "destination": "./upload/images",
    "filename": "1706247839665_img3.jpeg",
    "path": "upload/images/1706247839665_img3.jpeg",
    "size": 107421
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an easy way to upload files in a nodejs application. With more configurations, you can specify the required file types and so on.&lt;br&gt;
&lt;strong&gt;Credits:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;GreatStack. (2024, January 2). How to create full stack E-Commerce website using React JS, MongoDB, Express &amp;amp; Node JS 2024 [Video]. YouTube. &lt;a href="https://www.youtube.com/watch?v=y99YgaQjgx4"&gt;https://www.youtube.com/watch?v=y99YgaQjgx4&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Express multer middleware. (n.d.). &lt;a href="https://expressjs.com/en/resources/middleware/multer.html"&gt;https://expressjs.com/en/resources/middleware/multer.html&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>multer</category>
      <category>node</category>
      <category>express</category>
      <category>fileupload</category>
    </item>
    <item>
      <title>React useContext to Manage State</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Sun, 03 Dec 2023 21:44:57 +0000</pubDate>
      <link>https://forem.com/collinsmutai/react-usecontext-to-manage-state-4795</link>
      <guid>https://forem.com/collinsmutai/react-usecontext-to-manage-state-4795</guid>
      <description>&lt;p&gt;I struggled with useContext before I could wrap my head around it. It turns out it's really a simple and elegant way to persist data in a react application.&lt;br&gt;
To create a context we need to import from react.&lt;br&gt;
&lt;code&gt;import {useContext} from 'react';&lt;/code&gt;&lt;br&gt;
Then create an instance of the context.&lt;br&gt;
&lt;code&gt;export const UserContext = useContext.create({})&lt;/code&gt;&lt;br&gt;
Then create a wrapper and export it because we'll need to wrap the context around all our components.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
const [name, setName]=(null)&lt;br&gt;
export function ContextProvider({children}){&lt;br&gt;
return (&lt;br&gt;
&amp;lt;UserContext.Provider value={name,setName}&amp;gt;&lt;br&gt;
{children}&amp;lt;/UserContext.Provider&amp;gt;&lt;br&gt;
)&lt;br&gt;
}&lt;/code&gt;&lt;br&gt;
This makes it possible to provide for any of our routes, the value inside our context. So wrapping the ContextProvider in top level component is key. We can do so in the root file.  &lt;code&gt;&amp;lt;ContextProvider&amp;gt;&amp;lt;App/&amp;gt;&amp;lt;/ContextProvider&amp;gt;&lt;/code&gt;&lt;br&gt;
We can also wrap the context around our routes. &lt;code&gt;&amp;lt;ContextProvider&amp;gt;&amp;lt;Routes&amp;gt;&amp;lt;Route path={'/'} element={&amp;lt;Home/&amp;gt;}/&amp;gt;&amp;lt;/Routes&amp;gt;&amp;lt;/ContextProvider&amp;gt;&lt;/code&gt;&lt;br&gt;
For instance when a user logs in we can set the name of the user inside the context therefore making it accessible from any component that might require it.&lt;br&gt;
&lt;code&gt;&lt;br&gt;
import {useContext,useState} from react';&lt;br&gt;
const {UserContext} = './UserContext'&lt;br&gt;
const [username, setUsername] = useState('')&lt;br&gt;
const {setName} = useContext(UserContext);&lt;br&gt;
setName(username)&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Let say inside our header component we need to find out the name of the currently logged user. We can do so by tapping into the context and reading the name that's currently stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {useContext,useState} from react';
const {UserContext} = './UserContext'
const Header = ()=&amp;gt; {
const {name} = useContext(UserContext)

return (
&amp;lt;&amp;gt;
{name &amp;amp;&amp;amp; (
&amp;lt;p&amp;gt;&amp;lt;/p&amp;gt;
)}

&amp;lt;/&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see the useContext hook is really powerful and comes handy in small to medium size applications where you don't need redux or other state management tools to manage complex state. Thanks all for checking this out. Happy coding 🧑‍💻&lt;/p&gt;

</description>
      <category>react</category>
      <category>statemanagement</category>
      <category>usecontext</category>
      <category>reacthooks</category>
    </item>
    <item>
      <title>React useEffect Hook</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Sun, 03 Dec 2023 20:34:18 +0000</pubDate>
      <link>https://forem.com/collinsmutai/react-useeffect-hook-37n6</link>
      <guid>https://forem.com/collinsmutai/react-useeffect-hook-37n6</guid>
      <description>&lt;p&gt;A react hook that runs after component's first render. It has to be called inside a component function like all react hooks.&lt;br&gt;
&lt;/p&gt;

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

useEffect(()=&amp;gt; {
console.log('useeffect running')
},[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useEffect is useful when we want to perform an action when the state of our application changes. For example when getting user input. The state change will trigger the useEffect hook to rerun. The state acts as a dependency.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```import {useState} from 'react';&lt;/p&gt;

&lt;p&gt;const [userInput, setUserInput]= useState('')&lt;/p&gt;

&lt;p&gt;useEffect(()=&amp;gt; {&lt;br&gt;
console.log('useeffect running')&lt;br&gt;
}, [userInput])&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Finally the useEffect allows us to run cleanup actions. The clean up function runs right after the dependencies change but before the useEffect is triggered by the new dependency.



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

&lt;/div&gt;

&lt;p&gt;useEffect(()=&amp;gt; {&lt;br&gt;
console.log('useeffect running')&lt;/p&gt;

&lt;p&gt;return ()=&amp;gt; {&lt;br&gt;
console log('cleanup function')&lt;br&gt;
}&lt;br&gt;
})&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Together with other react hooks, useEffect hook is a powerful tool to build react applications.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>reacthooks</category>
      <category>useeffecthoo</category>
    </item>
    <item>
      <title>React useEffect Hook</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Sun, 03 Dec 2023 20:33:31 +0000</pubDate>
      <link>https://forem.com/collinsmutai/react-useeffect-hook-4e05</link>
      <guid>https://forem.com/collinsmutai/react-useeffect-hook-4e05</guid>
      <description>&lt;p&gt;A react hook that runs after component's first render. It has to be called inside a component function like all react hooks.&lt;br&gt;
&lt;/p&gt;

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

useEffect(()=&amp;gt; {
console.log('useeffect running')
},[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useEffect is useful when we want to perform an action when the state of our application changes. For example when getting user input. The state change will trigger the useEffect hook to rerun. The state acts as a dependency.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;```import {useState} from 'react';&lt;/p&gt;

&lt;p&gt;const [userInput, setUserInput]= useState('')&lt;/p&gt;

&lt;p&gt;useEffect(()=&amp;gt; {&lt;br&gt;
console.log('useeffect running')&lt;br&gt;
}, [userInput])&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Finally the useEffect allows us to run cleanup actions. The clean up function runs right after the dependencies change but before the useEffect is triggered by the new dependency.



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

&lt;/div&gt;

&lt;p&gt;useEffect(()=&amp;gt; {&lt;br&gt;
console.log('useeffect running')&lt;/p&gt;

&lt;p&gt;return ()=&amp;gt; {&lt;br&gt;
console log('cleanup function')&lt;br&gt;
}&lt;br&gt;
})&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

Together with other react hooks, useEffect hook is a powerful tool to build react applications.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>reacthooks</category>
      <category>useeffecthook</category>
    </item>
    <item>
      <title>React useEffect Hook</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Sat, 18 Nov 2023 20:53:45 +0000</pubDate>
      <link>https://forem.com/collinsmutai/react-useeffect-hook-307l</link>
      <guid>https://forem.com/collinsmutai/react-useeffect-hook-307l</guid>
      <description>&lt;p&gt;A react hook that runs after component's first render. It has to be called inside a component function like all react hooks.&lt;br&gt;
&lt;/p&gt;

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

useEffect(()=&amp;gt; {
console.log('useeffect running')
},[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The useEffect is useful when we want to perform an action when the state of our application changes. For example when getting user input. The state change will trigger the useEffect hook to rerun. The state acts as a dependency.&lt;br&gt;
&lt;/p&gt;

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

const [userInput, setUserInput]= useState('')

useEffect(()=&amp;gt; {
console.log('useeffect running')
}, [userInput])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally the useEffect allows us to run cleanup actions. The clean up function runs right after the dependencies change but before the useEffect is triggered by the new dependency.&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; {
console.log('useeffect running')

return ()=&amp;gt; {
console log('cleanup function')
}
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Together with other react hooks, useEffect hook is a powerful tool to build react applications.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>reacthooks</category>
      <category>useeffecthook</category>
    </item>
    <item>
      <title>Template driven forms in Angular</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Wed, 15 Mar 2023 12:52:48 +0000</pubDate>
      <link>https://forem.com/collinsmutai/template-driven-forms-in-angular-2eb7</link>
      <guid>https://forem.com/collinsmutai/template-driven-forms-in-angular-2eb7</guid>
      <description>&lt;p&gt;To use template driven approach to handle forms. We assign a name to the form using the NgForm directive. We then use a submit event handler, to listen to a submit event. Since our form button is of type submit, a submit event is triggered every time a user submits the form.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form (submit)="onAddPost(postForm)" #postForm="ngForm"&amp;gt;
    &amp;lt;input type="text" ngModel name="title" #title="ngModel"&amp;gt;
    &amp;lt;textarea rows="4" ngModel name="content"  #content="ngModel"&amp;gt;&amp;lt;/textarea&amp;gt;
    &amp;lt;button type="submit"&amp;gt; Save Post &amp;lt;/button&amp;gt; 
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The NgForm creates an instance of the form and binds to the form in the template. To access the forms inputs we call the form instance and its value property.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onAddPost(form: NgForm) {
    console.log(form, form.value, form.value.title, form.value.content);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Zuri internship August 2021 Cohort</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Mon, 16 Aug 2021 08:10:06 +0000</pubDate>
      <link>https://forem.com/collinsmutai/zuri-internship-august-2021-cohort-2l11</link>
      <guid>https://forem.com/collinsmutai/zuri-internship-august-2021-cohort-2l11</guid>
      <description>&lt;p&gt;My name is Collins Mutai, a fullstack software engineer from Kenya. I am on the backend development track for the August 2021 Zuri internship cohort. Zuri internship ia a 3 month long remote internship that enables participants to learn and improve their skills by working on real-world projects.&lt;br&gt;
Upon successful completion of the Zuri internship, I hope to achieve the following goals.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Gain hands-on experience in a real work environment setting.&lt;/li&gt;
&lt;li&gt;Build a portfolio of real-world projects.&lt;/li&gt;
&lt;li&gt;Widen my network and collaborate with other students across the globe.&lt;/li&gt;
&lt;li&gt;Gain mentorship from industry experts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To all newbies, entry levels, intermediate and experts this is an opportunity to improve your knowledge and advance your career. Checkout the link attached to learn more. [&lt;a href="https://internship.zuri.team/"&gt;https://internship.zuri.team/&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Below is some helpful beginner-friendly tutorial to get you up-to speed on the technologies that will be used during the internship period; Figma, Git and Python.&lt;br&gt;
Figma tutorial&lt;br&gt;
[&lt;a href="https://www.youtube.com/watch?v=c9Wg6Cb_YlU"&gt;https://www.youtube.com/watch?v=c9Wg6Cb_YlU&lt;/a&gt;]&lt;br&gt;
Git tutorial&lt;br&gt;
[&lt;a href="https://www.youtube.com/watch?v=8JJ101D3knE"&gt;https://www.youtube.com/watch?v=8JJ101D3knE&lt;/a&gt;]&lt;br&gt;
Python tutorial&lt;br&gt;
[&lt;a href="https://www.youtube.com/watch?v=8ext9G7xspg&amp;amp;t=1195s"&gt;https://www.youtube.com/watch?v=8ext9G7xspg&amp;amp;t=1195s&lt;/a&gt;]&lt;/p&gt;

&lt;p&gt;Get in touch and feel free if you have any questions, will be happy to help. Happy coding! &lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>codereview</category>
      <category>challenge</category>
    </item>
    <item>
      <title>Basic Git Commands</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Tue, 25 Aug 2020 04:46:26 +0000</pubDate>
      <link>https://forem.com/collinsmutai/basic-git-commands-1l58</link>
      <guid>https://forem.com/collinsmutai/basic-git-commands-1l58</guid>
      <description>&lt;p&gt;In this brief intro, we'll be diving into Git and learn basic git commands. Git is a version control system which comes handy when you're writing code as a team or independently. Some features that git allows includes; creation, merging, and deletion of multiple local branches that can be entirely independent of each other.&lt;/p&gt;

&lt;h1&gt;
  
  
  installation
&lt;/h1&gt;

&lt;p&gt;[&lt;a href="https://git-scm.com/downloads"&gt;https://git-scm.com/downloads&lt;/a&gt;]&lt;br&gt;
Use the git installer and follow the steps.&lt;br&gt;
Check for successful installation and version&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;git --version&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Set global configurations
&lt;/h1&gt;

&lt;p&gt;Now lets configure git by running the following global commands.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git config --global user.name "user name"&lt;br&gt;
git config --global user.email "email address"&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  git init
&lt;/h1&gt;

&lt;p&gt;Initializes your local git repository as your master.&lt;/p&gt;

&lt;h1&gt;
  
  
  git add . or git add "file name"
&lt;/h1&gt;

&lt;p&gt;To add all the files to staging area, run&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add .&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;or add specific file using&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;git add &amp;lt;file name&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  git commit -m "commit message"
&lt;/h1&gt;

&lt;p&gt;To save the changes to your repository with a message which describes the commit.&lt;/p&gt;

&lt;h1&gt;
  
  
  git remote add origin "repository URL"
&lt;/h1&gt;

&lt;p&gt;When you have an online repository and would like to link it to your local repository, you can do so by running.&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;git remote add origin &amp;lt;repository URL&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  git push -u origin master
&lt;/h1&gt;

&lt;p&gt;To push the local repository to the online repository.&lt;/p&gt;

&lt;h1&gt;
  
  
  git remote rename origin upstream
&lt;/h1&gt;

&lt;p&gt;Running this command will rename the online repository. Then add the new URL using "git remote add origin &amp;lt;repository URL" and push the local to the online using "git push -u origin master"&lt;/p&gt;

&lt;h1&gt;
  
  
  git clone "repository URL"
&lt;/h1&gt;

&lt;p&gt;To clone an online repository to your local machine.&lt;/p&gt;

&lt;h1&gt;
  
  
  git revert
&lt;/h1&gt;

&lt;p&gt;Will reverse changes made by an earlier commit.&lt;/p&gt;

&lt;h1&gt;
  
  
  git status
&lt;/h1&gt;

&lt;p&gt;To view the current status of the repository.&lt;/p&gt;

&lt;h1&gt;
  
  
  git checkout  
&lt;/h1&gt;

&lt;p&gt;View file from last commit and add it to staging area.&lt;/p&gt;

&lt;h1&gt;
  
  
  git restore --staged 
&lt;/h1&gt;

&lt;p&gt;Unstage files from last commit.&lt;/p&gt;

&lt;h1&gt;
  
  
  git restore 
&lt;/h1&gt;

&lt;p&gt;Restore file to original last commit.&lt;/p&gt;

&lt;h1&gt;
  
  
  git log --online
&lt;/h1&gt;

&lt;p&gt;Show all commits. &lt;/p&gt;

&lt;h1&gt;
  
  
  git log
&lt;/h1&gt;

&lt;p&gt;Display detail information about all commits.&lt;/p&gt;

&lt;h1&gt;
  
  
  git -b "branch name"
&lt;/h1&gt;

&lt;p&gt;Running this command will create a new branch.&lt;/p&gt;

&lt;h1&gt;
  
  
  git branch -a
&lt;/h1&gt;

&lt;p&gt;Show all branches.&lt;/p&gt;

&lt;h1&gt;
  
  
  git checkout "branch name"
&lt;/h1&gt;

&lt;p&gt;Will move git from the master branch to the new branch.&lt;/p&gt;

&lt;h1&gt;
  
  
  git merge "branch name"
&lt;/h1&gt;

&lt;p&gt;If there are new changes that needs to be added to the master, you can do so by switching back to the master branch.&lt;/p&gt;

&lt;h1&gt;
  
  
  git branch -a
&lt;/h1&gt;

&lt;p&gt;To list branches.&lt;/p&gt;

&lt;h1&gt;
  
  
  git pull
&lt;/h1&gt;

&lt;p&gt;To match the remote repository to the local.&lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Render Media List Component With Reactstrap</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Wed, 01 Jul 2020 17:22:50 +0000</pubDate>
      <link>https://forem.com/collinsmutai/render-media-list-component-with-reactstrap-46pk</link>
      <guid>https://forem.com/collinsmutai/render-media-list-component-with-reactstrap-46pk</guid>
      <description>&lt;h1&gt;
  
  
  Reactstrap
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Reactstrap&lt;/strong&gt; makes it easy to use &lt;strong&gt;Bootstrap 4&lt;/strong&gt; components in a react application. &lt;em&gt;Media&lt;/em&gt; is one of the components, and we'll use it to display a list of items in a simple react application. The list items will consist of an &lt;em&gt;image, heading, &amp;amp; a paragraph.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Installation
&lt;/h1&gt;

&lt;p&gt;To configure our project to use reactstrap, type the following at the terminal to install reactstrap, and Bootstrap 4 via either NPM or Yarn.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install --save bootstrap react-popper reactstrap&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;



&lt;p&gt;&lt;code&gt;yarn add bootstrap react-popper reactstrap&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h1&gt;
  
  
  Adding a Menu Component
&lt;/h1&gt;

&lt;p&gt;Now inside our &lt;em&gt;App.js&lt;/em&gt; let's import the menu component with the following code&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { Media } from "reactstrap";&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;The Media has properties including &lt;em&gt;list, tag, object, heading, &amp;amp; body.&lt;/em&gt; List encloses all the list items, tag encloses each list item, object encloses the image while the heading &amp;amp; body encloses the list heading and body respectively. With that explanation out the way lol 😉, let's add the code snippet below in the App.js to see this in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

export default function App() {
const Example = () =&amp;gt; {
return (
&amp;lt;div&amp;gt;
   &amp;lt;Media list&amp;gt;
      &amp;lt;Media tag="li"&amp;gt;
         &amp;lt;Media left top href="#"&amp;gt;
            &amp;lt;Media
               object
               src="http://place-puppy.com/100x200"
               alt="place-puppy image"
               /&amp;gt;
         &amp;lt;/Media&amp;gt;
         &amp;lt;Media body&amp;gt;
            &amp;lt;Media heading&amp;gt;Top aligned media&amp;lt;/Media&amp;gt;
            Cras sit amet nibh libero, in gravida nulla. Nulla vel metus
            scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum
            in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac
            nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
         &amp;lt;/Media&amp;gt;
      &amp;lt;/Media&amp;gt;
      &amp;lt;Media tag="li"&amp;gt;
         &amp;lt;Media left middle href="#"&amp;gt;
            &amp;lt;Media
               object
               src="http://place-puppy.com/200x200"
               alt="place-puppy image"
               /&amp;gt;
         &amp;lt;/Media&amp;gt;
         &amp;lt;Media body&amp;gt;
            &amp;lt;Media heading&amp;gt;Middle aligned media&amp;lt;/Media&amp;gt;
            Cras sit amet nibh libero, in gravida nulla. Nulla vel metus
            scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum
            in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac
            nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
         &amp;lt;/Media&amp;gt;
      &amp;lt;/Media&amp;gt;
      &amp;lt;Media tag="li"&amp;gt;
         &amp;lt;Media left bottom href="#"&amp;gt;
            &amp;lt;Media
               object
               src="http://place-puppy.com/300x200"
               alt="place-puppy image"
               /&amp;gt;
         &amp;lt;/Media&amp;gt;
         &amp;lt;Media body&amp;gt;
            &amp;lt;Media heading&amp;gt;Bottom aligned media&amp;lt;/Media&amp;gt;
            Cras sit amet nibh libero, in gravida nulla. Nulla vel metus
            scelerisque ante sollicitudin commodo. Cras purus odio, vestibulum
            in vulputate at, tempus viverra turpis. Fusce condimentum nunc ac
            nisi vulputate fringilla. Donec lacinia congue felis in faucibus.
         &amp;lt;/Media&amp;gt;
      &amp;lt;/Media&amp;gt;
   &amp;lt;/Media&amp;gt;
&amp;lt;/div&amp;gt;
);
};
return (
&amp;lt;div className="App"&amp;gt;
   &amp;lt;Example /&amp;gt;
&amp;lt;/div&amp;gt;
);
}


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

&lt;/div&gt;



&lt;p&gt;In essence we're declaring a function component called &lt;em&gt;Example&lt;/em&gt; which returns three list items, each item has an image with a heading and body text.&lt;br&gt;
Please checkout reacstrap documentation below for more examples on how to implement the Media component in a react application.&lt;/p&gt;

&lt;p&gt;Reference [&lt;a href="https://reactstrap.github.io/components/media/"&gt;https://reactstrap.github.io/components/media/&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Passing Props in React</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Sat, 20 Jun 2020 06:56:57 +0000</pubDate>
      <link>https://forem.com/collinsmutai/passing-props-in-react-dg</link>
      <guid>https://forem.com/collinsmutai/passing-props-in-react-dg</guid>
      <description>&lt;h1&gt;
  
  
  Components &amp;amp; Props
&lt;/h1&gt;

&lt;p&gt;Props are inputs which are passed into &lt;strong&gt;Components&lt;/strong&gt; and return React elements describing what should appear on the screen. Components helps with re-usability, by splitting the UI into independent sections. &lt;/p&gt;

&lt;p&gt;With this in mind let's use a simple app that returns a &lt;em&gt;div&lt;/em&gt; element with an &lt;em&gt;h1&lt;/em&gt; that displays a title and a &lt;em&gt;p&lt;/em&gt; tag that displays the content.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
        return (
            &amp;lt;div&amp;gt;
                &amp;lt;h1&amp;gt;Changes in Service&amp;lt;/h1&amp;gt;
                &amp;lt;p&amp;gt;We just updated our privacy policy here to better service our customers.&amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
        );
    }

    // Renders the App component into a div with id 'root'
    ReactDOM.render(&amp;lt;App /&amp;gt;, document.querySelector('#root'));
&amp;lt;/script&amp;gt;


&amp;lt;!--The App component above will be rendered into this--&amp;gt;
&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Passing a Prop to a Component
&lt;/h1&gt;

&lt;p&gt;Next we'll define a function component called &lt;strong&gt;Message&lt;/strong&gt; which accepts a single &lt;strong&gt;"props"(properties).&lt;/strong&gt; The Message component will be used to extract the &lt;strong&gt;h1&lt;/strong&gt; and &lt;strong&gt;p&lt;/strong&gt; as a separate reusable entity in our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const Message = (props) =&amp;gt; {
        return (
            &amp;lt;div&amp;gt;
                &amp;lt;h1&amp;gt;{props.title}&amp;lt;/h1&amp;gt;
                &amp;lt;p&amp;gt;{props.content}&amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
        );
    }

 // Renders the App component into a div with id 'root'
    ReactDOM.render(&amp;lt;App /&amp;gt;, document.querySelector('#root'));
&amp;lt;/script&amp;gt;


&amp;lt;!--The App component above will be rendered into this--&amp;gt;
&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Rendering a Component
&lt;/h1&gt;

&lt;p&gt;Now we can swap out the hard coded &lt;strong&gt;h1&lt;/strong&gt; title and &lt;strong&gt;p&lt;/strong&gt; content by calling our &lt;strong&gt;Message&lt;/strong&gt; component inside our app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
        return (
            &amp;lt;div&amp;gt;
                &amp;lt;Message title="Changes in Service" content="We just updated our privacy policy here to better service our customers." /&amp;gt;
            &amp;lt;/div&amp;gt;
        );
    }

    const Message = (props) =&amp;gt; {
        return (
            &amp;lt;div&amp;gt;
                &amp;lt;h1&amp;gt;{props.title}&amp;lt;/h1&amp;gt;
                &amp;lt;p&amp;gt;{props.content}&amp;lt;/p&amp;gt;
            &amp;lt;/div&amp;gt;
        );
    }


    // Renders the App component into a div with id 'root'
    ReactDOM.render(&amp;lt;App /&amp;gt;, document.querySelector('#root'));
&amp;lt;/script&amp;gt;


&amp;lt;!--The App component above will be rendered into this--&amp;gt;
&amp;lt;div id="root"&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;em&gt;h1&lt;/em&gt; and &lt;em&gt;p&lt;/em&gt; is being passed as a single object which we refer to it as &lt;strong&gt;props.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;That's all for today, thank you guys for following along till the end. For more detail explanation, please checkout React Docs, link attached below. Happy coding. 😎&lt;br&gt;
Reference [&lt;a href="https://reactjs.org/docs/components-and-props.html"&gt;https://reactjs.org/docs/components-and-props.html&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>react</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>How to automate git commits</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Wed, 27 May 2020 11:17:18 +0000</pubDate>
      <link>https://forem.com/collinsmutai/how-to-automate-git-commits-4i6e</link>
      <guid>https://forem.com/collinsmutai/how-to-automate-git-commits-4i6e</guid>
      <description>&lt;p&gt;Create a folder with a file name "push.bat" and save the file type as All files, then add the following code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m %1
git push

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

&lt;/div&gt;



&lt;p&gt;Next configure the path by opening Environment Variables on your machine then edit path, under path section, add the path to the "push.bat" file and click ok to exit.&lt;br&gt;
Now we can push changes to a remote repository using a single line of code 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;push "commit message"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it guys, this was a time saver for me because I'm currently taking a course on Bootstrap 4 and I have to regularly do commits. Happy coding 🤓&lt;/p&gt;

&lt;p&gt;Credit [&lt;a href="https://medium.com/@cibiaananth/how-to-add-commit-and-push-to-git-using-one-command-on-windows-25d756f444b7"&gt;https://medium.com/@cibiaananth/how-to-add-commit-and-push-to-git-using-one-command-on-windows-25d756f444b7&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>github</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Controlled Components - React Forms</title>
      <dc:creator>Collins Mutai </dc:creator>
      <pubDate>Tue, 21 Apr 2020 05:34:52 +0000</pubDate>
      <link>https://forem.com/collinsmutai/controlled-components-react-forms-5ac8</link>
      <guid>https://forem.com/collinsmutai/controlled-components-react-forms-5ac8</guid>
      <description>&lt;p&gt;HTML forms elements like input,textarea, and select typically re-renders every time a user submits an input. However with React, we can keep the state of the input form element in the component that renders it and only update it using useState(). &lt;/p&gt;

&lt;p&gt;With this capability in mind, we can get the value that the user inputs and use it anywhere else on our web application. This makes React state be the "single source of truth". We now can get the value of the input element by looking at our state at that current moment.&lt;/p&gt;

&lt;p&gt;A good example would be something like a login screen, once the user submits their login credentials and is verified, the welcome screen will display a welcome text with their name on it.&lt;/p&gt;

&lt;p&gt;To illustrate this in action we going to be using a div container with an h1 that updates it's innerText with the word "Hello" plus the name submitted on the input field when the user clicks submit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div className="container"&amp;gt;
      &amp;lt;h1&amp;gt;Hello&amp;lt;/h1&amp;gt;
      &amp;lt;input
        type="text"
        placeholder="What's your name?"
       /&amp;gt;
      &amp;lt;button&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's create an event handler which handles onChange events on our input field. The handleChange() will get called when a user starts typing and logs the value to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleChange(event) {
    console.log(event.target.value); // Prints the value from the input field
  }

&amp;lt;div className="container"&amp;gt;
      &amp;lt;h1&amp;gt;Hello&amp;lt;/h1&amp;gt;
      &amp;lt;input
        onChange={handleChange}
        type="text"
        placeholder="What's your name?"
        /&amp;gt;
      &amp;lt;button&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to control the state of our input element, we'll need to declare a useState() and set the value of the input as the state variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [name, setName] = useState("");

  function handleChange(event) {
    setName(event.target.value);
    console.log(name); // Prints the value from the input field
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point everything is working great, we now have access to our state and we know for sure what the value is for our input by checking at our state variable.&lt;/p&gt;

&lt;p&gt;But we far from over , remember our goal was to be able to update the h1 innerText dynamically when the user submits an input. We need to find a way to keep track of our submit button so that every time the user clicks Submit, the h1 is automatically updated. Thankfully, there's an onClick event type that we can tap into.&lt;/p&gt;

&lt;p&gt;Now we're getting close.😜 Using the onClick event, let's create another event handler that gets called whenever the submit button is clicked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleClick(event) {
    console.log("I got clicked!"); //Prints I got clicked!
   }
&amp;lt;div className="container"&amp;gt;
      &amp;lt;h1&amp;gt;Hello&amp;lt;/h1&amp;gt;
      &amp;lt;input
        onChange={handleChange}
        type="text"
        placeholder="What's your name?"
        value={name}
      /&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the h1 will constantly change depending on the input value, we'll need to keep track of it. And an easy way to do that is by using useState() 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;const [headingText, setHeading] = useState("");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next we'll assign the h1 text as the state variable called "headingText, then set headingText as the name variable from our first useState function. In this case the name variable which corresponds to the value of the input field will now be set to the state of our h1. So technically, we are getting the value from the input field and adding it to our h1 text 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;const [headingText, setHeading] = useState("");

function handleClick(event) {
    setHeading(name);
 }

&amp;lt;div className="container"&amp;gt;
      &amp;lt;h1&amp;gt;Hello {headingText}&amp;lt;/h1&amp;gt;
      &amp;lt;input
        onChange={handleChange}
        type="text"
        placeholder="What's your name?"
        value={name}
      /&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our final code snippet will look 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;import React, { useState } from "react";

function App() {
  const [name, setName] = useState("");
  const [headingText, setHeading] = useState("");

  function handleChange(event) {
    setName(event.target.value);
    console.log(name);
  }
  function handleClick(event) {
    setHeading(name);
    // event.preventDefault();
  }
  return (
    &amp;lt;div className="container"&amp;gt;
      &amp;lt;h1&amp;gt;Hello {headingText}&amp;lt;/h1&amp;gt;
      &amp;lt;input
        onChange={handleChange}
        type="text"
        placeholder="What's your name?"
        value={name}
      /&amp;gt;
      &amp;lt;button onClick={handleClick}&amp;gt;Submit&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;

  );
}

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

&lt;/div&gt;



&lt;p&gt;To summarize, we turned the input element into a controlled component, by making the React state be the input’s value . This allowed us to pass the value to other UI elements too like the h1.&lt;/p&gt;

&lt;p&gt;Read more about Controlled Components  🤓 [&lt;a href="https://reactjs.org/docs/forms.html#controlled-components"&gt;https://reactjs.org/docs/forms.html#controlled-components&lt;/a&gt;]&lt;/p&gt;

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