<?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 Arthur Visser on Medium]]></title>
        <description><![CDATA[Stories by Arthur Visser on Medium]]></description>
        <link>https://medium.com/@arthurvi?source=rss-2afbe13ab48d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*6XxDPBEaHoeQ3ifUTDWZwQ.jpeg</url>
            <title>Stories by Arthur Visser on Medium</title>
            <link>https://medium.com/@arthurvi?source=rss-2afbe13ab48d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:09:08 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@arthurvi/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[React Hooks — Using a wrapper to memoize event handlers and to prevent unnecessary re-renders]]></title>
            <link>https://blog.devgenius.io/react-hooks-using-a-wrapper-to-memoize-event-handlers-and-to-prevent-unnecessary-re-renders-9e7fbbc0f95c?source=rss-2afbe13ab48d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9e7fbbc0f95c</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[reactjs]]></category>
            <category><![CDATA[react-hook]]></category>
            <dc:creator><![CDATA[Arthur Visser]]></dc:creator>
            <pubDate>Mon, 05 Oct 2020 09:25:48 GMT</pubDate>
            <atom:updated>2020-10-05T19:19:41.997Z</atom:updated>
            <content:encoded><![CDATA[<h3>React Hooks — Using a wrapper to memoize event handlers and to prevent unnecessary re-renders</h3><p>A few weeks ago I encountered an issue with React Hooks, event handlers and unnecessary re-renders. Using classes the solution was quite trivial, using hooks, not so much.</p><p>The simplified requirements of our application:</p><ul><li>Show a list of items.</li><li>Each item has an input field.</li><li>Each item has a save button.</li><li>Changing or saving one item would not lead to a re-render of the other items.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/606/0*Q9VoU7nbyyD5XUg4.png" /></figure><p>Surely, if the render is cheap, it makes perfect sense to re-render each time. In this particular case we wanted to prevent re-renders for the other items.</p><h3>Our dataset</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8d92a09a25e67730a690f41da04b10f/href">https://medium.com/media/e8d92a09a25e67730a690f41da04b10f/href</a></iframe><h3>Why is this more difficult with React Hooks?</h3><p>The example below is a possible implementation of the ItemList component that renders all Item components.</p><p>The handleChange function is memoized using the <a href="https://reactjs.org/docs/hooks-reference.html#usecallback">useCallback</a> hook. The function is only recreated when one of the dependencies ( setItems in this case) change. Fine so far.</p><p>The problem is that the handleSave function is not memoized. On each render of ItemList a new handleSave function is created. Thus when one Item updates, all other Item component re-render too. We cannot use useCallback here as handleSave has a dependency on items. items will always change when a single item is updated.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31393c722ad5c4b5c4cfde9f69daa4d3/href">https://medium.com/media/31393c722ad5c4b5c4cfde9f69daa4d3/href</a></iframe><p>A few solutions you could try:</p><ul><li>Pass all dependencies of handleSave as parameters instead. In that case the Item component would also need to receive all these dependencies as props.</li><li>Pass the entire item object to the Item component. That would make the props equality check in Item more complex and more error prone.</li><li>With the useRef() hook, on each change to items write it to the ref and read items from the ref inside the handleSave function. Then items is not a dependency of handleSave anymore.</li><li>Keep a variable idToSave in the state. Set idToSave on save. Then trigger the save function with useEffect(() =&gt; { /* save */ }, [idToSave]), &quot;Reactively&quot;.</li></ul><p>Instead, I advocate there is a better solution. 🎉</p><h3>First things first, the Item component</h3><p>Our Item component is pretty straight forward. It has a saving and edit state. The most important thing to notice is the <a href="https://reactjs.org/docs/react-api.html#reactmemo">React.Memo</a> part. It&#39;s a higher order component that only re-renders when one of the props change.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a00f46366b35bfd923a0688795121f7/href">https://medium.com/media/7a00f46366b35bfd923a0688795121f7/href</a></iframe><h3>The trick, ItemWrapper</h3><p>By using an ItemWrapper component, we can memoize both event handlers. The result is an Item component that only re-renders when the individual item changes.</p><p>It adds an additional component to the tree, but provides value as the structure grows. It also reflects the situation we have: each item has a save functionality that requires the entire item object, but the Item component itself does not. So you might view the ItemWrapper as a provider of the save function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/663563f3ad75c8d83ccf1b9f7054a2bf/href">https://medium.com/media/663563f3ad75c8d83ccf1b9f7054a2bf/href</a></iframe><p>And ItemList will become:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b90a299941b8f76dddc2b7adf3dbaeeb/href">https://medium.com/media/b90a299941b8f76dddc2b7adf3dbaeeb/href</a></iframe><h3>One more thing…</h3><p>In this example we pass setItems to the ItemWrapper, there are better solutions. You could move handleChange and handleSave one step up, but I think a simple reducer would make this example complete.</p><p>Of course all this might feel overly complex for this “Hello World” example, but in more complex real world application this structure makes the most sense to me.</p><p>The full example can be found below, or <a href="https://codesandbox.io/s/billowing-monad-g7r4p?file=/src/App.js">here on CodeSandbox</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa976e6c6fbb5348d145b62c987f03b0/href">https://medium.com/media/aa976e6c6fbb5348d145b62c987f03b0/href</a></iframe><p><em>Originally published at </em><a href="https://www.arthurvisser.com/blog/react-hooks-memoize-event-handlers"><em>https://www.arthurvisser.com</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9e7fbbc0f95c" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/react-hooks-using-a-wrapper-to-memoize-event-handlers-and-to-prevent-unnecessary-re-renders-9e7fbbc0f95c">React Hooks — Using a wrapper to memoize event handlers and to prevent unnecessary re-renders</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[!! vs. Boolean()]]></title>
            <link>https://blog.devgenius.io/vs-boolean-cd502fbd6101?source=rss-2afbe13ab48d------2</link>
            <guid isPermaLink="false">https://medium.com/p/cd502fbd6101</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Arthur Visser]]></dc:creator>
            <pubDate>Fri, 05 Jun 2020 20:21:02 GMT</pubDate>
            <atom:updated>2020-06-08T12:30:14.488Z</atom:updated>
            <content:encoded><![CDATA[<h3>What’s the best way to answer the question “true or false?” in JavaScript</h3><h4>JavaScript does not bother you too much with types (at first), which is both a blessing and a curse. But we all know the Boolean type. Boolean variables can either be true or false. Yes or no.</h4><p>Every value in JavaScript can be translated into a boolean, true or false. Values that translate to true are truthy, values that translate to false are falsy. Simple.</p><p>This is about two ways to make that translation.</p><h3>!! and Boolean()</h3><pre>const value = &#39;hello world&#39;;<br><br>Boolean(value); // true - the boolean function<br>!!value; // true - double bang / double negation</pre><p>Which one is the best?</p><h3>Truthy and Falsy</h3><p>The conversion from any value to true or false is described by the abstract operation ToBoolean in <a href="https://www.ecma-international.org/ecma-262/5.1/#sec-9.2">section 9.2</a> of the ECMAScript® Language Specification.</p><p>All values are truthy unless they are falsy. So we only have to know the falsy ones to know the truthy ones.</p><p>Falsy values ( ToBoolean result is false):</p><ul><li>false</li><li>undefined</li><li>null</li><li>&#39;&#39; - empty string</li><li>0, -0 and 0n - zero</li><li>NaN (Not a Number)</li></ul><p>So truthy examples are ( ToBoolean result is true):</p><ul><li>true</li><li>{}</li><li>&quot;false&quot;</li></ul><p>In certain operations (like in if/else statements), JavaScript will automagically convert a value into true or false by using these truthy/falsy rules. This is type coercion. Read more about it in the <a href="https://developer.mozilla.org/en-US/docs/Glossary/Type_coercion">MDN web docs</a>.</p><p>An example:</p><pre>const student = {<br>  answer: &#39;correct&#39;<br>};<br><br>// student.answer is truthy, <br>// so we get true and the answer is verified.<br>if (student.answer) {<br>  verifyAnswer();<br>}</pre><p>Let’s take a closer look at Boolean() and !!.</p><h3>Boolean()</h3><p>Always use Boolean as a function. So do this:</p><pre>const trueOrFalse = Boolean(&#39;yes&#39;); // true</pre><p>Do not do this!</p><pre>const trueOrFalse = new Boolean(&#39;yes&#39;);</pre><p>trueOrFalse now becomes a Boolean object, not a boolean value! And we know {} is not falsy, so new Boolean() is always truthy. This can lead to unexpected behaviour.</p><pre>typeof new Boolean(false); // object - {...}<br>typeof Boolean(false); // boolean - false<br><br>if (new Boolean(false)) {<br>  // This will run! Because the Boolean {} is coerced to true.<br>}<br><br>if (Boolean(false)) {<br>  // This will not run! Which is probably what you want.<br>}</pre><p>More on this in the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean">MDN web docs</a> about the Boolean object.</p><h3>!!</h3><p>The logical NOT operator: !, the exclamation mark, also called a &quot;bang&quot;. It translates truthy values to false and falsy values to true. Get it?</p><p>Doing that twice translates truthy values to true and falsy values to false.</p><p>This is double negation. If a statement is true, then it is <em>not</em> the case that the statement is <em>not</em> true. Complicated, but just remember !! results in a boolean. true for truthy values, false for falsy values.</p><pre>const value = &#39;hello&#39;;<br><br>// Step 1. First bang.<br>const step1 = !value; // step1 = false (NOT truthy = false)<br><br>// Step 2. Second bang.<br>const step2 = !step1; // step2 = true (NOT false = true) <br><br>// Thus<br>console.log(step1, !value); // false<br>console.log(step2, !!value); // true</pre><h3>What do experts say?</h3><p>The excellent <a href="https://airbnb.io/javascript/#coercion--booleans">Airbnb style guide</a> prefers the double bang / !!.</p><pre>// good<br>const hasAge = Boolean(age);<br><br>// best<br>const hasAge = !!age;</pre><p>In <a href="https://github.com/getify/You-Dont-Know-JS/blob/1st-ed/types%20%26%20grammar/ch4.md">chapter 4</a> of the book You don’t know JS by Kyle Simpson both options are considered good.</p><pre>const a = &quot;42&quot;;<br><br>// bad (will fail!):<br>if (a == true) {<br>	// ..<br>}<br><br>// also bad (will fail!):<br>if (a === true) {<br>	// ..<br>}<br><br>// good enough (works implicitly):<br>if (a) {<br>	// ..<br>}<br><br>// better (works explicitly):<br>if (!!a) {<br>	// ..<br>}<br><br>// also great (works explicitly):<br>if (Boolean( a )) {<br>	// ..<br>}</pre><h3>Why is explicit conversion better than implicit conversion?</h3><p>There are multiple reasons to prefer explicit over implicit conversion.</p><p>First, it’s easier to see what’s intended. Good code makes you assume less and understand more. By using Boolean() or !! it becomes clear that the ToBoolean conversion is intented.</p><p>It prevents unexpected results, for example with the || and &amp;&amp; operators.</p><pre>// isValid becomes &#39;yup&#39;, not true as with for example PHP.<br>const isValid = &#39;yes&#39; &amp;&amp; &#39;yup&#39;; </pre><pre>// isValid becomes &#39;yes&#39;, not true.<br>const isValid = &#39;yes&#39; || &#39;no&#39;;</pre><p>Better:</p><pre>// isValid becomes true! Probably what we want.<br>const isValid = Boolean(&#39;yes&#39; &amp;&amp; &#39;yup&#39;); </pre><pre>// isValid becomes true!<br>const isValid = Boolean(&#39;yes&#39; || &#39;no&#39;);</pre><p>The || and &amp;&amp; operators result in one of the two variables, not necessarily a boolean. By using Boolean() we make that explicit, one thing less to think about!</p><p>And it’s also possible with a bang bang 🔫 🔫.</p><pre>!!(&#39;yes&#39; &amp;&amp; &#39;yup&#39;); // results in true</pre><h3>Performance</h3><p>I did a quick <a href="https://jsperf.com/double-bang-or-boolean">jsperf test</a>. I consider both operations to be basic and thus “fast enough” for most use cases. Readability and clarity would be more important to me. Still it’s interesting to see the results.</p><p>I always thought the double bang was the way to go in terms of performance. Although with a double bang you have to make two conversions (falsy/truthy -&gt; boolean -&gt; boolean). But maybe browsers have some optimizations built-in, for either way.</p><p>Safari, !! wins.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1LO21WV4ovLu4shp41wQ4Q.png" /></figure><p>Firefox, a draw.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*zPBL2UjKvN1e9gg2_kP61g.png" /></figure><p>Chromium, Boolean() wins (slightly).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ao5GwyzUQGXZ5m3JlKhwHw.png" /></figure><p>No clear winner. Check your usage share of webbrowsers. 🙃</p><h3>Conclusion</h3><p>Just pick one! 😁</p><p>Personally I prefer Boolean(). I like the way this says it all: &quot;Give me a boolean&quot;. Most developers would understand this immediately, !! is less clear (but easy enough to remember).</p><p>In terms of performance it doens’t really matter, but you can type !! quicker. A valid argument for the lazy ones.</p><p>And a bonus point for Boolean(), it&#39;s a function. So you can do stuff like this:</p><pre>const answers = [&#39;&#39;, &#39;correct&#39;, &#39;&#39;, &#39;good&#39;];<br>const completedAnswers = answers.filter(Boolean);</pre><p>If you want to read more about types in JavaScript I can greatly recommend the first book of <a href="https://github.com/getify/You-Dont-Know-JS/tree/1st-ed/types%20%26%20grammar">You Don’t Know JS, Types &amp; Grammar</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*8AcIyzgCiPl6P7-nKGLOGw.png" /></figure><p><em>Originally published at </em><a href="https://gist.github.com/arthurvi/66cb1e2bcfc92f99f465e0db04264367"><em>http://github.com</em></a><a href="https://gist.github.com/arthurvi/66cb1e2bcfc92f99f465e0db04264367">https://gist.github.com/arthurvi/66cb1e2bcfc92f99f465e0db04264367</a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cd502fbd6101" width="1" height="1" alt=""><hr><p><a href="https://blog.devgenius.io/vs-boolean-cd502fbd6101">!! vs. Boolean()</a> was originally published in <a href="https://blog.devgenius.io/">Dev Genius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>