<?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 Ceci García García on Medium]]></title>
        <description><![CDATA[Stories by Ceci García García on Medium]]></description>
        <link>https://medium.com/@ceci.garcia?source=rss-85c2933da91c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*1bzqGU9Tgeb-y9QFtoDyOw.jpeg</url>
            <title>Stories by Ceci García García on Medium</title>
            <link>https://medium.com/@ceci.garcia?source=rss-85c2933da91c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 21:52:50 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ceci.garcia/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[Preventing click events on double click with React, the performant way]]></title>
            <link>https://medium.com/trabe/preventing-click-events-on-double-click-with-react-the-performant-way-1416ab03b835?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/1416ab03b835</guid>
            <category><![CDATA[js]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 30 Mar 2020 08:01:01 GMT</pubDate>
            <atom:updated>2020-03-30T08:01:01.364Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OFxI-kJ_GByTgWj8zxLYoQ.png" /><figcaption>Photo by <a href="https://unsplash.com/@ciprian?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Ciprian Boiciuc</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>A few months ago I had to deal with a problem: React triggers the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event">onClick event</a> twice before triggering the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/dblclick_event">onDoubleClick</a> when a pointing device is double-clicked and I needed to handle both events in different ways. I wrote a post back then implementing a solution (“<a href="https://medium.com/trabe/prevent-click-events-on-double-click-with-react-with-and-without-hooks-6bf3697abc40?source=friends_link&amp;sk=cab2aeb0ba815fc6e1fe99d256bad4af">Prevent click events on double click with React</a>”) but it wasn’t until a few weeks ago that I found out that this solution can degrade the performance when managing these events.</p><h3>Implementation using delay</h3><p>Basically, we implemented a Hook that wraps both onClick and onDoubleClick handlers to delay every onClick handler until we can assure that the click is not part of a double-click event (after 300ms without receiving a new onClick event):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a66b942beb6d3f1d3a003ce417c75757/href">https://medium.com/media/a66b942beb6d3f1d3a003ce417c75757/href</a></iframe><p>In the mentioned post you can also find the fully implemented solution besides another implementation using a HOC.</p><p>This solution apparently works well in most common use cases, you barely can notice the lag between the click and the result of that click on the browser.</p><h3>The performance problem using delay</h3><p>The problem using delay came when we use this solution in some more complex scenarios.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">The </a><a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">setTimeout function</a> sets a timer which executes a function on the next event cycle once the timer expires. This means, the final delay will depend on the current status of the event loop. If the event loop is overloaded, the onClick handler could have to wait more than 300ms to be executed, which leads to a stuttering user interface.</p><p>I came up against this problem while analyzing the performance of a complex app: A delayed onClick handler took around 1200ms, 300ms of which were expended on the defined delay and 100ms on executing the callback. The onClick handler was spending 800ms just waiting for the event loop to finish the enqueued work.</p><p>The way I found to “avoid” the JS event loop was by using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame">requestAnimationFrame</a> method. The method allows you to tell the browser that you want it to perform a specified function before the next repaint.</p><h3>Solution using requestAnimationFrame</h3><p>A few weeks ago I wrote an entire post about this solution: “<a href="https://medium.com/trabe/implementing-settimeout-using-requestanimationframe-20cc2f6e6b5d?source=friends_link&amp;sk=8a2de90c91919d4951c1daeddac4ae2f">Implementing setTimeout using requestAnimationFrame</a>”. You can have a look at it if you want to know more about how the solution works, but maybe you can extract the idea from the implementation itself:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e4135c0b13676010008194803109b9c3/href">https://medium.com/media/e4135c0b13676010008194803109b9c3/href</a></iframe><p>Basically we implemented a “setTimeout with higher priority” using requestAnimationFrame under the hood. Instead of enqueuing the callback to the event loop when the delay time has passed like the setTimeout does, we chain requestAnimationFrame requests where every request checks if the delay time has passed and executes the callback if so, or enqueues a new request if not.</p><h3>Implementation using requestTimeout</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f25d725b585021c691269fa268b1778b/href">https://medium.com/media/f25d725b585021c691269fa268b1778b/href</a></iframe><p>We use a custom Hook useClickPrevention to wrap both callbacks onClick and onDoubleClick. To implement the boilerplate related with cancelling the scheduled current work we implemented another custom hook useCancelableScheduledWork so we can extract the logic and use it from the useClickPrevention Hook.</p><p>Maybe the trickiest thing in the solution is the way we implemented the support for cancelling the current pending request. We need to do it this way because the current request is changing with every new requestAnimationFrame and we need the actual ref to the request to cancel it. A complete explanation is in the post we mentioned above “<a href="https://medium.com/trabe/implementing-settimeout-using-requestanimationframe-20cc2f6e6b5d?source=friends_link&amp;sk=8a2de90c91919d4951c1daeddac4ae2f">Implementing setTimeout using requestAnimationFrame</a>”.</p><h3>Summing up</h3><p>If you want to handle both onClick and onDoubleClick, you will probably need to control the onClick handler to not be triggered when the click event comes from a dblClick event. This post improves the solution proposed in the post “<a href="https://medium.com/trabe/prevent-click-events-on-double-click-with-react-with-and-without-hooks-6bf3697abc40?source=friends_link&amp;sk=cab2aeb0ba815fc6e1fe99d256bad4af">Prevent click events on double click with React</a>” by using requestAnimationFrame to work around the bottleneck due to the JS event loop.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1416ab03b835" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/preventing-click-events-on-double-click-with-react-the-performant-way-1416ab03b835">Preventing click events on double click with React, the performant way</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing setTimeout using requestAnimationFrame]]></title>
            <link>https://medium.com/trabe/implementing-settimeout-using-requestanimationframe-20cc2f6e6b5d?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/20cc2f6e6b5d</guid>
            <category><![CDATA[javascript-event-loop]]></category>
            <category><![CDATA[event-loop]]></category>
            <category><![CDATA[settimeout]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 09 Mar 2020 09:01:00 GMT</pubDate>
            <atom:updated>2020-03-24T13:14:01.101Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-TLuPM885ZOiMG59h5tgwA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@brianna_santellan?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Brianna Santellan</a> on <a href="https://unsplash.com/s/photos/snooze?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Lately, I’ve been doing some work on improving the performance of a web application and I came up against an odd problem: a delayed onClick handler took around 1200ms, 300ms of which were expended on the defined delay and 100ms on executing the callback. So who is to blame for wasting the other 800ms? <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop">The JavaScript event loop</a> 😑.</p><p><a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">The </a><a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setTimeout">setTimeout function</a> sets a timer which executes a function on the next event cycle once the timer expires. This means, the final delay will depend on the current status of the event loop.</p><p>How can we make the callback function be executed right after the delay time has passed?</p><h3>requestAnimationFrame to force immediate execution</h3><p>With <a href="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame">requestAnimationFrame</a> method, you can tell the browser that you want it to perform a specified function before the next repaint. Even though the aim of the method is to perform animations, we can use it to prevent our callback being enqueued in the event loop, forcing the browser to give it high priority.</p><p>Here is our implementation of a “high priority” setTimeout:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16a25750bd819b707c5cceaeafb9e84c/href">https://medium.com/media/16a25750bd819b707c5cceaeafb9e84c/href</a></iframe><p>We require a registerCancel function to provide the option to cancel the request. The common way to do that is by returning an id and providing a function to cancel a request using its id, like setTimeout does:</p><pre>let t = setTimeout(work, 200);<br>clearTimeout(t);</pre><p>However our implementation of requestTimeout isn’t really a single request but a loop of requests. Every request creates a new request if the delay time hasn’t passed. This way the request id changes on every loop so we need to have the id updated to cancel the current request. To do that, we can create a local variable or an immutable object to store the current request id and a function to update it:</p><pre>let t = null;<br>requestTimeout(work, 200, id =&gt; (t = id));<br>cancelAnimationFrame(t);</pre><p>The same way we store the id, we can store the scoped function to cancel the current request so we don’t need to export a cancel function:</p><pre>let cancel = null;<br>requestTimeout(work, 200, fn =&gt; (cancel = fn));<br>cancel();</pre><p>Every time the loop creates a new request, the scoped cancel function is reinjected. Looking back at our code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e2f998b40f4ba7f0ce407f1ce7dcc11/href">https://medium.com/media/4e2f998b40f4ba7f0ce407f1ce7dcc11/href</a></iframe><h3>Legacy browsers compliant</h3><p><a href="https://caniuse.com/#feat=requestanimationframe">Most browsers support requestAnimationFrame</a> but if you need to support those that don’t, you can adapt the implementation to delegate to setTimeout in these cases:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7abc07101a89596cd90880d1f84dc30f/href">https://medium.com/media/7abc07101a89596cd90880d1f84dc30f/href</a></iframe><h3>Wrapping up</h3><p>Sometimes we need to delay an execution but when the delay time has passed we need to perform the execution as soon as possible, skipping the event loop. This article describes an alternative implementation of the setTimeout function that uses the requestAnimationFrame under the hood to give our callback a higher priority.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=20cc2f6e6b5d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/implementing-settimeout-using-requestanimationframe-20cc2f6e6b5d">Implementing setTimeout using requestAnimationFrame</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Can we rely on useCallback as a semantic guarantee?]]></title>
            <link>https://medium.com/trabe/can-we-rely-on-usecallback-as-a-semantic-guarantee-9329c17f40cf?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/9329c17f40cf</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[memoization]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 30 Sep 2019 08:01:00 GMT</pubDate>
            <atom:updated>2019-09-30T17:26:25.855Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R0Pjbg6hiSD3eH8ENGJwsw.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@gillendesign?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Ewa Gillen</a> on <a href="https://unsplash.com/s/photos/animal-funny?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In <a href="https://medium.com/trabe">Trabe</a>, we’re working on migrating class components to functional components with Hooks. This week, we came across a particular problem when passing callbacks as a dependency of a useEffect Hook. Let’s see the problem with an example:</p><p>We need a &lt;Counter /&gt; that accepts a “notify” callback, and uses it to notify about its value changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0d05fed48586b7580c3183d6564e8453/href">https://medium.com/media/0d05fed48586b7580c3183d6564e8453/href</a></iframe><p>The &lt;Counter /&gt; will notify about its value on the first render (the source gets the initial value), and on any subsequent changes.</p><p>The thing is, the source to notify may change because a new url is injected to &lt;CounterNotifier /&gt;. When the url changes, since the &lt;Counter /&gt; is already mounted, the new source won’t receive the current value until a change happens (the effect is triggered).</p><p>To solve that, we may need to pass the notify callback itself as a dependency of the effect:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0e89cd2ee8318b1847cb79b6052a3e94/href">https://medium.com/media/0e89cd2ee8318b1847cb79b6052a3e94/href</a></iframe><p>If the &lt;Counter /&gt; component notifies when the value or the notify callback changes, we have to ensure that the callback only changes when it really changes: when a new url is injected to &lt;CounterNotifier /&gt;.</p><h3>Memoizing a function with useCallback to ensure it doesn’t change</h3><p>Wrapping the notify callback with a useCallback Hook sounds like the way to ensure it won’t change unless its dependencies change (the url):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a40f5ceed64e596738af43594d86cd93/href">https://medium.com/media/a40f5ceed64e596738af43594d86cd93/href</a></iframe><p>Actually, the React docs describes this in a FAQ as <a href="https://reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies">the recommend way to pass a function as a Hook dependency</a> when we need to.</p><p>But…</p><h3>What about cache busting?</h3><p>React docs also warn:</p><blockquote><strong>You may rely on </strong><strong>useMemo as a performance optimization, not as a semantic guarantee.</strong> In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, e.g. to free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.</blockquote><p>And also:</p><blockquote>useCallback(fn, deps) is equivalent to useMemo(() =&gt; fn, deps).</blockquote><p>That led us to think that useCallback shouldn’t be used as a semantic guarantee either.</p><p>Doing some investigation, <a href="https://github.com/facebook/react/issues/15278">we’ve found some people complaining about cache busting on </a><a href="https://github.com/facebook/react/issues/15278">useCallback</a> and asking for a third argument where you can opt-out of this cache purging. We haven’t found any answer from the React team ensuring that the cache busting just affects useMemo, not useCallback.</p><p>The only thing we’ve found in the docs about this, is in <a href="https://reactjs.org/docs/hooks-faq.html#is-it-safe-to-omit-functions-from-the-list-of-dependencies">the FAQ mentioned before</a>:</p><blockquote>You can <strong>add a function to effect dependencies but <em>wrap its definition</em></strong> into the <a href="https://reactjs.org/docs/hooks-reference.html#usecallback">useCallback</a> Hook. This ensures it doesn’t change on every render unless <em>its own</em> dependencies also change.</blockquote><p>So… What should we think? 😅 I hope they may update the docs to make it clear.</p><h3>Summing up</h3><p>React team: We need a way to ensure that a callback that needs to be defined inside a functional component won’t change if we don’t want it to. If this way exists and it’s called useCallback, please tell us about the cache busting… Or maybe we’re losing something important, in that case:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Ftenor.com%2Fembed%2F14154125&amp;url=https%3A%2F%2Ftenor.com%2Fview%2Fpls-send-help-help-me-sos-dog-the-pet-collective-gif-14154125&amp;image=https%3A%2F%2Fmedia.tenor.com%2Fimages%2F3e3909afb2faca0e56fd65d2d44cb4b0%2Ftenor.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=tenor" width="600" height="400" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd776c09df26ca8a582d54da33b9a60a/href">https://medium.com/media/fd776c09df26ca8a582d54da33b9a60a/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9329c17f40cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/can-we-rely-on-usecallback-as-a-semantic-guarantee-9329c17f40cf">Can we rely on useCallback as a semantic guarantee?</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Prevent click events on double click with React (with and without Hooks)]]></title>
            <link>https://medium.com/trabe/prevent-click-events-on-double-click-with-react-with-and-without-hooks-6bf3697abc40?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/6bf3697abc40</guid>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[js]]></category>
            <category><![CDATA[react-hook]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 22 Jul 2019 08:01:01 GMT</pubDate>
            <atom:updated>2019-07-22T08:01:01.257Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Sxpdt6lF8uyHhSstpQmUwA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@bonniekdesign?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Bonnie Kittle</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>This post proposes two implementations (one with a <a href="https://reactjs.org/docs/higher-order-components.html">HOC</a>, and the other one with a <a href="https://reactjs.org/docs/hooks-intro.html">Hook</a>) that solve the problem of React triggering the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event">onClick event</a> twice before triggering the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/dblclick_event">onDoubleClick</a> when a pointing device is double-clicked.</p><p>If you already know about this problem, you can skip the next section and jump directly to the solution.</p><h3>Handling both onDoubleClick and onClick events</h3><p>React allows us to <a href="https://reactjs.org/docs/handling-events.html">handle DOM events in React elements</a>. Equivalent to <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event">click event</a> and <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/dblclick_event">dblclick event</a>, in React we can configure, for an element:</p><ul><li>onClick: callback that will be executed when a click event is triggered.</li><li>onDoubleClick: callback that will be executed when a dblclick event is triggered.</li></ul><p>Look at the example below of a &lt;ClickableBox&gt; component that allows the configuration of both handlers:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fb8eb8a532908e2e8ebe414493e7e2f/href">https://medium.com/media/1fb8eb8a532908e2e8ebe414493e7e2f/href</a></iframe><p>If we make a double-click on the &lt;ClickableBox&gt;, we get this in the browser console:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/228/1*qI7cdvcyLN1MZm8cDj4ZtQ.png" /></figure><p>Like browsers, React triggers two single click events before triggering the doubleclick event.</p><p>How can we avoid the click event to be executed twice when a doubleclick happens?</p><h3>Solution</h3><p>The solution lies in delaying the execution of the onClick handler until we know that the click is not part of a double-click: The onClick handlers are delayed for a while in a queue of pending <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise">promises</a> and, when a dblclick event is triggered, all the pending promises are cancelled so they will never be executed.</p><h4>Cancellable promise</h4><p>To handle the pending promises, we will use <em>cancellable promises. </em>I’ve already talked about the cancellable promises in a the post “<a href="https://medium.com/trabe/avoid-updates-on-unmounted-react-components-2fbadab17ad2">Avoid updates on unmounted React components</a>”, you can check it out if you haven’t!</p><p>A cancellable promise is, basically, a wrapped promise that can be cancelled (rejected with extra info to know the rejection happened because of a cancellation). Below the implementation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa532c400bf9344ea38f4337fe49594e/href">https://medium.com/media/fa532c400bf9344ea38f4337fe49594e/href</a></iframe><p>Below the tools we are going to import from /utils in the following implementations:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a66a8e1e03219633951562462ba25f48/href">https://medium.com/media/a66a8e1e03219633951562462ba25f48/href</a></iframe><h4>Solution using a HOC</h4><p>There’re some notes all over the implementation to make it easier to follow:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1865a75fcfcdea5dd448e034d7b5597b/href">https://medium.com/media/1865a75fcfcdea5dd448e034d7b5597b/href</a></iframe><p>We can use pleaseStopTriggeringClicksOnDoubleClick HOC with any component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c60939ade8ba38960856d8e80207e5f6/href">https://medium.com/media/c60939ade8ba38960856d8e80207e5f6/href</a></iframe><h4>Solution using Hooks</h4><p>With Hooks, we can extract to a <a href="https://reactjs.org/docs/hooks-custom.html">custom Hook</a> both logics: handling cancellable promises and redefining onClick and onDoubleClick handlers.</p><p>To extract the logic of handling cancellable promises, we define a custom Hook that holds a <a href="https://reactjs.org/docs/hooks-reference.html#useref">ref</a> with the array of pending promises, defines the api to interact with this array, and returns the api.</p><p>We don’t store the pending promises in a state variable because we don’t want the component to be re-rendered every time this array changes.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d566c4188049eb18c939df6ac2788cec/href">https://medium.com/media/d566c4188049eb18c939df6ac2788cec/href</a></iframe><p>To extract the logic of redefining onClick and onDoubleClick handlers, we define another custom Hook that, given both handlers, redefine them to implement the solution described above using the useCancellablePromises Hook:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94d740f18c91eb62d86b38b9abb687a2/href">https://medium.com/media/94d740f18c91eb62d86b38b9abb687a2/href</a></iframe><p>Any functional component can use the useClickPreventionOnDoubleClick Hook easily:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c2499bd6fa2eae21581a9a297664f0a3/href">https://medium.com/media/c2499bd6fa2eae21581a9a297664f0a3/href</a></iframe><h3>Summing up</h3><p>If you want to handle both onClick and onDoubleClick, you will probably need to control the onClick handler to not be triggered when the click event comes from a dblClick event. You can use the solution proposed in this post choosing the one that suits you best, the one with a HOC for your class components or the one using custom Hooks for your functional components.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6bf3697abc40" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/prevent-click-events-on-double-click-with-react-with-and-without-hooks-6bf3697abc40">Prevent click events on double click with React (with and without Hooks)</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React useRef Hook]]></title>
            <link>https://medium.com/trabe/react-useref-hook-b6c9d39e2022?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6c9d39e2022</guid>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[useeffect]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 01 Jul 2019 08:01:01 GMT</pubDate>
            <atom:updated>2020-05-10T12:31:06.290Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fGVtZ0a_ki-Vg14YzJqKYQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@brianna_santellan?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Brianna Santellan</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>In this post we’ll cover what the <a href="https://reactjs.org/docs/hooks-reference.html#useref">useRef Hook</a> is, what we can use it for and some advice for using it.</p><p>The useRef Hook is a function that returns a mutable <a href="https://reactjs.org/docs/refs-and-the-dom.html">ref</a> object whose .current property is initialized with the passed argument (initialValue). The returned object will persist for the full lifetime of the component.</p><pre>const refContainer = useRef(initialValue);</pre><p>There are two main uses of useRef that are explained in the following sections: <a href="https://reactjs.org/docs/refs-and-the-dom.html">Accessing the DOM nodes or React elements</a> and <a href="https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables">keeping a mutable variable</a>.</p><h3>Accessing the DOM nodes or React elements</h3><p>If you’ve worked with React for a while, you might be used to using refs for this purpose. Below there’s an example of the use of refs in class components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/564e7abd03cba7f6f7ac39a131eff6bc/href">https://medium.com/media/564e7abd03cba7f6f7ac39a131eff6bc/href</a></iframe><p>And its equivalent using a functional component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f24df03e8bcb5a768711aaedd28e5b06/href">https://medium.com/media/f24df03e8bcb5a768711aaedd28e5b06/href</a></iframe><p>‍Notice that with a functional component we are using useRef instead of createRef. If you create a ref using createRef in a functional component, React will create a new instance of the ref on every re-render instead of keeping it between renders.</p><h3>Keeping a mutable variable</h3><p>Both in class components and functional components using Hooks, we have two ways of keeping data between re-renders:</p><h4>In class components</h4><ul><li><strong>In the component state</strong>: Every time the state changes, the component will be re-rendered.</li><li><strong>In an instance variable</strong>: The variable will persist for the full lifetime of the component. Changes in an instance variable won’t generate a re-render.</li></ul><h4>In functional components</h4><p>The equivalent ways in functional components using Hooks:</p><ul><li><strong>In a state variable</strong>: useState or useReducer. Updates in state variables will cause a re-render of the component.</li><li><strong>In a ref: </strong>Equivalent to instance variables in class components. Mutating the .current property won’t cause a re-render.</li></ul><p>Below is an example of keeping a mutable variable in a ref. The component &lt;Timer&gt; initializes a <a href="https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/setInterval">setInterval</a> on every re-render and needs to implement a callback to stop its interval imperatively:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbb8e5d8ec6fa50aa4f6910db46d72b7/href">https://medium.com/media/bbb8e5d8ec6fa50aa4f6910db46d72b7/href</a></iframe><h3>Updating a ref</h3><p>In the Hooks FAQ <a href="https://reactjs.org/docs/hooks-faq.html#is-there-something-like-instance-variables">Is there something like instance variables?</a> this is mentioned:</p><p><em>“Unless you’re doing </em><a href="https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily"><em>lazy initialization</em></a><em>, avoid setting refs during rendering — this can lead to surprising behavior. Instead, typically you want to modify refs in event handlers and effects.”</em></p><p>Why is that? To understand it, we need to do a brief refresh of the React component lifecycle with Hooks:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Lvb5C_NdJCyt7YgA89nvNg.jpeg" /></figure><p>The “Render phase” may be restarted by React so it has to be pure, it can’t have side effects.</p><h4><strong>Updating a ref value is a side effect.</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b04de1ca896e4f24adce08b39cdb1782/href">https://medium.com/media/b04de1ca896e4f24adce08b39cdb1782/href</a></iframe><p>All side effects should be done in the “Layout phase” or in the “Commit phase”; inside <a href="https://reactjs.org/docs/hooks-reference.html#uselayouteffect">useLayoutEffect</a> or the <a href="https://reactjs.org/docs/hooks-reference.html#useeffect">useEffect</a> when using React Hooks.</p><p>Here is the right version of the example above:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4446ea142ce7a048a313a5f6ace0b583/href">https://medium.com/media/4446ea142ce7a048a313a5f6ace0b583/href</a></iframe><h3>Summing up</h3><p>The useRef Hook lets us create mutable variables inside functional components. There are three main key points that you should keep in mind when using the useRef Hook:</p><ul><li>A ref created with useRef will be created only when the component has been mounted and preserved for the full lifecycle.</li><li>Refs can be used for accessing DOM nodes or React elements, and for storing mutable variables (like with instance variables in class components).</li><li>Updating a ref is a side effect so it should be done only inside a useEffect (or useLayoutEffect) or inside an event handler.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6c9d39e2022" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/react-useref-hook-b6c9d39e2022">React useRef Hook</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing private routes with React Router and Hooks]]></title>
            <link>https://medium.com/trabe/implementing-private-routes-with-react-router-and-hooks-ed38d0cf93d5?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/ed38d0cf93d5</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-context]]></category>
            <category><![CDATA[react-router]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[hooks]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 17 Jun 2019 08:01:01 GMT</pubDate>
            <atom:updated>2019-06-17T08:01:01.059Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ocb_HEisQPOC_GCVeWCz1w.jpeg" /></figure><p>Recently I had to configure as private some routes of a React app (only accesible for authenticated users). This post describes the basic implementation of the app that covers:</p><ul><li>Routing managed with <a href="https://github.com/ReactTraining/react-router#readme">React Router</a>.</li><li>Private routes only accesible for authenticated users.</li><li>Auth data accesible from any component using <a href="https://reactjs.org/docs/context.html">React Context</a>.</li></ul><h3>Initial setup</h3><p>We create a simple React app with a common layout and a dynamic content that changes depending on the current route:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/14861d9c0d6a83d3a2fb441d78a2d051/href">https://medium.com/media/14861d9c0d6a83d3a2fb441d78a2d051/href</a></iframe><p>The &lt;Router&gt; component defines all the possible routes of our app and their corresponding components:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b34f8a0ae2866df6c9041683bdef6b3a/href">https://medium.com/media/b34f8a0ae2866df6c9041683bdef6b3a/href</a></iframe><p>We want to configure each route as public or private (or, in a future, some more specific restrictions like “only for admin users”). There are a lot of ways to do that, but the one we chose consists in delegating the responsibility to each route. This way, a route will render the corresponding component when the user is already authenticated or the sign in page when they are not. To do that, we need our &lt;Route&gt; components to have access to the auth data.</p><h3>Providing the auth data to the components</h3><p>No matter the authentication method your app is using, most probably you will need to store the auth data of the current user somewhere in the app, and some of your components will need to read from that data (our private routes, for example).</p><p>We can encapsulate all this logic in one single component that:</p><ul><li>Checks if the user is already logged in and if so, stores their auth data.</li><li>Provides access to the data to all its children.</li><li>Provides handlers to: store the auth data of a user just logged in, and remove the auth data of a user just logged out.</li></ul><p>We’ll do so by using the React Context and the <a href="https://reactjs.org/docs/hooks-reference.html#usecontext">useContext Hook</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be359d8edd6bafde0c7a520d2c4a615d/href">https://medium.com/media/be359d8edd6bafde0c7a520d2c4a615d/href</a></iframe><p><a href="https://medium.com/trabe/how-we-handle-react-context-e43d303a27a2">In his last post</a>, my colleague <a href="https://medium.com/@david.barral">David Barral</a> covered how we use the React Context using the example of an auth provider. The post explains why we implement Context providers like above. Have a look at it if you haven’t already!</p><p>Remember that we have to place the &lt;AuthDataProvider&gt; in the component hierarchy in a higher level that the customer components that need to read from the auth data context.</p><p>As we have seen above, our &lt;Route&gt; components inside the &lt;Router&gt; will need to access the auth data. Other components within the &lt;Layout&gt; like a box in the top bar of the app that shows the user info and a link to do the logout, will need to access this data too. The &lt;AuthDataProvider&gt; should be placed higher in the hierarchy than the &lt;Layout&gt; and the &lt;Router&gt; in our app:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6ea775eb788cd29f639223bd69513ee/href">https://medium.com/media/a6ea775eb788cd29f639223bd69513ee/href</a></iframe><p>Now all components placed inside the &lt;AuthDataProvider&gt; will have access to the auth data. For example, the box that shows the user info and the link to logout:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1c95791b33db7ebfcacddb30e879a56/href">https://medium.com/media/e1c95791b33db7ebfcacddb30e879a56/href</a></iframe><p>or the sign-in form that will be rendered inside the &lt;SignInPage&gt;:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39ac262321d87fe382afbdd0155f60fd/href">https://medium.com/media/39ac262321d87fe382afbdd0155f60fd/href</a></iframe><h3>Defining private routes</h3><p>Finally, we need to configure all our routes as private (only accesible for authenticated users) but the sign in page. To do so, we’ll implement a wrapper &lt;PrivateRoute&gt; for the &lt;Route&gt; components from React Router. The wrapper will make sure that the user is already authenticated before defining the matching component and render the sign in page if she isn’t:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9522b6a593f25e36ff640ac5a5fc6b1/href">https://medium.com/media/d9522b6a593f25e36ff640ac5a5fc6b1/href</a></iframe><h3>Summing up</h3><p>Implementing private routes is quite simple with React Router and React Context. There’re a lot of ways to do that, but I found this one specially focused and simple. I hope it helps!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ed38d0cf93d5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/implementing-private-routes-with-react-router-and-hooks-ed38d0cf93d5">Implementing private routes with React Router and Hooks</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Passing callbacks down with React Hooks]]></title>
            <link>https://medium.com/trabe/passing-callbacks-down-with-react-hooks-4723c4652aff?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/4723c4652aff</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[callback]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-hook]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 08 Apr 2019 08:01:01 GMT</pubDate>
            <atom:updated>2019-04-08T08:01:01.425Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y2vjQkCNcas2vjPyXBc1_A.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/lcL37UYO48o?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">zhenhao Liu</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>A common problem in React involves passing props from a component down through several layers of components.</p><p>Below is an example of a component that consists in a list of tasks and some actions to manage that tasks. The &lt;TodoList /&gt; keeps the state of the list, so the callbacks to manage it should be passed down to the components that will use them:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ceeb7ab9ea7ea6fd10ab8bba421ab64f/href">https://medium.com/media/ceeb7ab9ea7ea6fd10ab8bba421ab64f/href</a></iframe><p>Instead of having to pass each callback down the tree in every component, we can define an API object that collects all callbacks. This way, in case we need to add/remove some callbacks, we won’t have to adapt every component in the hierarchy.</p><p>We could simplify the example above even more. To avoid the hell of passing down through every component in the hierarchy, we could pass the “API object” via <a href="https://reactjs.org/docs/context.html">React Context</a> with the <a href="https://reactjs.org/docs/hooks-reference.html#usecontext">useContext Hook</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05f999ccd9d6b20fc6e0108a05a62afe/href">https://medium.com/media/05f999ccd9d6b20fc6e0108a05a62afe/href</a></iframe><p>The problem is that the API object will change in every rerender, so all components that read it from the context will be rerendered as well.</p><h3>The pattern recommended by the React team</h3><p>We talked a little about this problem in a <a href="https://medium.com/trabe/react-usereducer-hook-2b1331bb768">previous post</a> and pointed to <a href="https://reactjs.org/docs/hooks-faq.html#how-to-avoid-passing-callbacks-down">the pattern recommended by the React team</a> to solve it. The pattern is a refactor of the example above but using useReducer instead of useState. Managing the state with useReducer allows us to pass just the dispatcher down and, since the dispatcher doesn’t change between renders, the components that read it from the context won’t be rerendered:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5ba097020d593b5b6ac3af7a87fc7b47/href">https://medium.com/media/5ba097020d593b5b6ac3af7a87fc7b47/href</a></iframe><h3>Our thoughts about the recommended pattern</h3><p>This solution definitely solves our problem, but still we didn’t feel fully satisfied with it because:</p><ul><li><strong>We have to turn “dumb components”</strong> (only concerned with how things are painted) <strong>into “smart components”</strong> (also concerned with how things work)<strong>:</strong> In the example above, the action components &lt;AddTodoBtn /&gt;, &lt;RemoveAllBtn /&gt; can be dumb components if they only have to get the onClick handler from the context instead of getting the dispatcher and deciding which action they have to dispatch.</li><li><strong>We’re forced to use the </strong><strong>useReducer Hook to manage the state.</strong> It doesn’t matter in simple examples as the one above, but in more complex scenarios it could restrict us from using custom Hooks already implemented using useState to manage the state.</li></ul><h3>How we solve this problem</h3><p>Our solution lies in combining the two previous examples:</p><ul><li>Using the context to pass variables down to the components that will use them.</li><li>Using an “API object” that contains all the callbacks that the nested components will need (this callbacks can use either useState or useReducer to manage the state).</li><li><a href="https://reactjs.org/docs/hooks-reference.html#usememo">Memoizing</a> the creation of the “API object” to make sure it won’t change through rerenders.</li><li>Making sure all callbacks defined in the API object don’t depend on their scope.</li></ul><p>We’ve already see how to pass an API object using the <a href="https://reactjs.org/docs/hooks-reference.html#usecontext">React </a><a href="https://reactjs.org/docs/hooks-reference.html#usecontext">useContext Hook</a> in the first example so let’s see how we can memoize this API object.</p><h3>Memoizing an API object</h3><p>To memoize an API object we have to memoize both the API object itself and its callbacks. We use <a href="https://reactjs.org/docs/hooks-reference.html#usememo">the </a><a href="https://reactjs.org/docs/hooks-reference.html#usememo">useMemo Hook</a> to memoize the API object and <a href="https://reactjs.org/docs/hooks-reference.html#usecallback">the </a><a href="https://reactjs.org/docs/hooks-reference.html#usecallback">useCallback Hook</a> to memoize the callbacks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65fa936b6ad852633380b16462d890ec/href">https://medium.com/media/65fa936b6ad852633380b16462d890ec/href</a></iframe><p>Ok, we’ve memoized the API object so it won’t change between rerenders, but the action depending on the scope (addTodo depends on the todo state value) won’t be consistent over rerenders since the value of the state variable won’t change.</p><p>One possible solution could be to add the todos state variable to the array of dependencies (second argument) of the useCallback Hook and the useMemo Hook. This way, both callback and API object will update every time the state variable changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd8e1d659b210766230bde87b14763f2/href">https://medium.com/media/fd8e1d659b210766230bde87b14763f2/href</a></iframe><p>If the memoization of the API object depends on the scope where it’s defined, we just have partial memoization.</p><p>If we want our API object to never change between rerenders, we need our callbacks to be independent of the scope. <strong>We can do this by updating the state with </strong><a href="https://reactjs.org/docs/hooks-reference.html#functional-updates"><strong>functional updates</strong></a><strong>:</strong> We can pass a function to setState, the function will receive the previous value of the state, and return the updated value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4adf059ca3b48d3b3866ba570987116e/href">https://medium.com/media/4adf059ca3b48d3b3866ba570987116e/href</a></iframe><p>Our API object is now completely independent from the scope so, as the dispatch function, it won’t change between rerenders! Yay!</p><h3>To sum up</h3><p>To avoid passing callbacks down through the component hierarchy, we can follow the suggested pattern of passing the dispatch function of the useReducer using the Context, but we end up being forced to manage the state via a reducer, and we have to spread some knowledge about the state through the components that use this callbacks.</p><p>We suggest to do this by passing a memoized API object using the context. To achieve the complete invariability of the API object, we use functional updates so we don’t depend on the scope of the callbacks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4723c4652aff" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/passing-callbacks-down-with-react-hooks-4723c4652aff">Passing callbacks down with React Hooks</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React useEffect Hook]]></title>
            <link>https://medium.com/trabe/react-useeffect-hook-44d8aa7cccd0?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/44d8aa7cccd0</guid>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[useeffect]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-hook]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 18 Mar 2019 09:01:02 GMT</pubDate>
            <atom:updated>2019-04-03T11:59:05.143Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A1hvunN8rKtxjAG0dlB0QA.png" /><figcaption>Photo by <a href="http://aquiyenlima.tumblr.com/">Lucas Andión</a>.</figcaption></figure><p>In my last posts we <a href="https://medium.com/trabe/react-16-7-753c61ca7a1e">introduced the React Hooks</a> and talked a little about <a href="https://medium.com/trabe/react-usereducer-hook-2b1331bb768">managing the state with the state hooks</a> (useState and useReducer). In this post we’ll cover the <a href="https://reactjs.org/docs/hooks-effect.html">Effect Hook</a> useEffect.</p><p>The Effect Hook lets you perform side effects in function components. Some examples of “side effects” or just “effects” can be data fetching, manually changing the DOM or setting up a subscription.</p><p>With the useEffect Hook we can tell React that after every render, the callback passed to this useEffect function as its first parameter (“effect”) should be called. React will remember this callback and call it later after performing the DOM updates.</p><pre>useEffect(() =&gt; {<br>  // Here the effect<br>});</pre><p>Below is an example of an app with an input that changes the document title using Hooks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/394612ba3fd5fad272a75f244e73d334/href">https://medium.com/media/394612ba3fd5fad272a75f244e73d334/href</a></iframe><p>Before <a href="https://reactjs.org/docs/hooks-intro.html">Hooks</a>, we only could manage this effects within the lifecycle methods of class components: componentDidMount, componentDidUpdate and componentWillUnmount. The logic of an effect ended up being spread out throughout the different lifecycle methods.</p><p>The example of the previous app implemented without Hooks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcb030310fb3eeedf9583605c33d7f24/href">https://medium.com/media/dcb030310fb3eeedf9583605c33d7f24/href</a></iframe><p>We could extract the logic of updating the document title from the component state, but still we’ll have to call the updater function from different lifecycle methods.</p><h4>When does useEffect run?</h4><p>By default, it runs after every render but we can customize it with the second param of the useEffect function. As a second argument, the useEffect function accepts an array that allow us to tell React when we want our effect to be called.</p><p>After a render and before calling an effect, React will compare the array of values defined in the second parameter of the effect with the array defined in the same effect from the previous render. React will only call the effect when any value of the array has changed since the previous render.</p><p>In the previous example, we don’t need to update the document title (our effect) after every render but only when the state variable name has changed its value since the previous render. That’s why we pass an array with the value of name as the second parameter:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/394612ba3fd5fad272a75f244e73d334/href">https://medium.com/media/394612ba3fd5fad272a75f244e73d334/href</a></iframe><p>The same way, if we only want our effect to be called only after the first render, we have to pass an empty array [] (which never changes) as second parameter.</p><h4>How do effects run?</h4><p>Unlike componentDidMount and componentDidUpdate, effects scheduled with useEffect don’t block the browser from updating the screen. If you need your effect to be happen synchronously, you can use the Hook useLayoutEffect which has the same API as useEffect.</p><h4>Effects with cleanup</h4><p>There are some effects that require a cleanup to avoid memory leaks like subscriptions to external sources.</p><p>This is how we do the subscription of a component to an external source without Hooks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d9ebfb6d2dd76c33362a3c0a65e6ff17/href">https://medium.com/media/d9ebfb6d2dd76c33362a3c0a65e6ff17/href</a></iframe><p>And below the same example using Hooks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7b64e906a64ab0f805b185a1d724d3f0/href">https://medium.com/media/7b64e906a64ab0f805b185a1d724d3f0/href</a></iframe><p>As long as we only want our effect (subscribe to resizes) to be called once, we pass an empty array as the second parameter of the function useEffect.</p><p>An effect can optionally return a function (the cleanup function) that React will call when the component unmounts and before running the effect next time. In the example, our effect returns a function that unsubscribes it from the window resize events.</p><h4>Example with multiple effects</h4><p>It’s always a good idea to use multiple effects to separate concerns. Below is an example of a component that is subscribed to window resizes (we do this with the first effect) and shows the type of screen we’re managing depending on its width (second effect).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23183e1bef917e518caf7b16e4e1a8c7/href">https://medium.com/media/23183e1bef917e518caf7b16e4e1a8c7/href</a></iframe><h4>Summing up</h4><p>React useEffect Hook allows you to manage side effects with no need of class components, and to extract and reuse the logic of an effect from a component. Before using it, it’s important to know the key points we covered in this post:</p><ul><li>When and how does React perform the effects.</li><li>How to customize when an effect should be called.</li><li>How to cleanup the effects when unmounting the component.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=44d8aa7cccd0" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/react-useeffect-hook-44d8aa7cccd0">React useEffect Hook</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React useReducer hook]]></title>
            <link>https://medium.com/trabe/react-usereducer-hook-2b1331bb768?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b1331bb768</guid>
            <category><![CDATA[usereducer]]></category>
            <category><![CDATA[usestate]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[react-16]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 04 Feb 2019 09:01:01 GMT</pubDate>
            <atom:updated>2019-02-04T09:01:01.052Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s2v-hgmNAf4kn-V-jW35gA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/TvWLT7rnIJE?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Vedant Choudhary</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><a href="https://medium.com/trabe/react-16-7-753c61ca7a1e">In my last post “React 16.7”</a>, we had a look at the new React proposal version (which, by the way, <a href="https://reactjs.org/blog/2018/12/19/react-v-16-7.html">it’s not a proposal for v16.7 anymore, but for v16.8</a>) and a brief introduction to the hooks useState and useEffect. In this post we’ll address the useReducer hook.</p><h4>Managing component state with the useState hook</h4><p><a href="https://reactjs.org/docs/hooks-reference.html#usestate">useState</a> is the basic hook that allows us to keep a state in a functional component. A basic usage example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/080d11871ecef0970712bd101ad139f8/href">https://medium.com/media/080d11871ecef0970712bd101ad139f8/href</a></iframe><p>The updater function returned by the useState hook works much the same as the previous <a href="https://reactjs.org/docs/react-component.html#setstate">React </a><a href="https://reactjs.org/docs/react-component.html#setstate">setState</a> function, except that the new one replaces the whole state with the argument passed instead of merging it with the old state.</p><p>Since we can use as many useState functions as we want, we can create and handle component state no matter its shape.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27366c58d4f918465e39a31dd8a3bea3/href">https://medium.com/media/27366c58d4f918465e39a31dd8a3bea3/href</a></iframe><p>However, if our component has to keep several data which are all part of the same logic state, useState may become a bit cumbersome. Here is an example of a counter which can increment, decrement and reset its value and undo the last operation. To implement the undo operation, we need to keep the previous state in addition to the current state for every operation:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffdcbe5b1916870dbf474089e11d14c9/href">https://medium.com/media/ffdcbe5b1916870dbf474089e11d14c9/href</a></iframe><h4>useReducer to the rescue!</h4><p><a href="https://reactjs.org/docs/hooks-reference.html#usereducer">useReducer</a> is another hook to manage the state in a component. It looks like this:</p><pre>const [state, dispatch] = useReducer(reducer, initialState);</pre><p>The reducer function should be a function that receives a state and an action and (reduces it to) returns a new state: (state, action) =&gt; newState.</p><p>Below is the previous example but implemented using useReducer instead of useState:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22efed5e73b8e7ba0ccf247db7b94c12/href">https://medium.com/media/22efed5e73b8e7ba0ccf247db7b94c12/href</a></iframe><p>This way, besides doing all state changes at once when an action is triggered, we can find all logic related with the same state placed together.</p><p>Dan Abramov wrapped up on a tweet the matter of when to use useState or useReducer to manage the component state:</p><h3>Dan Abramov on Twitter</h3><p>Should I use multiple useState or useReducer? For independent things (isHovering and textInput), multiple useState. For things that change together (isFetching and fetchedItems), or if their next state depends on previous (todos), I prefer useReducer. https://t.co/wSyKITMRpZ</p><h4>Avoid passing callbacks down</h4><p>With the useReducer hook, we can simplify the problem of <a href="https://reactjs.org/docs/hooks-faq.html#how-to-avoid-passing-callbacks-down">passing callbacks down in large tree components</a>. Here’s an example, quite contrived but we still can see the point we are talking about:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a037ebadfeeccf61757b7ac1354a730/href">https://medium.com/media/8a037ebadfeeccf61757b7ac1354a730/href</a></iframe><p>We could simplify the example above by defining an object which collects all callbacks so we can pass it down. To avoid the hell of passing down through every component in the hierarchy, we could pass the “api object” via context. The problem is that the object would change in every rerender, so all components that read it from the context would be rerendered as well.</p><p>Abramov recommended a similar pattern but, instead of passing down an “api object”, passing the dispatch function:</p><h3>Dan Abramov on Twitter</h3><p>Have you tried this pattern yet? It&#39;s especially powerful combined with type definitions for context and reducer. https://t.co/3bel34I7QL</p><p>The key of this pattern is that the dispatch function doesn’t change between rerenders. So the final solution would look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/38fb4490bf97fbfdeb3caedc081b9473/href">https://medium.com/media/38fb4490bf97fbfdeb3caedc081b9473/href</a></iframe><h4>Wrapping up</h4><p>You can use useState for managing any component state but there are two scenarios where useReducer is more convenient:</p><ul><li>When the state keeps data which change together (like “data” and “isLoadingData”).</li><li>When you have to pass callbacks down in large component trees.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b1331bb768" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/react-usereducer-hook-2b1331bb768">React useReducer hook</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React 16.8]]></title>
            <link>https://medium.com/trabe/react-16-7-753c61ca7a1e?source=rss-85c2933da91c------2</link>
            <guid isPermaLink="false">https://medium.com/p/753c61ca7a1e</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[hooks]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-16]]></category>
            <dc:creator><![CDATA[Ceci García García]]></dc:creator>
            <pubDate>Mon, 03 Dec 2018 09:01:00 GMT</pubDate>
            <atom:updated>2019-02-06T09:36:54.874Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*wvmnYETqWUowkuP5Y2jQPA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/photos/TRggaD8mHJ4?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Fabien Bazanegue</a> on <a href="https://unsplash.com/?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The last October 25th <a href="https://twitter.com/sophiebits">Sophie Alpert</a> and <a href="https://twitter.com/dan_abramov">Dan Abramov</a> introduced the new React proposal version at the React conf 2018:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FV-QO-KO90iQ%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DV-QO-KO90iQ&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FV-QO-KO90iQ%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/57e54821f40675afc939d935fe06c450/href">https://medium.com/media/57e54821f40675afc939d935fe06c450/href</a></iframe><p>React v16.8 is meant for solving some problems that we’ve been dealing with for so long:</p><ul><li><a href="https://reactjs.org/docs/hooks-intro.html#classes-confuse-both-people-and-machines"><strong>Confusing classes</strong> for both human and machines</a>: Classes require a lot of boilerplate, force us to bind event handlers and to understand how this works in Javascript, present issues for some tools like hot reloading, decreases the compilation performance, etc.</li><li><a href="https://reactjs.org/docs/hooks-intro.html#its-hard-to-reuse-stateful-logic-between-components"><strong>Reusing logic</strong></a><strong>: </strong>React doesn’t offer a way to “attach” reusable behavior to a component. <a href="https://reactjs.org/docs/react-without-es6.html#mixins">Mixins</a> was the first workaround but we ended up concluding that <a href="https://reactjs.org/blog/2016/07/13/mixins-considered-harmful.html">mixins comes with worse problems</a> that the one we were trying to solve. Patterns like <a href="https://reactjs.org/docs/render-props.html"><em>render props</em></a> and <a href="https://reactjs.org/docs/higher-order-components.html"><em>HOCs</em></a><em> </em>solve this problem but they force us to restructure our components by surrounding them with several abstractions, which leads to the “wrapper hell”.</li><li><strong>Giant components:</strong> The more a component grows, the more we have to spread its logic and side effects through the whole component itself. This happens mainly for two reasons: lifecycle methods containing a mix of unrelated logic and the impossibility to split these components into smaller ones because the stateful logic cannot be extracted.</li></ul><p>The thing is that if we approach each problem in an isolated manner, it seems like solving one of them makes some other worst. For example trying to extract logic from a giant component leads to the wrapper hell and viceversa.</p><p>From now, we can consider the three problems described above not as isolated problems but as symptoms of the same problem: <strong>React doesn’t provide a stateful primitive simpler than a class component.</strong></p><h4>Hooks</h4><p><a href="https://reactjs.org/docs/hooks-intro.html">Hooks</a> are the new feature proposal that solves the problem:</p><ul><li><strong>Reusing logic</strong>: With Hooks, you can extract stateful logic from a component and reuse it without changing your component hierarchy.</li><li><strong>Giant components: </strong>Hooks let you split one component into smaller functions based on what pieces are logically related, rather than forcing a split based on lifecycle methods.</li><li><strong>Confusing classes: </strong>Hooks let you use React’s features without classes.</li></ul><p>Hooks are functions that let you “hook into” React state and lifecycle features from functional components. Actually hooks only work in functional components, not in class components nor in plain javascript functions except from your custom hooks (we will talk a little about this later).</p><p>Hooks are also completely opt-in and 100% backwards-compatible so we can migrate our projects and start using them seamlessly! yay!</p><h4>The State Hook: useState</h4><p>Below there is a simple counter implemented without using hooks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/774a7746f42af7e9b01262aa0fa856e1/href">https://medium.com/media/774a7746f42af7e9b01262aa0fa856e1/href</a></iframe><p>And below is the same component but refactored to use the <a href="https://reactjs.org/docs/hooks-state.html">useState hook</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/080d11871ecef0970712bd101ad139f8/href">https://medium.com/media/080d11871ecef0970712bd101ad139f8/href</a></iframe><p>So the state hook allows us to declare a “state variable” which will be preserved between renders by React. useState accepts only one argument, the initial value (value for the state variable in the initial render), and returns a pair of values: the current value and the updater function.</p><p><strong>How does the updater function </strong><strong>setState work? </strong>Much the same as the “old one”, but the new setState will replace the whole state with the argument passed instead of merging it with the old state.</p><p><strong>So how would we store our state objects? </strong>We can still do the merge manually in the updater function, but it won’t be necessary in most cases since we can use as many state variables as we want in the same component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27366c58d4f918465e39a31dd8a3bea3/href">https://medium.com/media/27366c58d4f918465e39a31dd8a3bea3/href</a></iframe><h4>Hooks can be easily <strong>extracted and reused</strong>:</h4><p>If a hook is just a function… Can it be extracted from the component? YES! 🙆🏼</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/534d60492bcd197a1186796ba2a47eee/href">https://medium.com/media/534d60492bcd197a1186796ba2a47eee/href</a></iframe><p>This way we’ve just extracted logic which uses internal state of the component, from the component itself.</p><h4>The Effect Hook: useEffect</h4><p>The <a href="https://reactjs.org/docs/hooks-effect.html">Effect Hook</a> lets you perform side effects in functional components.</p><p>If we wanted to show the counter value as the title of the page in the example above, we would have to do something like this without hooks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5620215e5f6c42855d2e394c621d28a7/href">https://medium.com/media/5620215e5f6c42855d2e394c621d28a7/href</a></iframe><p>The same example using hooks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/becbffbb8c8590515e87b010141b56ad/href">https://medium.com/media/becbffbb8c8590515e87b010141b56ad/href</a></iframe><p>By using this Hook, you tell React that your component needs to do something after render, so React will call the effect after performing the DOM updates.</p><p>By default, React runs the effect after every render including the first one, but we can tell React to skip applying it if certain values haven’t changed between re-renders. To do so, we only have to pass an array as an optional second argument to useEffect:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/287c0ad506a5e15198d2a4a3d9ef98a9/href">https://medium.com/media/287c0ad506a5e15198d2a4a3d9ef98a9/href</a></iframe><p>In some cases we need our component to be subscribed to the changes in some external source. We normally do this by subscribing it to the source on componentDidMount and unsubscribing it on componentWillUnmount:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/81aae492320b2576185dc3a593abbff2/href">https://medium.com/media/81aae492320b2576185dc3a593abbff2/href</a></iframe><p>How can we do this with hooks?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9a5e010283269eecc40c349047a9b67c/href">https://medium.com/media/9a5e010283269eecc40c349047a9b67c/href</a></iframe><p>The useEffect function can optionally return a function which is the cleanup mechanism for effects. React performs the cleanup when the component unmounts and before applying the next effects.</p><p>Pay attention to the second argument of the useEffect function in the example above. If we don’t define the second argument, the effect will be called after every re-render. To avoid so in the example, we passed an empty array as the second argument which will presumable not change over the re-renders so the effect will only be called after the first render and cleaned up when the component unmounts.</p><p>This way, we can think of useEffect Hook as componentDidMount, componentDidUpdate, and componentWillUnmount combined. Since we can use as many effects as we need, the code can be now arranged by concern instead of spread between the different lifecycle methods. I found <a href="https://twitter.com/prchdk/status/1056960391543062528">this gif in twitter</a>, which shows clearly the benefits of React Hooks, thanks to <a href="https://twitter.com/prchdk">@prchdk</a>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*6vYH5ogk15XrMHAieRf4TA.gif" /></figure><h4>Some rules with Hooks</h4><p>Hooks are JavaScript functions, but you need to follow two rules when using them:</p><ul><li><strong>Only call hooks at the top level:</strong> Don’t call Hooks inside loops, conditions or nested functions.</li><li><strong>Only call Hooks from React function components:</strong> Don’t call Hooks from regular Javascript functions except from your custom Hooks.</li></ul><h4>Custom Hooks</h4><p>A custom Hook is a JavaScript function whose name starts with ”use” and that may call other Hooks. We define custom Hooks to extract and share logic between components.</p><p>In case you didn’t notice, we’ve already wrote one in the previous section “Hooks can be easily extracted and reused”:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/534d60492bcd197a1186796ba2a47eee/href">https://medium.com/media/534d60492bcd197a1186796ba2a47eee/href</a></iframe><h4>Additional Hooks</h4><p>There are much more Hooks that we don’t cover in this post:<a href="https://reactjs.org/docs/hooks-reference.html#usecontext">useContext</a>, <a href="https://reactjs.org/docs/hooks-reference.html#usereducer">useReducer</a>, <a href="https://reactjs.org/docs/hooks-reference.html#usecallback">useCallback</a>, <a href="https://reactjs.org/docs/hooks-reference.html#useref">useRef</a>, etc. Take a look to <a href="https://reactjs.org/docs/hooks-reference.html">the Hooks API Reference doc</a> to learn more about them.</p><h4>What’s next</h4><p>Since Hooks are completely opt-in and 100% backwards-compatible, we’re already trying them in some test projects. We’ll write about our thoughts soon so stay tuned!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=753c61ca7a1e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/trabe/react-16-7-753c61ca7a1e">React 16.8</a> was originally published in <a href="https://medium.com/trabe">Trabe</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>