<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: MAXWELL KIMAIYO</title>
    <description>The latest articles on DEV Community by MAXWELL KIMAIYO (@developerkimaiyo).</description>
    <link>https://dev.to/developerkimaiyo</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%2F498424%2Fe4b52d5c-559f-4128-b490-2d0cbefc102a.jpeg</url>
      <title>DEV Community: MAXWELL KIMAIYO</title>
      <link>https://dev.to/developerkimaiyo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/developerkimaiyo"/>
    <language>en</language>
    <item>
      <title>What is Render in React and How Do You Force it?</title>
      <dc:creator>MAXWELL KIMAIYO</dc:creator>
      <pubDate>Sat, 21 Nov 2020 17:40:14 +0000</pubDate>
      <link>https://dev.to/developerkimaiyo/what-is-render-in-react-and-how-do-you-force-it-4pcp</link>
      <guid>https://dev.to/developerkimaiyo/what-is-render-in-react-and-how-do-you-force-it-4pcp</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;What is Render in React?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;React takes over the manipulation of DOM with the use of the React.createElement function so that we don’t have to do it manually. Instead, updates are done only when needed. We only describe how we want the DOM to look with JSX or pure createElement function, and React creates a virtual representation of DOM. Then, based on it, the real DOM will be updated whenever there are differences after the state changed. What’s more, if there are many DOM updates scheduled, React can batch them for efficiency. Nevertheless, this whole process consists of three stages: Render, Reconciliation, and Commit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Render&lt;/strong&gt; – React calls the render function to gather output from createElement functions&lt;br&gt;
&lt;strong&gt;Reconciliation&lt;/strong&gt; – New elements are compared against previously given elements and the virtual DOM is updated if there are differences&lt;br&gt;
&lt;strong&gt;Commit&lt;/strong&gt; – The real DOM is updated&lt;/p&gt;

&lt;p&gt;Like I mentioned before, changing the state does not mean that the commit phase will be executed, as there will be no need for it if there were no changes in the virtual DOM. As you can see in the example below, no matter how many times we click the button, the name property is set to the same value, despite the fact that we call the setState method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class App extends Components {  
    state = {
    name: ‘Thomas’
}

    onClickHandler = () =&amp;gt; {
        this.setState({name: ‘Thomas’})
}

    render() {
        &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt;My name is {this.state.name}&amp;lt;/p&amp;gt;&amp;lt;br/&amp;gt;
            &amp;lt;button onClick={this.onClickHandler}&amp;gt;Click me&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you put a console log in the render function, you will see that it will be called. However, if you check the DOM in the inspector, you won’t see a flash that indicates a change in DOM. Now, let’s talk about how we can trigger a re-render.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Forcing Re-render of a Component in React&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If you are using a React class component then it is as easy as using this.forceUpdate() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class App extends Components {  
    onClickHandler = () =&amp;gt; {
        this.forceUpdate()
}

    render() {
        &amp;lt;button onClick={this.onClickHandler}&amp;gt;Click me&amp;lt;/button&amp;gt;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Just make sure that this context refers to the component instance. In the example below, this refers to the scope of the inner function and not of the React component instance, and because of that, it won’t work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// This won’t work
class App extends Components {  
    onClickHandler = () =&amp;gt; {
        function innerFunction() {
            this.forceUpdate()
        }
        innerFunction()
}

    render() {
        &amp;lt;button onClick={this.onClickHandler}&amp;gt;Click me&amp;lt;/button&amp;gt;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you know how easy it is, but be aware that in 99.99% of cases you should not need it. If you do, then you might be doing something wrong, and probably there is a better solution to what you are trying to achieve. The benefit of the force update function over setState is the fact that it will update a component even if the shouldComponentUpdate lifecycle hook is implemented.&lt;/p&gt;

&lt;p&gt;If you are updating state values, but they are not rendered correctly, then instead of providing a new value, you might be directly mutating the current state. There is also a possibility that you are passing the same reference. Remember that when updating the state, you should always provide a new value. For example, strings are immutable; however, objects and arrays are passed as a reference, so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ Equality check is done by checking if values are the same
const str1 = ‘hello’
const str2 = ‘hello’

str1 == str2    // true

// Equality check is performed by checking if values have the same reference
const obj1 = {str: ‘hello’}
const obj2 = {str: ‘hello’}
const obj3 = obj1
ob1 == obj2 // false
obj3 == obj1    // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Forcing a Re-render in a Functional Component&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;In a function component, there is no forceUpdate method. However, we can mimic this functionality with the code below.&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’

const App = props =&amp;gt; {
const [count, setCount] = useState(0)
const onClickHandler = e = =&amp;gt; {
    setCount(prevCount =&amp;gt; prevCount + 1)
}

return (
    &amp;lt;button onClick={onClickHandler}&amp;gt;Click me&amp;lt;/button&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, whenever we need the component to be re-rendered, we just increment the counter. To be honest, we can go even further than that and create a custom hook for it.&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’

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

    const increment = () =&amp;gt; setCount(prevCount =&amp;gt; prevCount + 1)
    return [increment, count]
}

const App = props =&amp;gt; {
const [forceUpdate] = useForceUpdate()

const onClickHandler = e =&amp;gt; {
    forceUpdate()
}

return (
    &amp;lt;button onClick={onClickHandler}&amp;gt;Click me&amp;lt;/button&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have seen how to force re-render of a component. If for any reason you would like to re-render a child component from a parent, then you can do it by changing its prop as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ChildComponent = props =&amp;gt; {
    return (
        // child markup
)
}

const App = props =&amp;gt; {  
const [forceUpdate, forceUpdateValue] = useForceUpdate()

const onClickHandler = e =&amp;gt; {
    forceUpdate()
}

return (
    &amp;lt;div&amp;gt;
        &amp;lt;ChildComponent key={forceUpdateValue} /&amp;gt;
        &amp;lt;button onClick={onClickHandler}&amp;gt;Click me&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this article, we covered what render is in React, what happens when state is updated, and how to force a re-render in class and functional components. For the final note, remember, if you ever think that you need to force a re-render, think again as there might be a better way.&lt;/p&gt;

</description>
      <category>react</category>
    </item>
    <item>
      <title>Deploy React Apps for Free With Firebase Hosting</title>
      <dc:creator>MAXWELL KIMAIYO</dc:creator>
      <pubDate>Sat, 21 Nov 2020 11:43:42 +0000</pubDate>
      <link>https://dev.to/developerkimaiyo/deploy-react-apps-for-free-with-firebase-hosting-5cpd</link>
      <guid>https://dev.to/developerkimaiyo/deploy-react-apps-for-free-with-firebase-hosting-5cpd</guid>
      <description>&lt;p&gt;With the rise of cloud computing, hosting web apps on services like Heroku, AWS, Azure, Netfliy, and many more have been on the rise. Amidst all these options, Firebase has emerged as a great solution to host serverless web apps. It's easy, pretty fast, and Free! You don't need a lot of working to have your website running.&lt;/p&gt;

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

&lt;p&gt;In this tutorial, I'll take you through all the steps involved in deploying a React app on firebase. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Prerequisites:&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A firebase project set up. If you don't have one, create one using the firebase console.&lt;/li&gt;
&lt;li&gt;A React app set up that you wish to deploy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Getting Your React App Ready for Deployment&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Run the following command to create a &lt;strong&gt;build&lt;/strong&gt; directory with a production build of your app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ npm run build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the &lt;strong&gt;build/static&lt;/strong&gt; directory will be your JavaScript and CSS files. To know more about React production builds, refer to the &lt;br&gt;
&lt;a href="https://create-react-app.dev/docs/production-build/"&gt;production build&lt;/a&gt;&lt;br&gt;
 section of the create-react-app docs.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Configuring Firebase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Install Firebase CLI&lt;br&gt;
To host your site with Firebase Hosting, you need the Firebase command-line tool (CLI). Run the following npm command to install the CLI on your system globally&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 -g firebase-tools
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  &lt;strong&gt;Login to Firebase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Make sure you're in the root directory of your React app and run the following command to log in to firebase in your terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ firebase login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're not logged in, you'll be asked to enter the credentials for your google account.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Initiate your project&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Now that you've got the firebase CLI configured, it's time to initialize firebase in your react app. Run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ firebase init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You will now enter the firebase tools shell and will be prompted with a sequence of questions and various configuration options. Let's go through these questions together step by step to achieve our desirable configuration.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U-JzFtne--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956349686/JUv0x3sVR.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U-JzFtne--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956349686/JUv0x3sVR.png" alt="Screenshot from 2020-11-21 13-52-55.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select the firebase project that you created&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9ZO4CqH3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956535306/NsxSQurz5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9ZO4CqH3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956535306/NsxSQurz5.png" alt="Screenshot from 2020-11-21 13-55-52.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specify the &lt;strong&gt;build&lt;/strong&gt; that we created in the previous section, to be used as your project's public directory.&lt;/li&gt;
&lt;li&gt;Select whether or not you need your firebase app to be configured as a single-page app. I'm selecting Yes in my case.&lt;/li&gt;
&lt;li&gt;As we've already created a build directory in our previous section, therefore &lt;strong&gt;build/index.html&lt;/strong&gt; already exists. We would want to let it be as is and enter No for this question.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G7jIDVQl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956572936/rhtKtYsrO.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G7jIDVQl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956572936/rhtKtYsrO.png" alt="Screenshot from 2020-11-21 13-56-30.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This marks the end of our configuration process and our app is now ready to deploy!&lt;/p&gt;

&lt;p&gt;To verify the successful completion of the initialization process, simply check the presence of &lt;strong&gt;.firebaserc&lt;/strong&gt; and &lt;strong&gt;firebase.json&lt;/strong&gt; files. These are automatically created by firebase during the initialization process.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploy to Firebase
&lt;/h3&gt;

&lt;p&gt;Just run the following command to deploy your app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ firebase deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7X6dCtuU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956777684/102QIF-tk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7X6dCtuU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode.com/res/hashnode/image/upload/v1605956777684/102QIF-tk.png" alt="Screenshot from 2020-11-21 13-57-53.png"&gt;&lt;/a&gt;&lt;br&gt;
Firebase will now run the deploying process and will give you a unique URL where your app is deployed. For e.g. in my case, it was &lt;a href="https://developer-kimaiyo.web.app"&gt; https://developer-kimaiyo.web.app&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Voila! Your ReactApp is now deployed with Firebase hosting. &lt;/p&gt;

&lt;p&gt;To know more about Firebase hosting, refer to the &lt;a href="https://firebase.google.com/docs/hosting"&gt;hosting section&lt;/a&gt; of the firebase docs.&lt;/p&gt;

</description>
      <category>react</category>
      <category>firebase</category>
    </item>
    <item>
      <title>How to Organize Your React/Redux Projects </title>
      <dc:creator>MAXWELL KIMAIYO</dc:creator>
      <pubDate>Sun, 25 Oct 2020 14:38:31 +0000</pubDate>
      <link>https://dev.to/developerkimaiyo/how-to-organize-your-react-redux-projects-5dpj</link>
      <guid>https://dev.to/developerkimaiyo/how-to-organize-your-react-redux-projects-5dpj</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;React is one of the most unopinionated frontend frameworks in existence. From the selection of states, and routing to managing your code structure, React doesn't provide any guidance on how to structure your project - and that can be really confusing for developers.&lt;/p&gt;

&lt;p&gt;Here is the best way you can structure you &lt;em&gt;react/redux&lt;/em&gt; app&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
└──src/
     │
     ├──actions
     │       ├── Post.action.js
     │       └── User.action.js
     ├── components
     │       ├── Header.js
     │       ├── Footer.js
     │       └── Error.js
     ├── containers
     │       ├── PostContainer.js
     │     ├── LoginContainer.js
     │       └── RegisterContainer.java
     ├── constants
     │       ├── User.constant.js
     │       └── Post.constant.js
     ├── images
     │       ├── user.jpg
     │       └── post.png
     ├── reducers
     │       ├── Post.reducer.js
     │       └── Pser.reducer.js
     ├── style
     │       └── Main.css    
     ├── util
     │       ├── Store.js
     │     └── authUtils.js
     ├── views
     │       ├── Home.js
     │      ├── Register.js
     │       └── Login.js
     │
     ├── index.js
     └── root.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Directory functions, in brief, include the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;components&lt;/strong&gt; - Contains all 'dumb' or shared components, consisting only of JSX and styling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;containers&lt;/strong&gt; - Contains all corresponding components with logic in them. Each container will have one or more components depending on the view represented by the container. For example, PostContainer.js would have a Header.js as well as Footer.js.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;images&lt;/strong&gt; - Contain all Images that will be imported inside a component&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;actions&lt;/strong&gt; - All Redux actions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;reducers&lt;/strong&gt; - All Redux reducers&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;style&lt;/strong&gt; - This is where you include all of your main stylings &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;utils&lt;/strong&gt; - Other logical codes that are not React specific. For example, authUtils.js would have functions to process the JWT token from the API to determine the user scopes and store.js which simply is the Redux store.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;view&lt;/strong&gt; - keeps app pages or screens  (route endpoints)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;routes.js&lt;/strong&gt; - aggregates all routes together for easy access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Defining all routes in a single file has been deprecated as a practice, according to new React Router docs. It promoted segregating routes into components for better readability. Check &lt;a href="https://reactrouter.com/web/example/route-config"&gt;React Router Docs&lt;/a&gt; for a better understanding.&lt;/p&gt;

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