<?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 Ashwin M on Medium]]></title>
        <description><![CDATA[Stories by Ashwin M on Medium]]></description>
        <link>https://medium.com/@glocore?source=rss-2ed986b4bdb7------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gGh5zxd3ceJbegcmwfuRyA.png</url>
            <title>Stories by Ashwin M on Medium</title>
            <link>https://medium.com/@glocore?source=rss-2ed986b4bdb7------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:13:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@glocore/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[Debouncing Functions in React Components]]></title>
            <link>https://medium.com/@glocore/debouncing-functions-in-react-components-d42f5b00c9f5?source=rss-2ed986b4bdb7------2</link>
            <guid isPermaLink="false">https://medium.com/p/d42f5b00c9f5</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Ashwin M]]></dc:creator>
            <pubDate>Thu, 28 Sep 2017 14:35:38 GMT</pubDate>
            <atom:updated>2017-09-30T17:29:45.665Z</atom:updated>
            <content:encoded><![CDATA[<h4>Write efficient functions attached to DOM events without ruining user experience</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LEtkOUeDhYaI3IgIpz_mSQ.jpeg" /><figcaption><em>My first blog post ever, Yay!</em></figcaption></figure><p>Attaching a function to certain DOM events such as page scroll, window resize or changing text in an input field can prove to be costly, since they can be triggered several times within a short span of time.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fdcorb%2Fembed%2Fpreview%2FPZOZgB%3Fheight%3D600%26slug-hash%3DPZOZgB%26default-tabs%3Dcss%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fdcorb%2Fpen%2FPZOZgB&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F143454.PZOZgB.small.d2bc6869-0a6e-4a45-ad5c-aeb20750eb11.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/af538b233c47e3a65e0a64c0fa8e38f3/href">https://medium.com/media/af538b233c47e3a65e0a64c0fa8e38f3/href</a></iframe><p>Now unless you literally need to count the number of DOM events, it should suffice to trigger the function after the scrolling or window resizing has stopped. In order to limit the rate at which the function is fired, a <a href="https://davidwalsh.name/javascript-debounce-function">debounce function</a> can come in handy.</p><p>You can either write your own debounce function, or use one from a utility library such as lodash or underscore.js. I’ll be using _.debounce() <a href="https://lodash.com/docs/4.17.4#debounce">from lodash</a>.</p><h3>How it’s normally used</h3><p>Here’s an example of how you would normally use _.debounce in vanilla JS:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1f59355202f14c3a5cb3252e9a98128/href">https://medium.com/media/f1f59355202f14c3a5cb3252e9a98128/href</a></iframe><p>In most cases, you would need to pass it only two parameters:</p><ul><li>The function to be rate-limited</li><li>The rate limit in milliseconds, ie., the time to wait before the function is fired</li></ul><p>The above code will print hello in the console 500 milliseconds after the user has stopped scrolling, instead of printing it at every scroll event.</p><p>Simple enough? This should be easy to implement in a React component too, right? Well, it’s not that straightforward…</p><h3>React’s SyntheticEvent</h3><p>The following code won’t work:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd2537cb422642a017f3c837e83b5b2e/href">https://medium.com/media/dd2537cb422642a017f3c837e83b5b2e/href</a></iframe><p>This is because in React, event handlers are passed instances of <a href="https://facebook.github.io/react/docs/events.html">SyntheticEvent</a>, which is a wrapper over the native browser events. This SyntheticEvent object is ‘pooled’, meaning it will be cleared once the event callback function has been invoked.</p><p>In order to prevent the properties of the SyntheticEvent object from being erased, we include the persist() method. That way, our event will never be pooled.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fplatonish%2Fembed%2Fpreview%2FVMbYPW%3Fheight%3D600%26slug-hash%3DVMbYPW%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fplatonish%2Fpen%2FVMbYPW&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F985709.VMbYPW.small.8c74196e-7c28-4907-aecc-294045bb3143.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec0761e6196dd13713b31f0088eca6b0/href">https://medium.com/media/ec0761e6196dd13713b31f0088eca6b0/href</a></iframe><p>Another use case would be if you were to implement a simple search box which made AJAX calls in real-time based on the contents of the input field. Here’s an example fetching search results from the Reddit API. I’ve used <a href="https://github.com/mzabriskie/axios">axios</a> for making the HTTP requests.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fcodepen.io%2Fplatonish%2Fembed%2Fpreview%2FjGmPrW%3Fheight%3D600%26slug-hash%3DjGmPrW%26default-tabs%3Djs%2Cresult%26host%3Dhttps%3A%2F%2Fcodepen.io%26embed-version%3D2&amp;url=https%3A%2F%2Fcodepen.io%2Fplatonish%2Fpen%2FjGmPrW&amp;image=https%3A%2F%2Fs3-us-west-2.amazonaws.com%2Fi.cdpn.io%2F985709.jGmPrW.small.7f2c86a2-c0dd-441d-96f4-8c5166d347bf.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=codepen" width="800" height="600" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb4b8f01d3ecaff32c3de051e4255c1f/href">https://medium.com/media/eb4b8f01d3ecaff32c3de051e4255c1f/href</a></iframe><p>Throttling is a similar technique that allows us to limit the number of times a function is executed to once every X milliseconds. So say, if a throttled function is attached to a scroll event, it will be executed every X milliseconds while the user scrolls the page, instead of waiting for the user to stop scrolling entirely. If you are new to debouncing/throttling, I highly recommend reading <a href="https://css-tricks.com/debouncing-throttling-explained-examples/">this article</a>, which explains them with some common use cases.</p><p><em>Cheers :)</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d42f5b00c9f5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>