<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Karolis Ramanauskas</title>
    <description>The latest articles on Forem by Karolis Ramanauskas (@superkarolis).</description>
    <link>https://forem.com/superkarolis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F75828%2F64616320-0f51-45b6-bf58-027adc496786.jpeg</url>
      <title>Forem: Karolis Ramanauskas</title>
      <link>https://forem.com/superkarolis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/superkarolis"/>
    <language>en</language>
    <item>
      <title>Keeping up in the JavaScript world 🏃‍</title>
      <dc:creator>Karolis Ramanauskas</dc:creator>
      <pubDate>Fri, 03 Aug 2018 16:30:19 +0000</pubDate>
      <link>https://forem.com/superkarolis/keeping-up-in-the-javascript-world--50pl</link>
      <guid>https://forem.com/superkarolis/keeping-up-in-the-javascript-world--50pl</guid>
      <description>&lt;p&gt;JavaScript fatigue has been on the rise for a while now. You often hear developers complain that the front-end world is moving too fast, there are too many frameworks to choose from and too many things to learn all at once. While I agree there is truth to that, I also know that continuous learning and improvement is an inevitable aspect of being a programmer. You either accept it or go stale.&lt;/p&gt;

&lt;p&gt;This post is not about dealing with JavaScript fatigue. Instead, it assumes that you are ready for the challenge to keep up with the front-end world despite the risk of experiencing the so-called JavaScript fatigue. If you are, this post will introduce you to the best ways and resources of doing so.&lt;/p&gt;

&lt;p&gt;But before we dive into the nitty-gritty, we have to remember that none of us were born with natural talent and just knew how to program perfectly the first time we used a computer. Some of us learn new programming concepts faster, some take it slower. But none of us can progress as professionals without putting deliberate effort into it. So take your time, find what works for you and experiment with the following ways to improve as a developer.&lt;/p&gt;

&lt;h2&gt;
  
  
  📚 Consume curated content
&lt;/h2&gt;

&lt;p&gt;One of the best ways to find out what's happening in the JavaScript world is to let others do the hard work and curate the content for you. As a result, you will only get the news that are worth your attention. Below are some of the recommended resources to follow.&lt;/p&gt;

&lt;h3&gt;
  
  
  Newsletters:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://javascriptweekly.com/"&gt;JavaScript Weekly&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://frontendfront.com/"&gt;Front End Front&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://frontendnewsletter.com/"&gt;Front End Newsletter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://esnextnews.com/"&gt;ES.next News&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://adripofjavascript.com/index.html"&gt;A Drip of JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Podcasts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://syntax.fm/"&gt;Syntax.fm&lt;/a&gt; (so far my favorite podcast)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://devchat.tv/js-jabber/"&gt;JavaScript Jabber&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://shoptalkshow.com/"&gt;Show Talk Show&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://frontendhappyhour.com/"&gt;Front End Happy Hour&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.thisdot.co/modern-web"&gt;Modern Web&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Social media
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://twitter.com/JavaScriptDaily"&gt;JavaScript Daily&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://twitter.com/jswhizz"&gt;JavaScript Wisdom&lt;/a&gt; (my own Twitter handle tweeting bite-sized JavaScript wisdom every day)&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/JavascriptIO"&gt;We ❤ JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/JavaScript"&gt;JavaScript&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  👁️ Find the learning style that fits you
&lt;/h2&gt;

&lt;p&gt;It's great to continously learn, go to conferences, do tutorials and self-improve in whatever ways you can. But if you notice that one way of learning does not work for you, try others. The three primary modes of learning for developers are through blog posts, books and video courses. Experiment with each one of them, see which one works best for you and use it as much as possible.&lt;/p&gt;

&lt;p&gt;Personally, I use all three learning resources. This is because each one of them focuses on different levels of depth in regards to specific topics. Blog posts tend to focus deeply on a very narrow topic in a trendy niche, or give a shallow overview of a wider topic. Courses tend to cover medium-sized topics in a somewhat comprehensive manner. Just like blog posts, most courses also tend to cover what's hot. And books often tend to focus on fundamental aspects of programming meaning that the things you learn in them tend to be more foundational in nature than the ones learnt from video courses and blog posts. Thus, I believe it's important to combine all of the resources to become a well-rounded programmer.&lt;/p&gt;

&lt;p&gt;The best video based tutorials for JavaScript developers can be found on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://egghead.io/"&gt;egghead.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://frontendmasters.com/courses/"&gt;Frontend Masters&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://udemy.com/"&gt;Udemy&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.pluralsight.com/"&gt;Pluralsight&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some of the best places to learn from blog posts are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/"&gt;dev.to&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://scotch.io/"&gt;scotch.io&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/"&gt;Medium&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://code.tutsplus.com/categories/javascript"&gt;Envato&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And in regards to books, I just tend to look at Amazon reviews or recommendations from others and buy them whenever available.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔬 Experiment recklessly
&lt;/h2&gt;

&lt;p&gt;Sometimes it's a good idea to not only take the time to learn about shiny new libraries or frameworks but also explore existing ones deeper. My recommendation would be to experiment more with frameworks and libraries that you already use and push them to their limits. Try doing the same thing in a different way, create a performance benchmark, try doing some silly things, play around. This will force you to get to know your tools better and consequently become a better programmer. The key here is to not be afraid, the code will not bite you back.&lt;/p&gt;

&lt;h2&gt;
  
  
  🚀 Seek mentorship
&lt;/h2&gt;

&lt;p&gt;While it's great to self-learn and find your own path, having a good mentor can expedite your progress ten-fold. Receiving mentorship just means that you will be getting valuable advice from someone more experienced than you instead of seeking advice online from random strangers which may or may not have your best interests in mind. All of the greatest achievers had mentors in one way or another. Why not have one as a programmer?&lt;/p&gt;

&lt;p&gt;Finding a mentor may seem daunting but it doesn't have to be complicated. First, just try to improve on your own. Eventually, you will get better and meet like-minded individuals in conferences, meetups, online forums, etc. Some of them will be further along than you as programmers. All you have to do now is try to get to know them better, establish a relationship, see if you can help them in any way and only once in a while ask for advice on a specific topic where you want to improve. Finding a mentor is not easy but it's not complicated either.&lt;/p&gt;

&lt;p&gt;While I haven't tried it yet, it's also certainly possible to try out paid solutions out there such as &lt;a href="https://www.codementor.io/"&gt;codementor&lt;/a&gt;. If you give it a try, let me know how it went 😊&lt;/p&gt;

&lt;p&gt;☝️And remember that you certainly can grow as a programmer without a mentor, it's just easier with one.&lt;/p&gt;

&lt;h2&gt;
  
  
  🌎 Learn other languages
&lt;/h2&gt;

&lt;p&gt;Sometimes learning is not just about getting to know the next "new thing". It's also about thinking in different ways outside of your normal thought patterns.&lt;/p&gt;

&lt;p&gt;There is a saying that "when you are a hammer, everything looks like a nail". Similarly, when you are a JavaScript developer and all you know is JavaScript, all the code that you write will be somewhat constrained by the patterns that you have learnt as a JavaScript developer. However, if you are exposed to other languages, you may quickly discover new better ways to solve the existing problems that you have.&lt;/p&gt;

&lt;p&gt;"The Pragmatic Programmer", legendary book about software development craft confirms the same:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Learn at least one new language every year. Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  💬 Expose yourself to others
&lt;/h2&gt;

&lt;p&gt;Whilst having a mentor is great for getting answers to questions that are specifically relevant to you, it's also important to expose yourself to others and get to know what and how they think, what problems they face and how they go about them. Just like learning new languages, meeting other developers will broaden your thinking and may further your career in unexpected ways. Some of the best places to meet other developers can be found below:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Slack/IRC/Gitter groups&lt;/li&gt;
&lt;li&gt;Online communities such as &lt;a href="https://dev.to/"&gt;dev.to&lt;/a&gt;, &lt;a href="https://www.sitepoint.com/community/"&gt;sitepoint&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.karolisram.com/keeping-up-in-the-front-end-world/linkedin.com"&gt;Stack Overflow&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.meetup.com/"&gt;Meetups&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://twitter.com/"&gt;Twitter&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://confs.tech/"&gt;Conferences&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.reddit.com/r/javascript/"&gt;Reddit&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Water-cooler chats&lt;/li&gt;
&lt;li&gt;Co-working spaces&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🏗️ Build stuff
&lt;/h2&gt;

&lt;p&gt;It's widely agreed that some of the best learning in programming comes when you build stuff. While it's fine and dandy to consume tutorials, you will never know how well you understand the material until you apply it in your own work. So, the best strategy to use whilst learning is to have an end goal in mind. Then go learn from the best tutorials, books or courses you can find and try to apply it in your job or side-project as soon as possible. Building your own stuff forces you to deal with real problems rather than hypothetical ones which is what programming is all about.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎤 Teach
&lt;/h2&gt;

&lt;p&gt;Teaching is one of my favorite things to do as a programmer. It's a win-win situation for everyone involved. The students get to learn from someone more knowledgeable than them who practices what he/she teaches in real life situations. And the teacher gets to solidify his/her knowledge through teaching. As an added bonus, by teaching one also gets a chance to look at the concepts being taught from the fresh perspective of a student. This can be powerful by potentially providing an opportunity to re-think of things you already know in new ways. Some of the ways one can start teaching now:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Blog&lt;/li&gt;
&lt;li&gt;Screencast&lt;/li&gt;
&lt;li&gt;Give talks&lt;/li&gt;
&lt;li&gt;Podcast&lt;/li&gt;
&lt;li&gt;Answer on StackOverflow&lt;/li&gt;
&lt;li&gt;Help people on Gitter/Slack&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By the way, a lot of people shy away from teaching as they think they are not yet good enough to teach. However, it's most likely that there are people who are even less knowledgeable than you and would benefit from your knowledge and perspective about a given topic. It's like a hierarchy of knowledge. There is almost always one step below that you can teach to. So, don't be afraid to teach and just do it 🤜&lt;/p&gt;

&lt;h2&gt;
  
  
  👐 Contribute to open-source
&lt;/h2&gt;

&lt;p&gt;One good way to be up-to-date with what's happening in the JavaScript world is by actually contributing to it. This way you will get exposed to other developers code and inevitably some of the ways they do things will rub off on you as well. Just like with mentorship, one can be just fine developer without it but you will be even greater if you do indeed do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  🤔 Ask why
&lt;/h2&gt;

&lt;p&gt;Sometimes you don’t need to just keep on keeping up. It’s also worthwhile to stop once in a while and ask yourself “why” on the things you already know i.e. why did I choose to use Redux Thunk? What problem does it solve for me? Why not use something else? This will force you to be conscious of your decisions and not just randomly follow recommendations that you read online.&lt;/p&gt;

&lt;h2&gt;
  
  
  📅 Wait
&lt;/h2&gt;

&lt;p&gt;Now once we covered all the ways to keep up in the JavaScript world, I will say one last word of caution. And that is the fact that you don’t have to keep up with everything! There, I said it. You can be a good developer just fine without knowing about every new framework and library out there.&lt;/p&gt;

&lt;p&gt;One way to separate signal from the noise is to wait until a given technology has reached enough popularity and maturity to be worth having a look at. So it's fine to follow what is happening in the JavaScript world but only jump on the bandwagon once a certain technology has proved itself.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔥 Summary
&lt;/h2&gt;

&lt;p&gt;We have covered a lot of ways one can keep up in the JavaScript world. It can seem overwhelming. It almost seems overwhelming to me. The key is to choose what works for you and have regular intervals of time in which you deliberately focus on improving as a programmer. So, even if you don't contribute to open-source, perhaps you are active in online communities where you help others. For others this may not work but something else will. Don't be afraid to experiment and find the way to keep up-to-date and yet still keep sane. Good luck and let me know your own resources and strategies to keep up with all that is happening in the JavaScript world ✌️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>learning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Higher-Order Components (HOC) React Pattern</title>
      <dc:creator>Karolis Ramanauskas</dc:creator>
      <pubDate>Sun, 29 Jul 2018 15:59:01 +0000</pubDate>
      <link>https://forem.com/superkarolis/higher-order-components-hoc-react-pattern-5ejp</link>
      <guid>https://forem.com/superkarolis/higher-order-components-hoc-react-pattern-5ejp</guid>
      <description>&lt;p&gt;Today I decided to get more familiar with Higher-Order Components (HOC) commonly used in libraries such as Redux (&lt;code&gt;connect&lt;/code&gt;), react-i18next (&lt;code&gt;translate&lt;/code&gt;) or react-router (&lt;code&gt;withRouter&lt;/code&gt;). I have often used them as part of aforementioned libraries but never got the full grasp behind their magic. Today is the day to get underneath the carpet and see how they really work.&lt;/p&gt;

&lt;p&gt;But before we begin, let's define what a Higher-Order Component is. According to &lt;a href="https://reactjs.org/docs/higher-order-components.html" rel="noopener noreferrer"&gt;React docs&lt;/a&gt;, Higher-Order Component is an advanced React pattern consisting of a function that takes a base (wrapper) component as an argument and returns a new, enhanced component. The best thing about it is that it allows to reuse component logic in a clean compositional way.&lt;/p&gt;

&lt;p&gt;The abstract code for a HOC would look somewhat like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;higherOrderComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="c1"&gt;// create new component from old one and update&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;EnhancedComponent&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In a more concrete form, a Higher-Order Component always takes a form similar to this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;higherOrderComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HOC&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;
        &lt;span class="nx"&gt;enhancements&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;

        &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BaseComponent&lt;/span&gt; &lt;span class="nx"&gt;newProp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;newPropValue&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;HOC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Simple example
&lt;/h2&gt;

&lt;p&gt;To get a better understanding, let's take a look at a simple example of a Higher-Order Component. Imagine we want to have several components that all get set random background value each time the component is rendered.&lt;/p&gt;

&lt;p&gt;We do this by first defining a Higher-Order Component function called &lt;code&gt;withColor&lt;/code&gt;. It accepts &lt;code&gt;BaseComponent&lt;/code&gt; as a parameter and returns the enhanced component. &lt;code&gt;EnhancedComponent&lt;/code&gt; that gets returned consists of a &lt;code&gt;getRandomColor&lt;/code&gt; and &lt;code&gt;render&lt;/code&gt; methods. Within the &lt;code&gt;render&lt;/code&gt; method we return &lt;code&gt;BaseComponent&lt;/code&gt; that gets assigned a new &lt;code&gt;color&lt;/code&gt; prop and by taking advantage of &lt;code&gt;{...this.props}&lt;/code&gt; desctructuring, &lt;code&gt;BaseComponent&lt;/code&gt; also gets access to all the props which were passed to the &lt;code&gt;BaseComponent&lt;/code&gt; from outside the HOC.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;BaseComponent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EnhancedComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;getRandomColor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;letters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;0123456789ABCDEF&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;letters&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BaseComponent&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getRandomColor&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;EnhancedComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;withColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To use the newly created HOC, we just have to export it with our base component passed as an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;withColor&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./withColor&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ColoredComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{{&lt;/span&gt; &lt;span class="na"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="p"&gt;}}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;withColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ColoredComponent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, we render the new enhanced component the same way as we would render a regular component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ColoredComponent&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./ColoredComponent&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ColoredComponent&lt;/span&gt; &lt;span class="nx"&gt;someProp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Prop 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ColoredComponent&lt;/span&gt; &lt;span class="nx"&gt;someProp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Prop 2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ColoredComponent&lt;/span&gt; &lt;span class="nx"&gt;someProp&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Prop 3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that all the props defined at this level such as &lt;code&gt;someProp&lt;/code&gt; will be passed further down the line via &lt;code&gt;{...this.props}&lt;/code&gt; and new props such as &lt;code&gt;color&lt;/code&gt; in the above example are defined explicitly from within the HOC.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical example
&lt;/h2&gt;

&lt;p&gt;The previous example was a bit contrived. We could, for example, have used a utility function that generates random color, and then call it from within each component. The end result would have been the same with less code. Let's better look at an example where Higher-Order Component pattern is indeed the most elegant solution.&lt;/p&gt;

&lt;p&gt;In the following example we will develop a Higher-Order Component that accepts &lt;code&gt;BaseComponent&lt;/code&gt; and API URL to fetch the data that it needs. While the data is loading, it will show a loading state, and once the data is loaded we will display whatever &lt;code&gt;BaseComponent&lt;/code&gt; renders based on the data. The end result will look as follows.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.karolisram.com%2Fcontent%2Fimages%2F2018%2F07%2Floading_gif.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.karolisram.com%2Fcontent%2Fimages%2F2018%2F07%2Floading_gif.gif" alt="HOC example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To get started, we will first create a Higher-Order Component function. We will call it &lt;code&gt;withLoader&lt;/code&gt; and initially set the &lt;code&gt;data&lt;/code&gt; property of component's state to &lt;code&gt;null&lt;/code&gt;. Once the component has mounted we will start fetching the data and when that's done, set the &lt;code&gt;data&lt;/code&gt; property to the returned response.&lt;/p&gt;

&lt;p&gt;As already mentioned, whilst the state is &lt;code&gt;null&lt;/code&gt; we will show a loading state. And once we have the data fetched, we will return &lt;code&gt;BaseComponent&lt;/code&gt; which in return will render markup based on the returned data. The Higher-Order Component function looks as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;withLoader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;BaseComponent&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EnhancedComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Loading&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;BaseComponent&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;EnhancedComponent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;withLoader&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The components that use this HOC are fairly straightforward. We simply have to take the data from the &lt;code&gt;props&lt;/code&gt; and use it as needed. In the example below we use the data to show a list of users.&lt;/p&gt;

&lt;p&gt;As usual, we first need to import the Higher-Order Component function and pass the component as function argument. Along with it we also pass the API URL from which the data is fetched. See the code for a base component below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;withLoader&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./withLoader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;Users&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;}&amp;lt;/u&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;withLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Users&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, we use another component to show a list of posts. The code is pretty much the same except for the component's name and API URL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;withLoader&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./withLoader&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Posts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="na"&gt;Posts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ul&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;li&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/li&amp;gt;&lt;/span&gt;&lt;span class="se"&gt;)&lt;/span&gt;&lt;span class="sr"&gt;}&amp;lt;/u&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;withLoader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Posts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://jsonplaceholder.typicode.com/posts/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all that's left is to render the components. In order to do that we don't have to do anything special. Just import the components and render them where needed as usual. That's all 😃&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Users&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Posts&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Users&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Posts&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  HOC caveats 🤔
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;HOC should always be a pure function. That means that the same enhanced component should always be returned with the same base component passed as a parameter.&lt;/li&gt;
&lt;li&gt;Don’t use HOCs inside the render method. This makes React's reconciliation algorithm think that a new component is redeclared within each render, causing the whole subtree to be unmounted rather than just checked for differences.&lt;/li&gt;
&lt;li&gt;Static methods do not get copied implicitly. This needs to be done explicitl. A good way to do it is with &lt;code&gt;hoist-non-react-statics&lt;/code&gt; package.&lt;/li&gt;
&lt;li&gt;Refs don’t get passed through.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Summary 🔥🔥🔥
&lt;/h2&gt;

&lt;p&gt;A Higher-Order Component has access to all the default React API, including state and the lifecycle methods. This allows to reuse logic in a very conscise way and make your code more elegant. As we have seen, it can be used for a variety of use cases but sometimes it's not the most conscise solution. Sometimes a simple utility function or a smart parent component is all that's needed. So just use Higher-Order Components at your best judgement and don't over-engineer things where not needed 😊&lt;/p&gt;

</description>
    </item>
    <item>
      <title>React v16.4.0 - Pointer events</title>
      <dc:creator>Karolis Ramanauskas</dc:creator>
      <pubDate>Fri, 25 May 2018 11:37:57 +0000</pubDate>
      <link>https://forem.com/superkarolis/react-v1640---pointer-events-4blk</link>
      <guid>https://forem.com/superkarolis/react-v1640---pointer-events-4blk</guid>
      <description>&lt;p&gt;On May 23rd, 2018 a &lt;a href="https://github.com/facebook/react/releases/tag/v16.4.0"&gt;new minor release v16.4.0&lt;/a&gt; of React has been launched. Overall, it's a rather uneventful release mostly concerned with fixing a variety of bugs. Quite notably though, it adds support for &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events"&gt;pointer events&lt;/a&gt; which has been an often requested feature for almost the entire existence of React.&lt;/p&gt;

&lt;p&gt;Pointer events, in essence, are very similar to mouse events (mousedown, mouseup, etc.) but are hardware-agnostic and thus can handle all input devices such as a mouse, stylus or touch. This is great since it removes the need for separate implementations for each device and makes authoring for cross-device pointers easier.&lt;/p&gt;

&lt;h2&gt;
  
  
  Pointer events
&lt;/h2&gt;

&lt;p&gt;The API of pointer events works in the same manner as existing various event handlers. Pointer events are added as attributes to React component and are passed a callback that accepts an event. Inside the callback we process the event. The following pointer events have been added to React DOM:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;onPointerDown&lt;/li&gt;
&lt;li&gt;onPointerMove&lt;/li&gt;
&lt;li&gt;onPointerUp&lt;/li&gt;
&lt;li&gt;onPointerCancel&lt;/li&gt;
&lt;li&gt;onGotPointerCapture&lt;/li&gt;
&lt;li&gt;onLostPointerCapture&lt;/li&gt;
&lt;li&gt;onPointerEnter&lt;/li&gt;
&lt;li&gt;onPointerLeave&lt;/li&gt;
&lt;li&gt;onPointerOver&lt;/li&gt;
&lt;li&gt;onPointerOut&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When pointer events are fired is well explained on &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Pointer_events"&gt;MDN Pointer events documentation&lt;/a&gt; so keep it as a reference for in-depth explanation of each event. Note that at the time of this writing, pointer events are not supported by Safari.&lt;/p&gt;

&lt;p&gt;For an example of how pointer events can be incorporated on your own application, have a look at the below example by &lt;a href="https://github.com/philipp-spiess"&gt;Philip Spiess&lt;/a&gt;. I added &lt;a href="https://github.com/jquery/PEP"&gt;PEP&lt;/a&gt; polyfill to also make it work on Safari. Note that &lt;code&gt;onGotPointerCapture&lt;/code&gt; and &lt;code&gt;onLostPointerCapture&lt;/code&gt; events still do not work even with the polyfill.&lt;/p&gt;

&lt;p&gt;Check out this &lt;a href="https://codesandbox.io/embed/1wrqxkxl64"&gt;demo on Code Sandbox&lt;/a&gt; to get a glimpse of what's possible with new pointer events. Hint: try dragging the circle on various devices.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Can (aﾠ == 1 &amp;&amp; a == 2 &amp;&amp; ﾠa == 3) equal true?</title>
      <dc:creator>Karolis Ramanauskas</dc:creator>
      <pubDate>Sat, 21 Apr 2018 10:56:40 +0000</pubDate>
      <link>https://forem.com/superkarolis/can-a--1--a--2--a--3-equal-true-3bo2</link>
      <guid>https://forem.com/superkarolis/can-a--1--a--2--a--3-equal-true-3bo2</guid>
      <description>

&lt;p&gt;Here is a JavaScript trivia question. Can the expression &lt;code&gt;(aﾠ == 1 &amp;amp;&amp;amp; a == 2 &amp;amp;&amp;amp; ﾠa == 3)&lt;/code&gt; ever evaluate to &lt;code&gt;true&lt;/code&gt;? Well, today I found out it can. Run the below code and see for yourself.&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
      &lt;iframe height="550px" src="https://repl.it/repls/ProperMediocreRepo?lite=true"&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;p&gt;Now let's look at seemingly the same code below. Try to run it. This time the expression does not evaluate to &lt;code&gt;true&lt;/code&gt;. So what changed?&lt;/p&gt;


&lt;div class="ltag__replit"&gt;
      &lt;iframe height="550px" src="https://repl.it/repls/SufficientKindSolidstatedrive?lite=true"&gt;&lt;/iframe&gt;
    &lt;/div&gt;

&lt;p&gt;The difference is actally really simple and much less mysterious once you figure out what's happening. Apparently, there is a Unicode character called &lt;a href="https://www.compart.com/en/unicode/U+FFA0"&gt;Halfwidth Hangul Filler&lt;/a&gt; which is a Korean symbol and is, for all intents and purposes, invisible. So the reason why the first code snippet evaluates to &lt;code&gt;true&lt;/code&gt; is because the three variables &lt;code&gt;a&lt;/code&gt; are in fact three distinct variables. One of them is prepended with halfwidth hangul filler, another one is left intact and the third one is appended with the hidden character.&lt;/p&gt;

&lt;p&gt;Now next time someone asks you whether &lt;code&gt;(aﾠ == 1 &amp;amp;&amp;amp; a == 2 &amp;amp;&amp;amp; ﾠa == 3)&lt;/code&gt; can ever equal to &lt;code&gt;true&lt;/code&gt;, you can answer them that yes, indeed it can (with a small caveat) 😀&lt;/p&gt;


</description>
      <category>interviewquestions</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Binding in React</title>
      <dc:creator>Karolis Ramanauskas</dc:creator>
      <pubDate>Sun, 25 Mar 2018 10:01:58 +0000</pubDate>
      <link>https://forem.com/superkarolis/binding-in-react-2i5g</link>
      <guid>https://forem.com/superkarolis/binding-in-react-2i5g</guid>
      <description>&lt;p&gt;Before the release of React v0.13 using methods in React components was very straightforward since the traditional &lt;code&gt;React.createClass&lt;/code&gt; automatically bounded user defined methods to the correct &lt;code&gt;this&lt;/code&gt; context.&lt;/p&gt;

&lt;p&gt;However, since the introduction of ES6 classes in React components, the methods are no longer automatically bound. As a result, we have at least four ways to handle &lt;code&gt;this&lt;/code&gt; context in React. Let’s consider the advantages and disadvantages of each.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bind in render
&lt;/h2&gt;

&lt;p&gt;This approach works by allocating &lt;code&gt;this&lt;/code&gt; to a given function on each &lt;code&gt;render&lt;/code&gt; call. The downside of this approach is that the function is reallocated on each render. Whilst for most applications the performance implications of this are negligible, it's still something to keep in mind.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LogThis&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Arrow functions in render
&lt;/h2&gt;

&lt;p&gt;This approach makes use of ES6 fat-arrow functions which help preserve the context of &lt;code&gt;this&lt;/code&gt; by using lexical scoping. For those non-familiar with lexical scoping, it just means that a function uses &lt;code&gt;this&lt;/code&gt; from the code that contains the arrow function.&lt;/p&gt;

&lt;p&gt;Since &lt;code&gt;this&lt;/code&gt; in &lt;code&gt;render&lt;/code&gt; function always refers to the containing React component, and fat-arrow function uses lexical scoping, &lt;code&gt;handleClick&lt;/code&gt; retains the component's &lt;code&gt;this&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It stil has the downside of the previous approach in that a diffrent callback is created on each rerender and thus may cause performance issues.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LogThis&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Bind in constructor
&lt;/h2&gt;

&lt;p&gt;One way to avoid performance downsides of previous two approaches is to bind in the constructor. This approach is currently recommended by the &lt;a href="https://reactjs.org/docs/handling-events.html"&gt;official React documentation&lt;/a&gt; and it's also the approach I have adopted on my projects.&lt;/p&gt;

&lt;p&gt;The main downside of this approach is having to repeatedly bind each function in the constructor. If there are several functions in the component to be bound, it may start looking ugly quick.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LogThis&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello, world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Class fields
&lt;/h2&gt;

&lt;p&gt;The final way to bound &lt;code&gt;this&lt;/code&gt; is by using experimental &lt;a href="https://babeljs.io/docs/plugins/transform-class-properties/"&gt;public class fields syntax&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This approach is the most superior of all because it avoids the performance issues of reallocating functions on each render. And it also removes unnecessary repetition when binding functions in the constructor.&lt;/p&gt;

&lt;p&gt;One notable disadvatange of this approach is that by declaring methods in such a way, &lt;code&gt;handleClick&lt;/code&gt; is not declared in the prototype and thus is impossible to be called via &lt;code&gt;super.handleClick&lt;/code&gt; from the derived class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LogThis&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Extra: use react-autobind
&lt;/h2&gt;

&lt;p&gt;The final extra and from what I can tell, a non-conventional way to handle binding in React ES6 classes is by using npm module &lt;a href="https://www.npmjs.com/package/react-autobind"&gt;&lt;code&gt;react-autobind&lt;/code&gt;&lt;/a&gt;. It has all the advantanges of binding in constructor, plus you get to avoid repetition by having to bind each function separately.&lt;/p&gt;

&lt;p&gt;The disadvantage is that an additional npm module has to be imported and you still have to call &lt;code&gt;autoBind&lt;/code&gt; in the constructor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;autoBind&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-autobind&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;LogThis&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;autoBind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;this is:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;I personally see no real benefits of using binding or arrow functions in render. There are no real readability benefits as far as I'm concerned and the performance is negatively impacted, even if negligibly.&lt;/p&gt;

&lt;p&gt;Thus, if you are willing to use Babel &lt;code&gt;stage-2&lt;/code&gt; preset and will not have to call a parent function from derived class, I would recommend using class fields approach to binding.&lt;/p&gt;

&lt;p&gt;Otherwise, I would recommend binding in the constructor either by binding each function manually, or using &lt;code&gt;react-autobind&lt;/code&gt; module. Both are fine and comes down to personal preference. If there are several functions to be bound, I would use &lt;code&gt;react-autobind&lt;/code&gt; and if it's just one or two, go with the conventional approach.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>binding</category>
    </item>
  </channel>
</rss>
