<?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 Sandeep Rawat on Medium]]></title>
        <description><![CDATA[Stories by Sandeep Rawat on Medium]]></description>
        <link>https://medium.com/@sandeep-dev?source=rss-76e4ee5ed0f0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*HHEMT8dlTLD5bLux.jpg</url>
            <title>Stories by Sandeep Rawat on Medium</title>
            <link>https://medium.com/@sandeep-dev?source=rss-76e4ee5ed0f0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 03:33:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sandeep-dev/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[Promise polyfill with chaining]]></title>
            <link>https://medium.com/time-machine/promise-polyfill-with-chaining-e7204c3448d9?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e7204c3448d9</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[promises-in-javascript]]></category>
            <category><![CDATA[polyfill]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[promises]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Thu, 20 Mar 2025 19:41:42 GMT</pubDate>
            <atom:updated>2025-03-20T19:41:42.113Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*XIkgivV1T77Y7a2HgRxfmQ.png" /></figure><p>A Promise polyfill with <strong>chaining support</strong> is a great combination of <strong>closures, </strong><strong>this context, prototype, and constructor function</strong>.</p><p>Without chaining support, building a Promise polyfill is relatively easier. However, implementing <strong>chaining support</strong> makes it more complex and requires a deeper understanding of how Promises work.</p><p>In this blog, I will describe <strong>how to create a Promise polyfill with chaining support</strong>, step by step.</p><pre>// Step 1<br>function CPromise (cb) {<br>  // Step 3<br>  this.cbList = [];<br>  const that = this;<br>  <br>  // Step 6<br>  function callTillEnd (type, data) {<br>      let item = {};<br>      while (that.cbList.length &gt; 0) {<br>        item = that.cbList.shift();<br>        if (item.type === &#39;finally&#39;) {<br>          item.cb();<br>          item = {};<br>        }<br>        <br>        if (item.type === type) {<br>         try {<br>           data = item.cb(data);<br>            type = &quot;then&quot;;<br>           } catch (e) {<br>            type = &quot;catch&quot;;<br>            data = e;<br>           }<br>        }<br>      }<br>  }<br>  <br>  // Step 4<br>  function resolve (data) {<br>    callTillEnd(&#39;then&#39;, data);<br>  }<br>  <br>  // Step 4<br>  function reject (error) {<br>    callTillEnd(&#39;catch&#39;, error);<br>  }<br>  <br>  // Step 5<br>  requestAnimationFrame(() =&gt; {<br>    cb(resolve, reject);<br>  });<br>}<br><br>// Step 2<br>CPromise.prototype.then = function (cb) {<br>  this.cbList.push({<br>    type: &#39;then&#39;,<br>    cb<br>  });<br>  <br>  return this;<br>}<br><br>// Step 2<br>CPromise.prototype.catch = function (cb) {<br>  this.cbList.push({<br>    type: &#39;catch&#39;,<br>    cb<br>  });<br>  <br>  return this;<br>}<br><br>// Step 2<br>CPromise.prototype.finally = function (cb) {<br>  this.cbList.push({<br>    type: &#39;finally&#39;,<br>    cb<br>  });<br>  <br>  return this;<br>}</pre><h3>Step 1:</h3><p><strong>Defining a constructor function </strong><strong>CPromise</strong> – Since a promise is instantiated using the new keyword, it requires a constructor function.</p><h3>Step 2:</h3><p><strong>Adding core Promise functions to the prototype</strong> — Using the prototype ensures that functions are not redefined for every instance, saving memory.</p><h3>Step 3:</h3><p><strong>Creating a list to store callbacks</strong> — This list will store all callbacks (then, catch, finally) for later execution.</p><h3>Step 4:</h3><p><strong>Defining </strong><strong>resolve and </strong><strong>reject functions</strong> – These functions will be passed as arguments to the Promise callback.</p><h3>Step 5:</h3><p><strong>Executing the Promise callback inside </strong><strong>requestAnimationFrame</strong> – This ensures the first callback runs asynchronously. While setTimeout could be used, requestAnimationFrame is preferred due to its higher priority.</p><h3>Step 6:</h3><p><strong>Triggering stored callbacks (</strong><strong>then/catch/finally)</strong> – Once resolve or reject is called, all stored callbacks should execute based on the return values from then or catch.</p><h3>Uses:</h3><pre>const p = new CPromise((resolve, reject) =&gt; {<br>    resolve(&quot;S1: Sandeep&quot;);<br>});<br><br>p.then((data) =&gt; {<br>  console.log(data);<br>  return &#39;S2: Frontend - Bachpan ka Pyaar&#39;;<br>}).catch ((e) =&gt; {<br>  console.log(e);<br>  throw new Error(&quot;E1: Backend - Maturity wala Pyaar&quot;);<br>}).then((data) =&gt; {<br>  console.log(data);<br>  throw new Error(&quot;E2: Backend - Maturity wala Pyaar&quot;);<br>}).catch ((e) =&gt; {<br>  console.log(e.message);<br>  return &quot;S3: Frontend - Bachpan ka Pyaar&quot;;<br>}).finally(() =&gt; {<br>  console.log(&quot;F1: Love both&quot;);<br>}).then((data) =&gt; {<br> console.log(data);<br>}).finally(() =&gt; {<br>  console.log(&quot;F2: The End&quot;);<br>});</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e7204c3448d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/promise-polyfill-with-chaining-e7204c3448d9">Promise polyfill with chaining</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Craft your no internet/404 web page]]></title>
            <link>https://medium.com/time-machine/craft-your-no-internet-404-web-page-6ddeb3056411?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ddeb3056411</guid>
            <category><![CDATA[page-not-found]]></category>
            <category><![CDATA[service-worker]]></category>
            <category><![CDATA[no-internet]]></category>
            <category><![CDATA[404-not-found]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Sun, 12 Mar 2023 12:36:47 GMT</pubDate>
            <atom:updated>2023-03-12T12:36:47.026Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*hFnyMGto5eCiBPF-asLfhg.jpeg" /></figure><p>Are you guys also get bore to see the browser default screen when your internet or web server is down? Mostly it feel not good when the web app opening inside a native app (web view). I also worked on multiple web app which was opened inside a native app. But whenever the internet or the server get down, the browser default error page come. That time the question come in our mind, can I change this page ?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*UKdBX06cE4OL5jVcEJiO-Q.gif" /></figure><p>Yes we can change this page, how ?</p><h3><strong>Service Worker:</strong></h3><p>Yes <a href="https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API">Service worker</a> is the solution of your question, You can find many articles on internet about the service worker in depth. The sort answer is Service worker is working as proxy between a web page and web server. So we can modify any network call from service worker. This is running on a different thread.</p><p>Lets change my default browser error page.</p><ol><li>Firstly we have you create a service worker file (<strong>sw.js</strong>) inside your root directory.</li></ol><figure><img alt="Frontend web directory" src="https://cdn-images-1.medium.com/max/970/1*3rV7k77URodYiRt6HIRZiA.png" /></figure><p>2. Copy and paste the code from <a href="https://raw.githubusercontent.com/rawatsandeep670/no-internet-web-craft/main/src/sw.js">https://github.com/rawatsandeep670/no-internet-web-craft/blob/main/src/sw.js</a> inside <strong>sw.js</strong> file</p><p>3. Create a html file <strong>no-internet.html</strong> inside your root directory and write the code which you want to so when internet or server will down. <strong>Ex:</strong></p><pre>&lt;!DOCTYPE html&gt;<br>&lt;html&gt;<br>    &lt;head&gt;<br>        &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot; /&gt;<br>        &lt;title&gt;No Internet Page&lt;/title&gt;<br><br>        &lt;style&gt;<br>            body {<br>                display: flex;<br>                justify-content: center;<br>                align-items: center;<br>                height: 100vh;<br>                background: #6fa7b7;<br>                color: #ffffff;<br>            }<br>        &lt;/style&gt;<br>    &lt;/head&gt;<br>    &lt;body&gt;<br>        You can craft your No Internet or 404 page here<br>    &lt;/body&gt;<br>&lt;/html&gt;</pre><p>4. Install <a href="https://www.npmjs.com/package/no-internet-web-craft">no-internet-web-craft</a> npm package:</p><pre>npm i no-internet-web-craft</pre><blockquote>Web page not support <strong>npm</strong> don’t worry you can also download library using script tag, Go through <a href="https://www.npmjs.com/package/no-internet-web-craft">https://www.npmjs.com/package/no-internet-web-craft</a> page.</blockquote><p>5. Initialise the <strong>NoInternetWebCraft </strong>constructor function.</p><pre>const webCraft = new NoInternetWebCraft(&#39;/no-internet.html&#39;, &#39;1.0.0&#39;)<br>webCraft.initWebSocket().then((res) =&gt; {<br>  if (res.status === &#39;success&#39;) {<br>    console.log(&#39;Service worker activated&#39;)<br>  } else {<br>    console.log(&#39;Service worker activation failed&#39;)<br>  }<br>})</pre><p>Send your no-internet.html file path and the version(version just for refreshing your new changes) to <strong>NoInternetWebCraft.</strong></p><blockquote><strong>Version:</strong> Whenever you want to change your no-internet.html design, you have to update the version then only your new changes will be shown.</blockquote><p>6. Finally the browser default error page has been modified.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*bxzc79PjcHnsIp9-4p--fA.gif" /></figure><blockquote><strong>Note:</strong> At least one time your web worker must be initialised, then only it will works.</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ddeb3056411" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/craft-your-no-internet-404-web-page-6ddeb3056411">Craft your no internet/404 web page</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Best way to auto cancel HTTP request in angular]]></title>
            <link>https://medium.com/time-machine/best-way-to-auto-cancel-http-request-in-angular-18835aa6e979?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/18835aa6e979</guid>
            <category><![CDATA[angularjs]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[decorators]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[http-client]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Mon, 05 Apr 2021 12:58:03 GMT</pubDate>
            <atom:updated>2021-04-15T19:22:00.838Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*OCOvhMCW5_LL0VDz" /><figcaption>Photo by <a href="https://unsplash.com/@markuswinkler?utm_source=medium&amp;utm_medium=referral">Markus Winkler</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Before reading this story, I believe you guys are from Angular 👨‍💻 background. As a Front-End developer, we guys are consuming Rest API’s in our daily job 🙋🏻‍♂️ and sometime we need to cancel the HTTP Request on some conditional basis like(On component destroy, on basis of request timeout or may be on multiple request of same API’s). If you also need to cancel HTTP request, This story for you 🏌🏻. Yup!!! This story about how to auto cancel the HTTP request on:</p><ol><li>Component Destroy</li><li>On request timeout value</li><li>Multiple request of same API</li></ol><p>I personally love to use decorators in angular/typescript, It’s also backbone of Angular application. So I go with method decorator to auto cancel HTTP request in above scenario.</p><p><strong>Follow the steps:</strong></p><p>Firstly creating a method decorator:</p><pre>export function ngAutoCancelable() {<br>    return function (target: any, propertyKey: string,    descriptor: PropertyDescriptor) {</pre><pre>.......<br>   return descriptor;<br>};}</pre><p>Created a <strong>ngAutoCancelable </strong>method decorator. Firstly we need to save the original function which targeted by decorator and override the function body.</p><pre>......</pre><pre>const original = descriptor.value;<br>const originalOnDestroy = target.ngOnDestroy;<br>let subscription: Subscription;</pre><pre>// override original function body<br>descriptor.value = function autCancelable(...args: any) {</pre><pre>// call original function<br>    subscription = original.apply(this, args);<br>}</pre><pre>.....</pre><p>Need to define <strong>ngOnDestroy</strong> method in component class, If component already define this method then save it for future use. I always prefer Object.defineProperty to define property of object.</p><pre>.......</pre><pre>Object.defineProperty(target, &quot;ngOnDestroy&quot;, {<br>  value: function ngOnDestroy() {<br>     if (subscription &amp;&amp; !subscription.closed) {<br>       subscription.unsubscribe();<br>     }</pre><pre>if (typeof originalOnDestroy === &#39;function&#39;) {<br>      originalOnDestroy.apply(this);<br>     }<br>  }<br>});</pre><pre>.....</pre><p>Above code is like, How to make <strong>ngAutoCancelable</strong> decorator in angular/typescript.</p><blockquote>I also make a NPM package for the same use case which covered some additional features like below.</blockquote><p>👉 Component Destroy</p><p>👉 On request timeout value</p><p>👉 Multiple request of same API</p><p><a href="https://www.npmjs.com/package/ng-auto-cancelable">ng-auto-cancelable</a></p><p>You can easily use it in your angular application:</p><pre>npm i ng-auto-cancelable</pre><p>Use case:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/546b52ae40dcc224e86b30d0ebe20882/href">https://medium.com/media/546b52ae40dcc224e86b30d0ebe20882/href</a></iframe><p>For more details:</p><p><a href="https://github.com/rawatsandeep670/ng-auto-cancelable#readme">rawatsandeep670/ng-auto-cancelable</a></p><p>This is also open source project, feel free to contribute. 👍</p><p>Happy Reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=18835aa6e979" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/best-way-to-auto-cancel-http-request-in-angular-18835aa6e979">Best way to auto cancel HTTP request in angular</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Angular HTTP request auto cancellation decorator]]></title>
            <link>https://medium.com/time-machine/angular-http-request-auto-cancelation-3331b300756?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3331b300756</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[decorators]]></category>
            <category><![CDATA[angular-http-service]]></category>
            <category><![CDATA[http-client]]></category>
            <category><![CDATA[angular]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Sun, 28 Mar 2021 19:17:30 GMT</pubDate>
            <atom:updated>2021-04-01T04:56:44.229Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Gk09ToxydE8NAbUw" /><figcaption>Photo by <a href="https://unsplash.com/@markuswinkler?utm_source=medium&amp;utm_medium=referral">Markus Winkler</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Before reading this story, I believe you guys are from Angular 👨‍💻 background. As a Front-End developer, we guys are consuming Rest API’s in our daily job 🙋🏻‍♂️ and sometime we need to cancel the HTTP Request on some conditional basis like(On component destroy, on basis of request timeout or may be on multiple request of same API’s). If you also need to cancel HTTP request, This story for you 🏌🏻. Yup!!! This story about how to auto cancel the HTTP request on:</p><ol><li>Component Destroy</li><li>On request timeout value</li><li>Multiple request of same API</li></ol><p>I personally love to use decorators in angular/typescript, It’s also backbone of Angular application. So I go with method decorator to auto cancel HTTP request in above scenario.</p><p><strong>Follow the steps:</strong></p><p>Firstly creating a method decorator:</p><pre>export function ngAutoCancelable() {<br>    return function (target: any, propertyKey: string,    descriptor: PropertyDescriptor) {</pre><pre>   .......<br>   return descriptor;<br>};}</pre><p>Created a <strong>ngAutoCancelable </strong>method decorator. Firstly we need to save the original function which targeted by decorator and override the function body.</p><pre>......</pre><pre>const original = descriptor.value;<br>const originalOnDestroy = target.ngOnDestroy;<br>let subscription: Subscription;</pre><pre>// override original function body<br>descriptor.value = function autCancelable(...args: any) {</pre><pre>    // call original function<br>    subscription = original.apply(this, args);<br>}</pre><pre>.....</pre><p>Need to define <strong>ngOnDestroy</strong> method in component class, If component already define this method then save it for future use. I always prefer Object.defineProperty to define property of object.</p><pre>.......</pre><pre>Object.defineProperty(target, &quot;ngOnDestroy&quot;, {<br>  value: function ngOnDestroy() {<br>     if (subscription &amp;&amp; !subscription.closed) {<br>       subscription.unsubscribe();<br>     }</pre><pre>     if (typeof originalOnDestroy === &#39;function&#39;) {<br>      originalOnDestroy.apply(this);<br>     }<br>  }<br>});</pre><pre>.....</pre><p>Above code is like, How to make <strong>ngAutoCancelable</strong> decorator in angular/typescript.</p><blockquote>I also make a NPM package for the same use case which covered some additional features like below.</blockquote><p>👉 Component Destroy</p><p>👉 On request timeout value</p><p>👉 Multiple request of same API</p><p><a href="https://www.npmjs.com/package/ng-auto-cancelable">ng-auto-cancelable</a></p><p>You can easily use it in your angular application:</p><pre>npm i ng-auto-cancelable</pre><p>Use case:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/546b52ae40dcc224e86b30d0ebe20882/href">https://medium.com/media/546b52ae40dcc224e86b30d0ebe20882/href</a></iframe><p>For more details:</p><p><a href="https://github.com/rawatsandeep670/ng-auto-cancelable#readme">rawatsandeep670/ng-auto-cancelable</a></p><p>This is also open source project, feel free to contribute. 👍</p><p>Happy Reading :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3331b300756" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/angular-http-request-auto-cancelation-3331b300756">Angular HTTP request auto cancellation decorator</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Do you know why React is more secure?]]></title>
            <link>https://medium.com/time-machine/do-you-know-why-react-is-more-secure-5f3167be80b?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/5f3167be80b</guid>
            <category><![CDATA[events]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Tue, 22 Dec 2020 18:59:17 GMT</pubDate>
            <atom:updated>2020-12-22T19:04:15.569Z</atom:updated>
            <content:encoded><![CDATA[<h3>Do you know, why React is more secure?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*oHoQG1cvBJoZ7ivo" /><figcaption>Photo by <a href="https://unsplash.com/@jamessutton_photography?utm_source=medium&amp;utm_medium=referral">James Sutton</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>React is more secure than other JS libraries or frameworks 🙈. Also we know React play with Virtual DOM instead of Real DOM but some time we don’t know about what is the benefit of Virtual DOM. Why React create Virtual DOM. Many more interesting facts available on internet about React Virtual DOM. Today, I show you a real example from one of them. Why React is more secure than other JS libraries? Yes, it’s because of Virtual DOM. 😎</p><p>So, Let’s discussed about <strong>Event Handling</strong>. Events handling are most interesting stuff in Javascript and I don’t think any website will be going so far without it.</p><p>Also,<strong> Event Validation</strong> are most important thing when we guys are working with Events. <strong>Event Validation</strong>😶 Like, Before submitting any <strong>Form</strong> we guys check form valid or not Or check <strong>Buttons </strong>element Enable/Disable.</p><blockquote>Checking event valid or not from inside its callback function is boring but YES, still we guys are doing this. Because we fear about, if any user manipulate the DOM from <em>browser console</em>🎭, than what 😣 our events will be fire. That’s why we are recheck the validation. But If you use React, no need to recheck or worried about DOM manipulation.</blockquote><p>I can show you a case when I removed <em>disabled</em> attribute from element and event will fire. This is Real DOM case.</p><figure><img alt="Event validation on Real DOM" src="https://cdn-images-1.medium.com/max/640/1*TLuqwU2cWihMUWuKqVsJyg.gif" /></figure><blockquote>Check Live: <a href="https://codesandbox.io/s/unruffled-swartz-0hqg1">https://codesandbox.io/s/unruffled-swartz-0hqg1</a></blockquote><p>Now, I will do the same thing in React.</p><figure><img alt="Event validation on Virtual DOM" src="https://cdn-images-1.medium.com/max/640/1*UhPlLnqpgNBG6yWMIJCkmA.gif" /></figure><blockquote>Check Live: <a href="https://codesandbox.io/s/kind-curran-x05c2">https://codesandbox.io/s/kind-curran-x05c2</a></blockquote><p>OOh! OOh!! 😳 This is the power of Virtual DOM. No need to recheck, is <em>button</em> disabled or not.</p><p>The whole point is, If anyone manipulate the DOM manually, it will not affected to the React Virtual DOM. So the final conclusion is, React check Virtual DOM whenever any <a href="https://reactjs.org/docs/events.html">SyntheticEvent</a> Event fired.</p><p>Happy Coding 🙏</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5f3167be80b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/do-you-know-why-react-is-more-secure-5f3167be80b">Do you know why React is more secure?</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Take care of using Arrow functions as react props]]></title>
            <link>https://medium.com/time-machine/do-not-pass-arrow-functions-as-props-of-react-purecomponent-c01da1af38ee?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c01da1af38ee</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[purecomponent]]></category>
            <category><![CDATA[props-and-state]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Sat, 18 Apr 2020 06:18:16 GMT</pubDate>
            <atom:updated>2020-04-24T14:30:50.191Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="Pure are Pure" src="https://cdn-images-1.medium.com/max/1024/0*c5ZFaCCsJfs93sGN" /><figcaption>Photo by <a href="https://unsplash.com/@boxedwater?utm_source=medium&amp;utm_medium=referral">Boxed Water Is Better</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hello guys, Nowadays I learn and also working on React JS 😃. So today I will discuss about, when we use <strong>Arrow method</strong> or when <strong>bind</strong>. I am also a beginner in the ReactJS community, any mistake please check me.</p><p>As usual, I also write components like you, We all make stateful component using <strong>React.Component</strong> or <strong>React.PureComponent </strong>classes. So firstly, I brief about <strong>React.Component</strong> vs <strong>React.PureComponent</strong>. If you already known, jump down to the next section.</p><p><strong>React.Component</strong>: Simply!!! Components are special type of functions/classes which execute the Business logic(JS) and View(JSX/HTML/CSS) part together. Read more about <a href="https://reactjs.org/docs/components-and-props.html">Components</a>.</p><p><strong>Rect.PureComponent:</strong> PureComponent also does the same thing as Component. So why it differs from <strong>React.Component</strong> class 🤔? Ok Ok, When I searched it on the internet, I found the difference between those.</p><blockquote>React.PureComponent is similar to React.Component. The difference between them is that <strong>React.Component doesn’t implement </strong><a href="https://reactjs.org/docs/react-component.html#shouldcomponentupdate"><strong>shouldComponentUpdate()</strong></a><strong>, but </strong><strong>React.PureComponent implements it with a shallow prop and state comparison</strong>.😕 For more check <a href="https://reactjs.org/docs/react-api.html#reactpurecomponent"><em>PureComponent</em></a><em>.</em></blockquote><p>Ok firstly, I discuss about <strong>shouldComponentUpdate</strong> method: As it’s naming convention, this method decides to component will update or not. In the case of <strong>React.PureComponent,</strong> ReactJS implement <strong>shouldComponentUpdate</strong> method by default but not with <strong>React.Component</strong>.</p><p><strong>Now understand, Shallow prop and state comparison</strong>: Shallow comparison worked with two different types:</p><ol><li><strong>Primitives:</strong> React check <em>value</em> in case of primitive types. Ex.</li></ol><pre>var previous = 10;<br>var current = 10;<br>previous === current // true, do not update component</pre><pre>var previous = 10;<br>var current = 12;<br>previous === current // false, update component</pre><p>2.<strong> Reference (Objects): </strong>In case of Reference variables, React checks only previous and current memory addresses of variables. Ex.</p><pre>var previous = {name: &#39;Sandeep&#39;};<br>previous.name = &#39;Pradeep&#39;;<br>var current = previous;<br>previous === current // true, do not update component</pre><pre>var previous = {name: &#39;Sandeep&#39;};<br>var current = {name: &#39;Sandeep&#39;};<br>previous === current // false, update component</pre><pre>// I believe above lines are not surprising for you, If YES🤓 <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators"><em>Please read first</em></a></pre><p>I believe you understand about <strong>Shallow comparison </strong>and <strong>shouldComponentUpdate </strong>method.</p><p><strong>React Change Detection</strong>: Whenever a component renders it’s child components also re-render while child component has no changes.😳 Prevent rendering otherwise, it creates performance issues. So take care of it. How?</p><p>Easy <strong>React.PureComponent </strong>takes care of it. If you don&#39;t understand about <strong>React.PureComponent </strong>read the above section.</p><p>When we have to use <strong>Arrow method</strong> and when use <strong>bind. </strong>According to me, whenever you make a component using <strong>React.PureComponent, </strong>never pass <strong>Arrow method</strong> in props. If you do it, you do not gain any benefits from <strong>PureComponent</strong>. See below Example:</p><pre>// <strong>Child component created using React.PureComponent</strong></pre><pre>// Passing Arrow function as props of Child component</pre><pre>class Parent extend React.Component {<br>   constructor() {}<br>    <br>   ...</pre><pre>   render() {<br>      return (<br>         &lt;Child callback={(val) =&gt; {}} /&gt;<br>      )<br>   }<br>}</pre><pre>// Passing function as props using <strong>bind</strong></pre><pre>class Parent extend React.Component {</pre><pre>   childCallbackRef = null;</pre><pre>   // define constructor<br>   constructor() {</pre><pre>      // return new reference of childCallback method with class this<br>      this.childCallbackRef = this.childCallback.bind(this);</pre><pre>   }</pre><pre>   childCallback(val) {<br>      ...<br>   }</pre><pre>   ...<br>   <br>   render() {<br>      return (<br>         &lt;Child callback={this.childCallbackRef} /&gt;<br>      )<br>   }</pre><p>See live example here:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstackblitz.com%2Fedit%2Farrow-function-pure-component%3Fembed%3D1%26file%3Dindex.js&amp;display_name=StackBlitz&amp;url=https%3A%2F%2Fstackblitz.com%2Fedit%2Farrow-function-pure-component%3Fembed%3D1%26file%3Dindex.js&amp;image=https%3A%2F%2Fc.staticblitz.com%2Fassets%2Ficon-664493542621427cc8adae5e8f50d632f87aaa6ea1ce5b01e9a3d05b57940a9f.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=stackblitz" width="745" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/3e3bede1bbd1e7d55904ec7585324802/href">https://medium.com/media/3e3bede1bbd1e7d55904ec7585324802/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c01da1af38ee" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/do-not-pass-arrow-functions-as-props-of-react-purecomponent-c01da1af38ee">Take care of using Arrow functions as react props</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pay attention when defining any property of an object]]></title>
            <link>https://medium.com/time-machine/be-alert-to-define-any-property-of-an-object-d0768fd31b52?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0768fd31b52</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[javascript-object]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript-development]]></category>
            <category><![CDATA[es6]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Tue, 25 Feb 2020 18:35:15 GMT</pubDate>
            <atom:updated>2020-02-25T18:57:19.551Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*2GxDY-llsLfMm-Zj" /><figcaption>Photo by <a href="https://unsplash.com/@eugenezhyvchik?utm_source=medium&amp;utm_medium=referral">Eugene Zhyvchik</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In javascript, we create many objects (literal, prototype or other ways) to complete our daily basis task. Normally we create an object and define property like:</p><p><em>Object literal</em> way:</p><pre>const sum = {<br>    num1: 0,<br>    num2: 0,<br>    getSum: function() {<br>        return (this.a + this.b)<br>    }<br>};</pre><pre>sum.num1 = 10;<br>sum.num2 = 20;<br>console.log(sum.getSum());</pre><p><em>Prototype</em> way:</p><pre>function Sum() {<br>    this.num1 = 0;<br>    this.num2 = 0;<br>}</pre><pre>Sum.prototype.getSum = function() {<br>    return (this.num1 + this.num2);<br>}</pre><pre>// create constructor of function<br>var sObj= new Sum();<br>sObj.num1 = 10;<br>sObj.num2 = 20;<br>console.log(sObj.getSum());</pre><p>Be more alert when defining the property of any object. don’t always define all property directly like above.</p><p>We all use <strong>for...in</strong> loop to iterate over objects. If I iterate over <strong>sum </strong>object using <strong>for...in</strong> loop, the loop runs 3 times. Let’s go with an example.</p><p>I want to iterate over <strong>sum</strong> object using <strong>for...in</strong> loop:</p><pre>for (const key in sum) {<br>    console.log(&#39;key = &#39;, key);<br>}</pre><pre>// Output<br>key = num1     // 1st count<br>key = num2     // 2nd count<br>key = getSum   //3rd count</pre><p>Even the same output found with <strong>prototype</strong> case. So don’t define every property directly to object it’s delaying your iteration.</p><p>Now the better way to define any property (spacially functions) of objects using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty"><strong>Object.defineProperty</strong></a><strong> </strong>or <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties"><strong>Object.defineProperties</strong></a>.</p><pre>function Sum() {<br>    this.num1 = 0;<br>    this.num2 = 0;<br>}</pre><pre>// use Object.defineProperty method<br>Object.defineProperty(Sum.prototype, &#39;getSum&#39;, {<br>    enumerable: false,<br>    value: function() {<br>        return (this.num1 + this.num2);<br>    }</pre><pre>});</pre><pre>// create constructor of function<br>var sObj = new Sum();<br>sObj.num1 = 10;<br>sObj.num2 = 20;<br>console.log(sObj.getSum());</pre><p><strong>enumerable</strong> property responsible for iterate or not over a key.</p><p>Finally, Now if we iterate over <strong>sObj</strong>:</p><pre>for (const key in sum) {<br>    console.log(&#39;key = &#39;, key);<br>}</pre><pre>// Output<br>key = num1     // 1st count<br>key = num2     // 2nd count</pre><p>For better performance, think before defining the property of any objects.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0768fd31b52" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/be-alert-to-define-any-property-of-an-object-d0768fd31b52">Pay attention when defining any property of an object</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to implement debounce decorator]]></title>
            <link>https://medium.com/time-machine/how-to-implement-debounce-decorator-2e963b70cd27?source=rss-76e4ee5ed0f0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2e963b70cd27</guid>
            <category><![CDATA[angular-6]]></category>
            <category><![CDATA[decorators]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[angular]]></category>
            <category><![CDATA[debounce]]></category>
            <dc:creator><![CDATA[Sandeep Rawat]]></dc:creator>
            <pubDate>Thu, 16 Jan 2020 19:41:50 GMT</pubDate>
            <atom:updated>2020-02-17T19:03:11.683Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to implement debounce decorator?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gk-1X8HbKeWKdxQa" /><figcaption>Photo by <a href="https://unsplash.com/@lifelikeinspiration?utm_source=medium&amp;utm_medium=referral">Jean Lakosnyk</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hi guys, A couple of days before I need to consume an API with <strong>debounce</strong> functionality in an Angular project. Some developers already known about <strong>debounce</strong> functionality or maybe some <em>not</em>. If you belong to <em>not</em> then first read <a href="https://www.geeksforgeeks.org/debouncing-in-javascript/"><em>Explain Debounce</em></a><em>. </em>Debounce functionality is not a new thing for me at all. I have already used debounce functionality in JavaScript. Now it’s time to implement debounce in Typescript, Finally much consideration I decided to implement debounce using <strong>Method Decorator</strong>. Yes, I use <a href="https://www.typescriptlang.org/docs/handbook/decorators.html#method-decorators"><em>Method Decorator</em></a> in Angular project. Decorators are an awesome feature of Typescript :).</p><h4><strong>Implementation of Debounce Decorator</strong></h4><p>Read first about method decorator:</p><blockquote>The expression for the method decorator will be called as a function at runtime, with the following three arguments:</blockquote><blockquote><strong>target</strong>: body of function.</blockquote><blockquote><strong>propertyKey</strong>: The name of the function.</blockquote><blockquote><strong>Descriptor</strong><em>: </em>The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty"><em>Property Descriptor</em></a> for the function.</blockquote><p>As per Typescript guideline firstly we need to make a function called <strong>ngDebounce</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d734da3b8eec5794a64ac530ee7b5c0/href">https://medium.com/media/0d734da3b8eec5794a64ac530ee7b5c0/href</a></iframe><p>After creating the basic structure, We need to write core functionality of debounce:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/17a970ea81cb02846256ba93a942ccb1/href">https://medium.com/media/17a970ea81cb02846256ba93a942ccb1/href</a></iframe><p>After the implementation of <strong>ngDebounce</strong> method decorator, it’s time to use it. According to Typescript guideline, we need to prefix <strong>@</strong> symbol to called a decorator.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/379feddb8c5b11c058e7372a5f391823/href">https://medium.com/media/379feddb8c5b11c058e7372a5f391823/href</a></iframe><p>Complete source code of <a href="https://gist.github.com/rawatsandeep670/9cffe81d5a53632cbaa3f01a32626e4b"><strong>ngDebounce </strong></a>decorator:</p><blockquote>want to manually cancel debounce, check cancelable-debounce.decorator.ts</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e37586c112e0673a2aead3eb2b7b2532/href">https://medium.com/media/e37586c112e0673a2aead3eb2b7b2532/href</a></iframe><p><strong>Checkout live example:</strong></p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fstackblitz.com%2Fedit%2Fdebouce-decorator%3Fembed%3D1&amp;url=https%3A%2F%2Fstackblitz.com%2Fedit%2Fdebouce-decorator&amp;image=https%3A%2F%2Fc.staticblitz.com%2Fassets%2Ficon-664493542621427cc8adae5e8f50d632f87aaa6ea1ce5b01e9a3d05b57940a9f.png&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=stackblitz" width="745" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/b81cac56eade45c4432b648f64fe1dd0/href">https://medium.com/media/b81cac56eade45c4432b648f64fe1dd0/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2e963b70cd27" width="1" height="1" alt=""><hr><p><a href="https://medium.com/time-machine/how-to-implement-debounce-decorator-2e963b70cd27">How to implement debounce decorator</a> was originally published in <a href="https://medium.com/time-machine">Time Machine</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>