<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Anish Duwal on Medium]]></title>
        <description><![CDATA[Stories by Anish Duwal on Medium]]></description>
        <link>https://medium.com/@anishduwal?source=rss-a4e6e4ea126e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*NgmT8b7TUwlxPPoAW6TH2A.jpeg</url>
            <title>Stories by Anish Duwal on Medium</title>
            <link>https://medium.com/@anishduwal?source=rss-a4e6e4ea126e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 02:03:24 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@anishduwal/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Higher Order Component in React]]></title>
            <link>https://medium.com/@anishduwal/higher-order-component-in-react-f7844f6677ed?source=rss-a4e6e4ea126e------2</link>
            <guid isPermaLink="false">https://medium.com/p/f7844f6677ed</guid>
            <category><![CDATA[higher-order-components]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Anish Duwal]]></dc:creator>
            <pubDate>Sun, 10 Dec 2017 16:46:32 GMT</pubDate>
            <atom:updated>2017-12-10T16:47:09.837Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have been working in React for quite a few times, you may have heard about the term Higher Order Component(HOC) and might also have used it in some of your codebase. However understanding how exactly does HOC work may have been a challenge for you as a beginner, especially for me :). In this article, we will try to understand the concept of HOC with a very basic example.</p><blockquote><strong>Higher Order Components are basically functions that takes a component as an argument and returns a new component.</strong></blockquote><p>Let’s say we need to create a component that renders a loader (with text loading) for a while and then it loads the actual component.</p><p><strong>MyComponent</strong></p><pre>const MyComponent = (props) =&gt; {<br>  if (props.isLoading) {<br>    return &lt;div&gt;Hey! I&#39;m Loading&lt;/div&gt;<br>  }<br>  return &lt;div&gt;Hey! I&#39;m actual Content&lt;/div&gt;<br>}</pre><p>MyComponent basically checks for <em>isLoading </em>prop and renders accordingly.</p><p><strong>MyContainer</strong></p><pre>class MyContainer extends Component {<br>  state = {<br>    isLoading: true,<br>  }</pre><pre>  componentDidMount() {<br>    setTimeout(() =&gt; {<br>      this.setState((state) =&gt; ({<br>        isLoading: !state.isLoading<br>      }))<br>    }, 1000);<br>  }</pre><pre>  render() {<br>    return (<br>      &lt;MyComponent isLoading={this.state.isLoading} /&gt;<br>    );<br>  }<br>}</pre><p>MyContainer sets initialState <em>isLoading</em> to true and renders MyComponent passing <em>isLoading</em> as props. A second after the component is mounted, it will set <em>isLoading</em> to false and re-renders MyComponent.</p><p>Yay! We have created component which initially renders loader.</p><p>Let’s re-write it using Higher Order Component Pattern.</p><p><strong>MyComponent</strong></p><pre>const MyComponent = (props) =&gt; {<br>  return &lt;div&gt;Hey! I&#39;m actual Content&lt;/div&gt;<br>}</pre><p>MyComponent only renders the actual Content.</p><p><strong>MyHOC</strong></p><pre>const MyHOC = (WrappedComponent) =&gt; {<br>    return (props) =&gt; {<br>    if(props.isLoading) {<br>      return &lt;div&gt;Hey! I&#39;m Loading&lt;/div&gt;;<br>    }<br>    return &lt;WrappedComponent {...props} /&gt;<br>  }<br>}</pre><p>MyHOC is a function that takes wrappedComponent as an argument and check for <em>isLoading</em> props. if <em>isLoading</em> is true it will return <strong><em>Hey! I’m Loading </em></strong>else it will return the wrappedComponent that has been passed.</p><p><strong>MyComponentWithHOC</strong></p><pre>const MyComponentWithHOC = MyHOC(MyComponent);</pre><p>MyComponentWithHOC will store the returned value (in this case a component) from MyHOC function which takes MyComponent as an argument.</p><p><strong>MyContainer</strong></p><pre>class MyContainer extends Component {<br>  state = {<br>    isLoading: true,<br>  }</pre><pre>componentDidMount() {<br>    setTimeout(() =&gt; {<br>      this.setState((state) =&gt; ({<br>        isLoading: !state.isLoading<br>      }))<br>    }, 1000);<br>  }</pre><pre>render() {<br>    return (<br>      &lt;MyComponentWithHOC isLoading={this.state.isLoading} /&gt;<br>    );<br>  }<br>}</pre><p>And Finally MyContainer does every thing else same except it renders MyComponentWithHOC instead of MyComponent.</p><p>Now, let’s take a deeper look into the above code to understand what actually is happening inside it. At first we have …</p><pre>const MyComponentWithHOC = MyHOC(MyComponent);</pre><p>When we execute <strong>MyHOC(MyComponent)</strong>,<strong> </strong>it will return a component and assign it to MyComponentWithHOC as shown below.</p><pre>const MyComponentWithHOC = (props) =&gt; {<br>    if(props.isLoading) {<br>      return &lt;div&gt;Hey! I&#39;m Loading&lt;/div&gt;;<br>    }<br>    return &lt;MyComponent {...props} /&gt;<br>  };</pre><p>Note that the above code has replaced WrappedComponent to MyComponent as it is passed to MyHOC function. You can see<strong> {…props}</strong> in the above code which means passing each and every props to MyComponent. If you are not familiar with it take a look into <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread operator</a>.</p><p>When MyComponent is rendered it will return</p><pre>&lt;div&gt;Hey! I&#39;m actual Content&lt;/div&gt;</pre><p>Now let’s replace MyComponent with it’s returned value.</p><pre>const MyComponentWithHOC = (props) =&gt; {<br>    if(props.isLoading) {<br>      return &lt;div&gt;Hey! I&#39;m Loading&lt;/div&gt;;<br>    }<br>    return &lt;div&gt;Hey! I&#39;m actual Content&lt;/div&gt;<br>  };</pre><p>You may have noticed that the final MyComponentWithHOC is almost same as MyComponent that we wrote in the beginning. Now you know that how it actually works. From now on you don’t have to check for <em>isLoading</em> in every component, simply pass it to MyHOC.</p><p>Until now you might already have figured out why and where we have to create HOC. One of the mostly used example of HOC is <em>connect()</em> function of <a href="https://github.com/reactjs/react-redux">react-redux</a>.</p><blockquote>Finally, if you have a generalized pattern in many components, just create a HOC and wrap those components with it.</blockquote><p>Thank you for taking time to read. Here is the <a href="https://jsfiddle.net/duwalanise/69z2wepo/94438/">link</a> to the fiddle of the example above.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f7844f6677ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>