<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Render Props - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Render Props - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 27 Jul 2026 04:22:44 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/render-props/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Re-Render React Component When Its Props Changes ]]>
                </title>
                <description>
                    <![CDATA[ Imagine you have a React and Redux project with two components, a parent and a child. The parent component passes some props to the child component. When the child component receives those props, it should call some Redux action which changes some of... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/re-render-react-component-when-its-props-changes/</link>
                <guid isPermaLink="false">66c35d4f56d1719c395f31a2</guid>
                
                    <category>
                        <![CDATA[ components ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Render Props ]]>
                    </category>
                
                    <category>
                        <![CDATA[ toothbrush ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Tue, 02 Jun 2020 02:13:00 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9aaa740569d1a4ca26f9.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Imagine you have a React and Redux project with two components, a parent and a child.</p>
<p>The parent component passes some props to the child component. When the child component receives those props, it should call some Redux action which changes some of the props that were passed from the parent asynchronously.</p>
<p>Here are the two components:</p>
<h3 id="heading-parent-component">Parent Component</h3>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Parent</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
  getChilds(){
    <span class="hljs-keyword">let</span> child = [];
    <span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>;
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> keys <span class="hljs-keyword">in</span> <span class="hljs-built_in">this</span>.props.home.data) {
      child[i] = (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Child</span> <span class="hljs-attr">title</span>=<span class="hljs-string">{keys}</span> <span class="hljs-attr">data</span>=<span class="hljs-string">{this.props.home.data[keys]}</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{keys}</span> /&gt;</span></span>
      );
      i++;
      <span class="hljs-keyword">if</span> (i === <span class="hljs-number">6</span>) <span class="hljs-keyword">break</span>;
    }

    <span class="hljs-keyword">return</span> Rows;
  }
  render(){
<span class="hljs-keyword">return</span> (
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>I am gonna call my child <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    {this.getChilds()}
 <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
)
</code></pre>
<h3 id="heading-child-component">Child Component</h3>
<pre><code class="lang-jsx"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Child</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">React</span>.<span class="hljs-title">Component</span> </span>{
 componentDidMount(){
  <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.props.data.items.length === <span class="hljs-number">0</span>){
    <span class="hljs-comment">// calling an action to fill this.props.data.items array with data</span>
   <span class="hljs-built_in">this</span>.props.getData(<span class="hljs-built_in">this</span>.props.data.id);
  }
 }
  getGrandSon(){
  <span class="hljs-keyword">let</span> grandSons = [];
  <span class="hljs-keyword">if</span>(<span class="hljs-built_in">this</span>.props.data.items.length &gt; <span class="hljs-number">0</span>){
   grandSons = <span class="hljs-built_in">this</span>.props.data.items.map( <span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">GrandSon</span> <span class="hljs-attr">item</span>=<span class="hljs-string">{item}</span> /&gt;</span></span>);
  }
  <span class="hljs-keyword">return</span> grandSons;
 }
  render(){
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span> I am the child component and I will call my own child <span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      {this.getGrandSon()}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
     )
 }
}
</code></pre>
<p>The <code>redux-store</code> is updated properly, but the child component doesn't re-render.</p>
<p>It's normally not the responsibility of the <code>Child</code> to fill the data. Instead, it should receive data that's already been prepared by the <code>Parent</code>. Also, props are automatically updated.</p>
<p>Still, it can be useful to update and manipulate data from a <code>Child</code> component, especially when Redux is involved.</p>
<p>React is probably performs shallow comparisons, and might not re-render even though the state is clearly changing.</p>
<p>As a workaround, you can do this in <code>mapStateToProps</code>:</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">const</span> mapStateToProps = <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> {
  <span class="hljs-keyword">return</span> { 
    <span class="hljs-attr">id</span>: state.data.id,
   <span class="hljs-attr">items</span>: state.data.items
  }
}
</code></pre>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to track page visibility in React using render props ]]>
                </title>
                <description>
                    <![CDATA[ By Soumyajit Pathak In this article, we will create a simple reusable React component that tracks “Page Visibility State.” When creating a web application you may come across situations where you need to track the current visibility state of the app.... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-track-page-visibility-in-react-using-render-props-b895537d62f7/</link>
                <guid isPermaLink="false">66c35523c7095d76345eaf66</guid>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Render Props ]]>
                    </category>
                
                    <category>
                        <![CDATA[ technology ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 05 Sep 2018 22:40:51 +0000</pubDate>
                <media:content url="https://cdn-media-1.freecodecamp.org/images/1*WsI4NrSVmdM-xjYDPa-Wgw.jpeg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Soumyajit Pathak</p>
<p>In this article, we will create a simple reusable React component that tracks “Page Visibility State.”</p>
<p>When creating a web application you may come across situations where you need to track the current visibility state of the app. You may need to play/pause a video or animation effect, throttle some performance intensive work or simply track the user’s behaviour for analytics based on whether the browser tab is active or not.</p>
<p>Now, this feature seems pretty simple to implement until you actually try to implement it for the first time. It turns out tracking whether the user is actively interacting with the web application or not can be quite tricky.</p>
<p>There is a Page Visibility API which works fine for most cases, but it does not handle all the possible cases of browser tab inactivity. The <a target="_blank" href="https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API"><strong>Page Visibility API</strong></a> that sends a <code>visibilitychange</code> event to let listeners know that the visible state of the page has changed, or has some irregularities. It doesn't fire the event in some of the cases even when the window or concerned browser tab is out of sight or out of focus.</p>
<p>To handle some of these edge cases, we need to use a combination of <code>focus</code> and <code>blur</code> event listeners on both the <code>document</code> and the <code>window</code> element. You can find a detailed discussion about it <a target="_blank" href="https://stereologics.wordpress.com/2015/04/02/about-page-visibility-api-hidden-visibilitychange-visibilitystate/">here</a>.</p>
<p>We will implement the workaround logic described in the tutorial mentioned above in a small React app. Don’t worry you can read it later — we will explain every aspect of the logic that we will be using.</p>
<p><a target="_blank" href="https://codesandbox.io/embed/81x9n78qmj"><strong>CodeSandbox</strong></a><br><a target="_blank" href="https://codesandbox.io/embed/81x9n78qmj">_CodeSandbox is an online editor tailored for web applications._codesandbox.io</a></p>
<p>If you want to dive into the code or watch the demo, it is available on <a target="_blank" href="https://codesandbox.io/s/81x9n78qmj"><strong>Codesandbox</strong></a> as well as <a target="_blank" href="https://github.com/drenther/react-page-visibility-example"><strong>Github</strong></a>.</p>
<h3 id="heading-getting-started">Getting Started</h3>
<p><img src="https://cdn-media-1.freecodecamp.org/images/1*SZoc6eC41JBxJSQ_xODPrA.gif" alt="Image" width="1254" height="588" loading="lazy">
<em>The video pauses when it is in the background</em></p>
<p>We will use Codesandbox to bootstrap our React application (you can use <strong>create-react-app</strong> as well). We will create a small app that will have an HTML5 Video element that will play only when the browser tab is in focus or active otherwise it will be paused automatically. We are using a video because it will make testing our app’s functionality easy.</p>
<p>Let’s start by creating the simplest piece i.e. the <code>Video</code> component. It will be a simple component that will receive a Boolean props named <code>active</code> and a String props named <code>src</code> that will hold the URL for the video. If the <code>active</code> props is true, then we will play the video. Otherwise we will pause it.</p>
<p>We will create a simple React class component. We will render a simple video element with its source set to the URL passed using the <code>src</code> props, and we’ll use React's new <code>ref</code> API to attach a <code>ref</code> on the video DOM node. We will set the video to autoplay, assuming when we start the app the page will be active.</p>
<p>One thing to note here is that Safari now doesn't allow auto-playing media elements without user interaction. The <code>componentDidUpdate</code> lifecycle method is very handy in handling side effects when a component's props change. Therefore, here we will use this lifecycle method to play and pause the video based on the current value of <code>this.props.active</code>.</p>
<p>Browser vendor prefix differences are very annoying to deal with when using certain APIs, and the Page Visibility API is certainly one of them. Therefore, we will create a simple utility function that will handle these differences and return us the compatible API based on the user’s browser in a uniform manner. We will create and export this small function from <strong>pageVisibilityUtils.js</strong> under the <strong>src</strong> directory.</p>
<p>In this function, we will utilize simple if-else statement based control flow to return the browser-specific API. You can see we attached the <strong>ms</strong> prefix for Internet Explorer and <strong>webkit</strong> prefix for Webkit browsers. We will store the correct API in <code>hidden</code> and <code>visibilityChange</code> string variables and return them from the function in the form of a simple object. Lastly, we will export the function.</p>
<p>Next, we move onto our main component. We will encapsulate all of our Page Visibility tracking logic in a reusable React class component by leveraging the <strong>Render Props</strong> pattern. We will create a class component called <code>VisibilityManager</code>. This component will handle the adding and removing of all the DOM based event listeners.</p>
<p>We will start by importing the utility function we created earlier and invoking it to get the correct browser specific APIs. Then, we will create the React component and initialize its state with a single field <code>isVisible</code> set to <code>true</code>. This Boolean state field will be responsible for reflecting our page visibility state.</p>
<p>In the component's <code>componentDidMount</code> lifecycle method, we will attach an event listener on the document for the <code>visibilitychange</code> event with the <code>this.handleVisibilityChange</code> method as its handler. We will also attach event listeners for the focus and blur events on the document as well as the window element. This time we will set <code>this.forceVisibilityTrue</code> and <code>this.forceVisibilityFalse</code> as the handlers for the focus and blur events, respectively.</p>
<p>Next, we will create the <code>handleVisibilityChange</code> method that will accept a single argument <code>forcedFlag</code>. This <code>forceFlag</code> argument will be used to determine whether the method is called because of the <code>visibilitychange</code> event or the focus or blur events. This is so because the <code>forceVisibilityTrue</code> and <code>forceVisibilityFalse</code> methods do nothing but call the <code>handleVisibilityChange</code> method with true and false value for the <code>forcedFlag</code> argument.</p>
<p>Inside the <code>handleVisibilityChange</code> method, we first check whether the <code>forcedFlag</code> argument value is a Boolean (this is because if it is called from the <code>visibilitychange</code> event handler, than the argument passed on will be a <a target="_blank" href="https://reactjs.org/docs/events.html"><strong>SyntheticEvent</strong></a> object).</p>
<p>If it is a Boolean then we check if it's true or false. When it's true we called the <code>setVisibility</code> method with true otherwise we call the method with false as an argument. The <code>setVisibility</code> method leverages <code>this.setState</code> method to update <code>isVisible</code> field's value in the component's state.</p>
<p>But, if the <code>forcedFlag</code> is not a Boolean, then we check the hidden attribute value on the document and call the <code>setVisibility</code> method accordingly. This wraps up our Page Visibility State tracking logic.</p>
<p>To make the component reusable in nature, we use the Render Props pattern. That is, instead of rendering a component from the <code>render</code> method, we invoke <code>this.props.children</code> as a function with <code>this.state.isVisible</code>.</p>
<p>Lastly, we mount our React app to the DOM in our <strong>index.js</strong> file. We import our two React components <code>VisibilityManager</code> and <code>Video</code> and create a small functional React component <code>App</code> by composing them. We pass a function as the children of the <code>VisibilityManager</code> component that accepts <code>isVisible</code> as an argument and passes it to the <code>Video</code> component in its return statement. We also pass a video URL as <code>src</code> props to the <code>Video</code> component. This is how we consume the Render Props based <code>VisiblityManager</code> component. Finally, we use <code>ReactDOM.render</code> method to render our React app on the DOM node with id "root".</p>
<h3 id="heading-conclusion">Conclusion</h3>
<p>Modern browser APIs are getting really powerful and can be used to do amazing things. Most of these APIs are imperative in nature and can be tricky to work with sometimes in React’s declarative paradigm. Using powerful patterns like Render Props to wrap these APIs into their own reusable React components can be very useful.</p>
<p><em>Originally published at <a target="_blank" href="https://able.bio/drenther/track-page-visibility-in-react-using-render-props--78o9yw5">able.bio</a>.</em></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
