<?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: Franco D'Alessio</title>
    <description>The latest articles on Forem by Franco D'Alessio (@francodalessio).</description>
    <link>https://forem.com/francodalessio</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%2F357813%2F0dd39184-6866-4fea-abff-67dea926feb1.jpeg</url>
      <title>Forem: Franco D'Alessio</title>
      <link>https://forem.com/francodalessio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/francodalessio"/>
    <language>en</language>
    <item>
      <title>Yes, your users care about how you write code</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Mon, 28 Sep 2020 13:09:55 +0000</pubDate>
      <link>https://forem.com/francodalessio/yes-your-users-care-about-how-you-write-code-2ek9</link>
      <guid>https://forem.com/francodalessio/yes-your-users-care-about-how-you-write-code-2ek9</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published in &lt;a href="https://francodalessio.com/users-care-about-how-you-write-code/"&gt;my personal site&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The experience of the user is the only thing that matters?
&lt;/h2&gt;

&lt;p&gt;It's a common thought and a widespread idea that the only thing that matters is the user experience. If a certain product is well designed, the user will have a &lt;strong&gt;good experience&lt;/strong&gt; and be able to &lt;strong&gt;accomplish his/her goal&lt;/strong&gt;. And that's all that matters.&lt;/p&gt;

&lt;p&gt;Following this line of thought, &lt;strong&gt;the user doesn't care about how the website was coded&lt;/strong&gt; or &lt;strong&gt;what your technology choices were&lt;/strong&gt;. She doesn't care if you have a great readme or comment your code. She doesn't care if you apply good design patterns or if you write good integration tests for your application. She doesn't even know (or want to know) all this stuff. If she has &lt;strong&gt;a great experience using your product&lt;/strong&gt;, that's enough.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I totally agree that the user experience is what matters the most&lt;/strong&gt;. We build great products to save people's time, money or improve their lives in some other way. But I think that we cannot speak about user experience without &lt;strong&gt;taking into consideration the way we write your code and our technology choices&lt;/strong&gt;. These things have &lt;strong&gt;indirect, but huge implications in your user experiences&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Imagine this situation
&lt;/h2&gt;

&lt;p&gt;You're a front-end developer working for a certain company. As every good team does, you're consistently trying to find out what your users struggle with when using your product, so you can &lt;strong&gt;improve it and provide a better experience&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's say that using Hotjar, you find out that &lt;strong&gt;your users are struggling with some filters&lt;/strong&gt;. The way some of them are grouped is &lt;strong&gt;confusing&lt;/strong&gt;, and the users are struggling to find the ones they need.&lt;/p&gt;

&lt;p&gt;You're asked to fixed this and your PM asks you &lt;strong&gt;how much time&lt;/strong&gt; you think you'll need.&lt;/p&gt;

&lt;h2&gt;
  
  
  And here comes the problem
&lt;/h2&gt;

&lt;p&gt;If your application is &lt;strong&gt;well written&lt;/strong&gt;, &lt;strong&gt;documented&lt;/strong&gt;, uses solid &lt;strong&gt;design patterns&lt;/strong&gt;, &lt;strong&gt;latest technologies&lt;/strong&gt; that simplify your code, has good &lt;strong&gt;integration tests&lt;/strong&gt; for that filters functionality, &lt;strong&gt;deployment&lt;/strong&gt; is very simple, etc., it will most probably be an &lt;strong&gt;easy task&lt;/strong&gt;. Let's say you can fix it in &lt;strong&gt;2 days&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But, on the other hand, if your application code is a &lt;strong&gt;mess&lt;/strong&gt;, uses &lt;strong&gt;old technology&lt;/strong&gt; that only a one person of the team understands, has files with over a &lt;strong&gt;thousand lines&lt;/strong&gt;, has &lt;strong&gt;no tests&lt;/strong&gt;, the &lt;strong&gt;deployment&lt;/strong&gt; process is horrible and often &lt;strong&gt;fails&lt;/strong&gt;, etc., it will be a lot more complicated. You will struggle to make it work, and you most probably will introduce &lt;strong&gt;new bugs&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this scenario, 2 days will not be enough, and you will probably need &lt;strong&gt;a week or more&lt;/strong&gt; to finish this task.&lt;/p&gt;

&lt;h2&gt;
  
  
  So, does the user care about how your code is written?
&lt;/h2&gt;

&lt;p&gt;Indirectly, yes, of course! She's &lt;strong&gt;struggling&lt;/strong&gt; with your product's filters. She expects to find the filters she needs easily, but she doesn't. And she &lt;strong&gt;doesn't accomplish her goal&lt;/strong&gt; of finding what she needs.&lt;/p&gt;

&lt;p&gt;She's having a &lt;strong&gt;bad experience&lt;/strong&gt;, and she wants this to be fixed &lt;strong&gt;immediately&lt;/strong&gt;. Maybe she even took the time to write and email and complain about this. And what will you answer? "Sorry, but this will take over a week because our code is horrible"?&lt;/p&gt;

&lt;p&gt;If you and your team had done things correctly, this issue would be solved in two days, and would be a &lt;strong&gt;minor issue&lt;/strong&gt;. But because your code is a mess, you need &lt;strong&gt;a lot more time and resources&lt;/strong&gt; and you have the risk of &lt;strong&gt;losing a lot of users&lt;/strong&gt; who are struggling with this same thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  So does it matter if you use React or Vue?
&lt;/h2&gt;

&lt;p&gt;No! Of course not. We should &lt;strong&gt;stop discussing&lt;/strong&gt; these things that have &lt;strong&gt;little impact&lt;/strong&gt; and ultimately are a matter of preference (and a waste of time). Both React and Vue (and other JS frameworks) are great and you can accomplish what you need with any of them. Using semicolons is the exact same thing as not using them. Using tabs over spaces doesn't produce any &lt;strong&gt;real impact&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead, we should focus on discussing things that have &lt;strong&gt;real impact&lt;/strong&gt;. We should try to include &lt;strong&gt;latest technology&lt;/strong&gt; that really makes our lives as developers easier and &lt;strong&gt;simplifies our code&lt;/strong&gt;. We should use &lt;strong&gt;solid design patterns&lt;/strong&gt;. We should &lt;strong&gt;document&lt;/strong&gt; our code. We should write good &lt;strong&gt;integration tests&lt;/strong&gt; for common user cases. We should have a &lt;strong&gt;simple and predictable deployment process&lt;/strong&gt; that allows us to hit production with new features regularly.&lt;/p&gt;

&lt;p&gt;These (and a lot more) are the things that &lt;strong&gt;matter and produce real impact&lt;/strong&gt;. And ultimately, the things that indirectly matter to your users. Because &lt;strong&gt;if you do a good work, you will deliver what your users really need much faster&lt;/strong&gt;. And that means providing a great experience, which is the most important thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  My advice for you
&lt;/h2&gt;

&lt;p&gt;If I may, my advice for you is that next time that you're discussing something technical or trying to choose a new technology for your project, &lt;strong&gt;think first about your users&lt;/strong&gt;. Think about what's best for them. Think if this decision will really help you to &lt;strong&gt;give them what they need in an easier, faster way&lt;/strong&gt;. Think if it will really make your life as a developer easier the next time users needs something fixed or a new feature.&lt;/p&gt;

&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ux</category>
      <category>codequality</category>
    </item>
    <item>
      <title>Understanding React's Error Boundaries</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Mon, 08 Jun 2020 12:13:55 +0000</pubDate>
      <link>https://forem.com/francodalessio/understanding-react-s-error-boundaries-2l7c</link>
      <guid>https://forem.com/francodalessio/understanding-react-s-error-boundaries-2l7c</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published in &lt;a href="https://francodalessio.com/understanding-react-error-boundaries/" rel="noopener noreferrer"&gt;my personal site&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;In the past, &lt;strong&gt;Javascript errors inside components used to corrupt React’s internal state and produce a corrupted UI in place&lt;/strong&gt;. React did not provide any way to handle these errors gracefully, and could not recover from them.&lt;/p&gt;

&lt;p&gt;An important change was introduced in &lt;strong&gt;React version 16&lt;/strong&gt;. Now &lt;strong&gt;any uncaught Javascript error will result in unmounting the whole React component tree&lt;/strong&gt;. This leaves no room for corrupted UIs.&lt;/p&gt;

&lt;p&gt;Why was this change introduced? Well, the React team believes that &lt;strong&gt;it is a bad practice to leave a corrupted UI in place&lt;/strong&gt;, because it may have dangerous outcomes. For example, in a bank app, it is worse to display a wrong account balance than to render nothing.&lt;/p&gt;

&lt;p&gt;That sounds logical, but still &lt;strong&gt;a Javascript error in some part of the UI shouldn’t break the whole app&lt;/strong&gt;. To solve this problem, the React team introduced a new concept called &lt;strong&gt;error boundary&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an error boundary?
&lt;/h2&gt;

&lt;p&gt;Error boundaries are React components. Their role is to &lt;strong&gt;catch Javascript errors&lt;/strong&gt; anywhere in their child component tree, &lt;strong&gt;log them&lt;/strong&gt; and &lt;strong&gt;display an alternative UI&lt;/strong&gt; instead of the component tree that crashed.&lt;/p&gt;

&lt;p&gt;Error boundaries catch errors during:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rendering&lt;/li&gt;
&lt;li&gt;Lifecycle methods&lt;/li&gt;
&lt;li&gt;Constructors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But it's important to know that error boundaries &lt;strong&gt;do not&lt;/strong&gt; catch errors for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Event handlers&lt;/li&gt;
&lt;li&gt;Async code&lt;/li&gt;
&lt;li&gt;Server side rendering (SSR)&lt;/li&gt;
&lt;li&gt;Errors thrown in the error boundary itself&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Error boundaries work like a Javascript &lt;code&gt;catch {}&lt;/code&gt; block, but for components.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before we create an error boundary component...
&lt;/h2&gt;

&lt;p&gt;We need some app to test it. So let's create a very simple UI where we have two sections: &lt;code&gt;News&lt;/code&gt; and &lt;code&gt;Chat&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Both sections have a button that will &lt;strong&gt;simulate a Javascript error&lt;/strong&gt; when clicked.&lt;/p&gt;

&lt;p&gt;This is our &lt;code&gt;News&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&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;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;newsBox&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1px solid #333&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.5rem 0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;50px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;300px&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;News&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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;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="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;News error!&lt;/span&gt;&lt;span class="dl"&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="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;News&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;newsBox&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;newsBox&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Throw Error&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&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="nx"&gt;News&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Chat&lt;/code&gt; component, very similar to the previous one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&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;Chat&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;styles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;chatBox&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;border&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1px solid #333&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.5rem 0&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;150px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;300px&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&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;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="nf"&gt;setError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;News error!&lt;/span&gt;&lt;span class="dl"&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="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Chat&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;styles&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;chatBox&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Throw Error&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&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="nx"&gt;Chat&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And our &lt;code&gt;App&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&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;News&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./News&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;Chat&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Chat&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.5rem 1.5rem&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;hr&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;News&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;hr&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Chat&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;Our app looks like this:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft6fj61k5r20pd98nkyom.png" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ft6fj61k5r20pd98nkyom.png" alt="Our simple application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's see what happens when a Javascript error is thrown.&lt;/p&gt;

&lt;h2&gt;
  
  
  Without error boundaries
&lt;/h2&gt;

&lt;p&gt;If we click one of the &lt;code&gt;Throw Error&lt;/code&gt; buttons, &lt;strong&gt;we would expect the whole app to break&lt;/strong&gt;. As we previously discussed, React 16 has this behavior for any uncaught Javascript error.&lt;/p&gt;

&lt;p&gt;And effectively, it does break:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Febslya83end829juzlou.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Febslya83end829juzlou.gif" alt="Our breaking app"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's see how to catch these errors with an error boundary component.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to create an error boundary component
&lt;/h2&gt;

&lt;p&gt;Creating an error boundary component is very easy. The first thing you should know is that error boundaries have to be &lt;strong&gt;class&lt;/strong&gt; components. Right now there is no way to create an error boundary using a functional component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorBoundary&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;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="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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The next thing you should do is adding the &lt;code&gt;componentDidCatch&lt;/code&gt; method. This method receives two parameters: the &lt;code&gt;error&lt;/code&gt; and the &lt;code&gt;errorInfo&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;As we want to display a &lt;strong&gt;fallback UI&lt;/strong&gt; in case of error, we need to have some &lt;strong&gt;state&lt;/strong&gt; that indicates that. So let's add it, and update the state when an error is caught:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorBoundary&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;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;error&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="na"&gt;errorInfo&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;componentDidCatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;errorInfo&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="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;errorInfo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;errorInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// Log error info somewhere&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You could also log the &lt;code&gt;errorInfo&lt;/code&gt; somewhere.&lt;/p&gt;

&lt;p&gt;Great! The last step is to complete the render function. We want to render a fallback UI if there is an error. Otherwise, we just need to render the &lt;code&gt;children&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&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="s2"&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ErrorBoundary&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;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;error&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="na"&gt;errorInfo&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;componentDidCatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;errorInfo&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="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;errorInfo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;errorInfo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// Log error info somewhere&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="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;errorInfo&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Something went wrong!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&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="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="nx"&gt;children&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;p&gt;Perfect! We have our error boundary component, now let's see how we can use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Catching errors with an error boundary component
&lt;/h2&gt;

&lt;p&gt;Using our new error boundary component is very easy. We just need to include it in our component tree as a &lt;strong&gt;wrapper&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.5rem 1.5rem&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;hr&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;News&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;hr&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Chat&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;This way, when a Javascript error is thrown, it will be caught by our error boundary and the fallback UI will be displayed instead:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwcga6kgc01vdsudnznkv.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwcga6kgc01vdsudnznkv.gif" alt="Our error boundary component catches the error"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Using multiple error boundaries
&lt;/h2&gt;

&lt;p&gt;The previous example worked fine, we were able to catch our error and display a fallback UI.&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;our entire application was replaced by this error message&lt;/strong&gt;, even though the error was thrown by only one of the components.&lt;/p&gt;

&lt;p&gt;The good news is that you can have &lt;strong&gt;multiple error boundaries for different sections&lt;/strong&gt; of your application.&lt;/p&gt;

&lt;p&gt;For example, Facebook Messenger wraps content of the sidebar, the info panel, the conversation log, and the message input into separate error boundaries. If some component in one of these UI areas crashes, the rest of them remain interactive.&lt;/p&gt;

&lt;p&gt;In our case, we could use an error boundary for our &lt;code&gt;News&lt;/code&gt; component, and another one for our &lt;code&gt;Chat&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;style&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0.5rem 1.5rem&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;hr&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;News&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;hr&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Chat&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;ErrorBoundary&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;Now, if our &lt;code&gt;News&lt;/code&gt; section throws a Javascript error, our &lt;code&gt;Chat&lt;/code&gt; section won't be affected:&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fp5jf46mdfxg9b6ulk5t0.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fp5jf46mdfxg9b6ulk5t0.gif" alt="Multiple error boundary components catch different errors"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And this is the real &lt;em&gt;power&lt;/em&gt; of error boundaries. If you use multiple of them, you can &lt;strong&gt;isolate&lt;/strong&gt; errors in &lt;strong&gt;different, independent sections&lt;/strong&gt;, without affecting the whole application.&lt;/p&gt;

&lt;h2&gt;
  
  
  That's it!
&lt;/h2&gt;

&lt;p&gt;I hope this post was helpful and now you understand better what error boundaries are and how to use them. Thanks for reading! ❤️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Writing an integration test with React Testing Library</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Mon, 25 May 2020 12:49:07 +0000</pubDate>
      <link>https://forem.com/francodalessio/writing-an-integration-test-with-react-testing-library-1eo3</link>
      <guid>https://forem.com/francodalessio/writing-an-integration-test-with-react-testing-library-1eo3</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally published in &lt;a href="https://francodalessio.com/writing-integration-test-react-testing-library/"&gt;my personal site&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;At this point I think I don't have to explain the &lt;strong&gt;importance of testing&lt;/strong&gt; your applications. Automated tests allow you to &lt;strong&gt;catch bugs while still developing&lt;/strong&gt; and avoid getting a call at 2am because your application is not working as expected.&lt;/p&gt;

&lt;p&gt;It's all about &lt;strong&gt;confidence&lt;/strong&gt;. When shipping new code, you want that confidence. You want to &lt;em&gt;know&lt;/em&gt; that your application will work just fine. Of course you can't know, but if you have &lt;strong&gt;a bunch of great tests&lt;/strong&gt;, you will be a lot more confident.&lt;/p&gt;

&lt;p&gt;Yes, you need to &lt;strong&gt;invest time&lt;/strong&gt; writing tests. But in the long run, it will &lt;strong&gt;save you a lot of time&lt;/strong&gt; and will avoid you a lot of headaches.&lt;/p&gt;

&lt;h2&gt;
  
  
  But can you have &lt;em&gt;too many tests&lt;/em&gt;?
&lt;/h2&gt;

&lt;p&gt;Nobody argues that testing applications is a waste of time anymore. But we still have this problem of writing &lt;strong&gt;too many tests&lt;/strong&gt;. It's a big challenge to understand &lt;strong&gt;what should be tested&lt;/strong&gt;, and also &lt;strong&gt;how to test it&lt;/strong&gt; in order to get as much confidence as possible.&lt;/p&gt;

&lt;p&gt;A while back, &lt;a href="https://twitter.com/rauchg"&gt;Guillermo Rauch‏&lt;/a&gt; tweeted this:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--oVN8Phr3--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/871555682608136205/yMs8Gnot_normal.jpg" alt="Guillermo Rauch profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        Guillermo Rauch
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        @rauchg
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--52oNvK_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://practicaldev-herokuapp-com.freetls.fastly.net/assets/twitter-ff4bdab814039c4cb172a35ea369e0ea9c6a4b59b631a293896ae195fa26a99d.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      Write tests. Not too many. Mostly integration.
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      16:43 PM - 10 Dec 2016
    &lt;/div&gt;


    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=807626710350839808" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-reply-action.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=807626710350839808" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-retweet-action.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      185
      &lt;a href="https://twitter.com/intent/like?tweet_id=807626710350839808" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="/assets/twitter-like-action.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
      513
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;Many times we hear that our tests should cover 100% of our code. And that's simply not always a good idea. There is always a point that is the &lt;strong&gt;turning point&lt;/strong&gt;. Once you get that X% of coverage, &lt;strong&gt;the new tests you write are not really helping&lt;/strong&gt;. The number is different for every project, but it's never as high as 100%.&lt;/p&gt;

&lt;p&gt;When you go over that point, you start spending time &lt;strong&gt;testing things that really don't need to be tested&lt;/strong&gt;. You test things that have &lt;strong&gt;no logic at all&lt;/strong&gt;. You have one line of code that is uncovered and you write awful tests, usually involving &lt;strong&gt;implementation details&lt;/strong&gt;, just to get that 100% coverage.&lt;/p&gt;

&lt;p&gt;You may not see the issue right away, but as you start to have more and more tests like that, it becomes &lt;strong&gt;hard to mantain them&lt;/strong&gt; and ultimately &lt;strong&gt;slow the whole team down&lt;/strong&gt;. The important thing to have in mind here is that &lt;strong&gt;you should write tests to have more confidence&lt;/strong&gt; that your application is working fine. And these kind of tests doesn't help to increase that confidence at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, and why integration tests?
&lt;/h2&gt;

&lt;p&gt;The three most common types of tests are: &lt;strong&gt;unit, integration and end to end.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unit tests&lt;/strong&gt; are &lt;strong&gt;faster&lt;/strong&gt; to write and of course, &lt;strong&gt;cheaper&lt;/strong&gt;. But they also &lt;strong&gt;don't give you a lot of confidence&lt;/strong&gt;. It's fine testing if component &lt;code&gt;A&lt;/code&gt; renders correctly, but if you don't &lt;em&gt;also&lt;/em&gt; test it together with &lt;code&gt;B&lt;/code&gt; and &lt;code&gt;C&lt;/code&gt;, you won't have much confidence in your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;End to end&lt;/strong&gt; tests give you &lt;strong&gt;a lot of confidence&lt;/strong&gt;, because you are testing everything. But they also take &lt;strong&gt;a lot of time to write&lt;/strong&gt;, and are of course &lt;strong&gt;the most expensive option&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Integration&lt;/strong&gt; tests are the &lt;strong&gt;perfect balance&lt;/strong&gt;. You test &lt;strong&gt;how your components work together&lt;/strong&gt;, but mock some things (for example, HTTP requests) so it &lt;strong&gt;doesn't take you a lot of time&lt;/strong&gt; to write them. And they give you &lt;strong&gt;a lot of confidence&lt;/strong&gt; as well. Once you start testing your components this way, you'll see that you may not need a lot of your unit tests at all.&lt;/p&gt;

&lt;p&gt;This is why it's a great idea to &lt;strong&gt;spend &lt;em&gt;most&lt;/em&gt; of your time writing integration tests&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This doesn't mean that you should &lt;em&gt;only&lt;/em&gt; write this kind of tests.&lt;/p&gt;

&lt;p&gt;This also doesn't mean that unit and end to end tests are useless.&lt;/p&gt;

&lt;p&gt;It just means that it's intelligent to spend &lt;em&gt;most&lt;/em&gt; of your time writing integration tests, because they provide you with &lt;strong&gt;a great balance on the trade-offs between confidence and effort&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  A quick note on mocking
&lt;/h3&gt;

&lt;p&gt;When you write integration tests, you want to &lt;strong&gt;mock as less as possible&lt;/strong&gt;. Mocking stuff reduces the confidence in that test. So just keep that in mind.&lt;/p&gt;

&lt;h2&gt;
  
  
  Have you heard of React Testing Library?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://testing-library.com/docs/react-testing-library/intro"&gt;React Testing Library&lt;/a&gt; is by far the &lt;strong&gt;best&lt;/strong&gt; and &lt;strong&gt;most popular testing library&lt;/strong&gt; available for React right now.&lt;/p&gt;

&lt;p&gt;Its creator, &lt;a href="https://kentcdodds.com/"&gt;Kent C. Dodds&lt;/a&gt;, wrote it with this in mind:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The more your tests resemble the way your software is used, the more confidence they can give you."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;He's also a big advocate of integration tests, so let's use this library to write one!&lt;/p&gt;

&lt;h2&gt;
  
  
  First we need an application
&lt;/h2&gt;

&lt;p&gt;I wrote a small app that consists of two pages. In the home page, you can write a &lt;code&gt;postId&lt;/code&gt;. The &lt;code&gt;Submit&lt;/code&gt; button is disabled until you write something.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sJJJpEWe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dcbi3dzbh8dkdqnm6nzz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sJJJpEWe--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/dcbi3dzbh8dkdqnm6nzz.png" alt="The Home page of our application"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once you write a &lt;code&gt;postId&lt;/code&gt; and press the &lt;code&gt;Submit&lt;/code&gt; button, you navigate to a second page, &lt;code&gt;/post/:postId&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you land on this page, you will first see a &lt;code&gt;Loading...&lt;/code&gt; message:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xvYXteCE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xqj9ly99tx3o7rq5p3bm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xvYXteCE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xqj9ly99tx3o7rq5p3bm.png" alt="The loading message in the Post page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;An HTTP request is fired to the &lt;a href="https://jsonplaceholder.typicode.com/"&gt;JSON Placeholder API &lt;/a&gt; using the provided &lt;code&gt;postId&lt;/code&gt;, and once the data is received, the post is displayed. This page also has a link to go back to the Home page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hg26xs_k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/soelscxsth6nrtgqwm3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hg26xs_k--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/soelscxsth6nrtgqwm3d.png" alt="The post data is fetched from an API an displayed once it's ready"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the full code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BrowserRouter&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Switch&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="s2"&gt;react-router-dom&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;fetchPost&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="s2"&gt;./api&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App2&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="nt"&gt;exact&lt;/span&gt; &lt;span class="na"&gt;path=&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt; &lt;span class="na"&gt;component=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Route&lt;/span&gt; &lt;span class="nt"&gt;exact&lt;/span&gt; &lt;span class="na"&gt;path=&lt;/span&gt;&lt;span class="s2"&gt;"/post/:postId"&lt;/span&gt; &lt;span class="na"&gt;component=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Switch&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Router&lt;/span&gt;&lt;span class="p"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;history&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPostId&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&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;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Search for a post by its ID&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt; &lt;span class="na"&gt;htmlFor=&lt;/span&gt;&lt;span class="s2"&gt;"postId"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Post ID: &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;label&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt;
        &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s2"&gt;"postId"&lt;/span&gt;
        &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onChange=&lt;/span&gt;&lt;span class="si"&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="nx"&gt;setPostId&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="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;
        &lt;span class="na"&gt;disabled=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="na"&gt;onClick=&lt;/span&gt;&lt;span class="si"&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="nx"&gt;history&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`/post/${postId}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Submit
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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="kd"&gt;function&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;match&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;postId&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&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;setPost&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;setPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetchPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;postId&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;postId&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Post &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Loading...&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&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="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&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;body&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt; &lt;span class="na"&gt;to=&lt;/span&gt;&lt;span class="s2"&gt;"/"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Back to Home&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;Link&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;And this is the &lt;code&gt;api.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchPost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;postId&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;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`https://jsonplaceholder.typicode.com/posts/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;postId&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&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;p&gt;You can play with the application in &lt;a href="https://codesandbox.io/s/search-post-by-id-62jwh"&gt;this code sandbox&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Now we're ready for the test!
&lt;/h2&gt;

&lt;p&gt;I won't write about configurations in this post. &lt;strong&gt;I'm assuming you have Jest and React Testing Library set up&lt;/strong&gt; and you're ready to write your test.&lt;/p&gt;

&lt;p&gt;I will write each step without repeating any code, and then &lt;strong&gt;at the end I'll leave the complete test snippet&lt;/strong&gt; for you to check.&lt;/p&gt;

&lt;p&gt;Let's start with a few imports. Of course we need to import &lt;code&gt;React&lt;/code&gt; first, and we also need &lt;code&gt;render&lt;/code&gt; and &lt;code&gt;screen&lt;/code&gt; from React Testing Library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="s2"&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="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="nx"&gt;screen&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="s2"&gt;@testing-library/react&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;&lt;code&gt;render&lt;/code&gt; allows us to render our component in order to start testing it, and &lt;code&gt;screen&lt;/code&gt; provides us with all the &lt;strong&gt;queries&lt;/strong&gt; we need to get elements.&lt;/p&gt;

&lt;p&gt;We now need to create our test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Can search for a post using its ID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We import our &lt;code&gt;App&lt;/code&gt; component and call the &lt;code&gt;render&lt;/code&gt; function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Can search for a post using its ID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&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;render&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;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;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;p&gt;Great! Our test should be passing. Now we can start using &lt;code&gt;screen&lt;/code&gt; to check if our component is displaying what it should.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Home page
&lt;/h3&gt;

&lt;p&gt;We're in the Home page, so we should see a &lt;em&gt;Welcome&lt;/em&gt; message. As this is is text, we can use &lt;code&gt;getByText&lt;/code&gt; query. We get that element and then wrap it in an &lt;code&gt;expect&lt;/code&gt; statement, &lt;em&gt;expecting&lt;/em&gt; that it exists in the document.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/welcome/i&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It's always a good idea to use &lt;strong&gt;regular expressions&lt;/strong&gt; for your text content, and &lt;strong&gt;ignore cases&lt;/strong&gt;. First, your users doesn't care about text cases. And second, if it's a phrase, you may slightly &lt;strong&gt;change&lt;/strong&gt; your words in the future, but you'll always keep a few &lt;strong&gt;important&lt;/strong&gt; ones. Your test should be looking for them, as they will always be there.&lt;/p&gt;

&lt;p&gt;Let's see an example of that. In our home page we have an &lt;code&gt;h2&lt;/code&gt; element that says &lt;code&gt;Search for a post by its ID&lt;/code&gt;. We could totally do this, and it would work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Search for a post by its ID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But what if the next week we change that phrase to &lt;code&gt;Here you can search for a post. The only thing you need is its ID&lt;/code&gt;? Of course, now our test &lt;strong&gt;would be broken&lt;/strong&gt;! A better way to write this statement would be this one:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/search.*post.*id/i&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's perfect! We know that &lt;strong&gt;we have three important words that will always be there&lt;/strong&gt; (&lt;code&gt;search&lt;/code&gt;, &lt;code&gt;post&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt;). With this statement, the test wouldn't break if we changed our phrase as we said before.&lt;/p&gt;

&lt;p&gt;The last thing we need to check in our Home page is the &lt;code&gt;Submit&lt;/code&gt; button, which should be there but be &lt;strong&gt;disabled&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/submit/i&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeDisabled&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Searching for a post
&lt;/h3&gt;

&lt;p&gt;Our Home page is rendering correctly and has all the user needs to search for a post. We need to &lt;strong&gt;simulate the user typing&lt;/strong&gt; in our &lt;code&gt;input&lt;/code&gt; field, and React Testing Library has our backs.&lt;/p&gt;

&lt;p&gt;We need to import the &lt;code&gt;user-event&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;user&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@testing-library/user-event&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;But before we can simulate the user typing in the &lt;code&gt;input&lt;/code&gt; field, we need to &lt;strong&gt;get&lt;/strong&gt; that element. A good practice when testing forms is to &lt;strong&gt;get elements by their &lt;code&gt;label&lt;/code&gt;&lt;/strong&gt;. This way we can also check if the &lt;code&gt;label&lt;/code&gt; and the &lt;code&gt;input&lt;/code&gt; field are associated correctly, which is important for &lt;strong&gt;accessibility&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So let's use the &lt;code&gt;getByLabelText&lt;/code&gt; query to get that input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByLabelText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/post id/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And now we are ready to simulate the user typing in it, using the &lt;code&gt;user-event&lt;/code&gt; module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByLabelText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/post id/i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&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;Great! To finish the user interaction, we need to &lt;strong&gt;click&lt;/strong&gt; the &lt;code&gt;Submit&lt;/code&gt; button, which we expect to be &lt;strong&gt;enabled&lt;/strong&gt; now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;submitButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/submit/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;submitButton&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toBeEnabled&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;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;submitButton&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Landing on the Post page
&lt;/h3&gt;

&lt;p&gt;Now that we clicked the submit button, we should &lt;strong&gt;navigate&lt;/strong&gt; to the &lt;code&gt;Post&lt;/code&gt; page. The first thing we should see there is a &lt;code&gt;Loading...&lt;/code&gt; message, so let's get that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/loading/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But if you write that, you'll see that &lt;strong&gt;the test won't pass&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pLluK10h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9dn3bbhi16avnx3kx8fi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pLluK10h--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9dn3bbhi16avnx3kx8fi.png" alt="The test fails when we try to use getByText query"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When this happens, we need to &lt;strong&gt;use &lt;code&gt;find*&lt;/code&gt; queries, along with &lt;code&gt;await&lt;/code&gt;&lt;/strong&gt;. The test will then &lt;em&gt;wait&lt;/em&gt; until the loading message shows up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/loading/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Perfect! Now the test is passing.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mocking HTTP requests
&lt;/h3&gt;

&lt;p&gt;The next thing that happens in our application is that &lt;strong&gt;an HTTP request is fired to the API&lt;/strong&gt; to get the post details. We don't want to fire an actual request in our test, so we need to &lt;strong&gt;mock&lt;/strong&gt; it.&lt;/p&gt;

&lt;p&gt;First of all, right after our imports, let's mock the &lt;code&gt;api&lt;/code&gt; module using &lt;code&gt;jest&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../api&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;And now we can import the module as a &lt;strong&gt;mocked&lt;/strong&gt; version of it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;fetchPost&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;mockFetchPost&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="s2"&gt;../api&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;In our test, let's create a &lt;strong&gt;mock post&lt;/strong&gt;, the object that our fake request will return when resolved:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;mockPost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Post Title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Post Body&lt;/span&gt;&lt;span class="dl"&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;p&gt;And then instruct our mock function to &lt;strong&gt;return that object&lt;/strong&gt; when called:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;mockFetchPost&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mockResolvedValueOnce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can also make a small enhancement to use this mock post object when we simulate the user typing in the input field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByLabelText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/post id/i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;mockPost&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Perfect! Everything is set up and we can now continue with our test.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping up our test
&lt;/h3&gt;

&lt;p&gt;First of all we should check that our mock function is called, and that &lt;strong&gt;it's called just once&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockFetchPost&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toHaveBeenCalledTimes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And also that it is called with the &lt;strong&gt;mock post id&lt;/strong&gt; we defined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockFetchPost&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's check that the actual &lt;strong&gt;post information is displayed&lt;/strong&gt; on the screen for the user:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&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="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The only thing left to test is the &lt;strong&gt;link back home&lt;/strong&gt;. First we click it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/back.*home/i&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then we should be in the Home page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/welcome/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We're done! This is the complete test:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="s2"&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="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="nx"&gt;screen&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="s2"&gt;@testing-library/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;user&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@testing-library/user-event&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;fetchPost&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;mockFetchPost&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="s2"&gt;../api&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;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../app&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;jest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;../api&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;test&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Can search for a post using its ID&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&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;const&lt;/span&gt; &lt;span class="nx"&gt;mockPost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Post Title&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Post Body&lt;/span&gt;&lt;span class="dl"&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;mockFetchPost&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;mockResolvedValueOnce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&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;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/submit/i&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeDisabled&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/welcome/i&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/search.*post.*id/i&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&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;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByLabelText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/post id/i&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nx"&gt;mockPost&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;submitButton&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/submit/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;submitButton&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toBeEnabled&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;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;submitButton&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/loading/i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockFetchPost&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toHaveBeenCalledWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&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="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockFetchPost&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toHaveBeenCalledTimes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&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="nx"&gt;toBeInTheDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mockPost&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nx"&gt;toBeInTheDocument&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;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/back.*home/i&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;screen&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/welcome/i&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;
  
  
  That's it!
&lt;/h2&gt;

&lt;p&gt;We tested the &lt;strong&gt;&lt;em&gt;happy path&lt;/em&gt;&lt;/strong&gt; of &lt;strong&gt;searching&lt;/strong&gt; for a post, &lt;strong&gt;navigating&lt;/strong&gt; to a different page, &lt;strong&gt;waiting&lt;/strong&gt; for it to load, &lt;strong&gt;seeing&lt;/strong&gt; the post information on the screen and &lt;strong&gt;navigating&lt;/strong&gt; back to the home page. It was easy, wasn't it?&lt;/p&gt;

&lt;p&gt;Of course there are &lt;strong&gt;more things to test&lt;/strong&gt;, even in this small application. I hope this was useful and that you &lt;strong&gt;start writing a lot of long, integration tests&lt;/strong&gt; for your applications.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>testing</category>
      <category>javascript</category>
    </item>
    <item>
      <title>The 3 essentials to get started with styled-components</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Sun, 10 May 2020 19:18:20 +0000</pubDate>
      <link>https://forem.com/francodalessio/the-3-essentials-to-get-started-with-styled-components-258</link>
      <guid>https://forem.com/francodalessio/the-3-essentials-to-get-started-with-styled-components-258</guid>
      <description>&lt;p&gt;&lt;em&gt;This post was originally posted in &lt;a href="https://francodalessio.com/the-3-essentials-to-get-started-with-styled-components/"&gt;my personal site&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I love &lt;code&gt;styled-components&lt;/code&gt;. I use this library for all my personal projects (including my &lt;a href="https://francodalessio.com"&gt;personal site&lt;/a&gt;). It's very &lt;strong&gt;powerful&lt;/strong&gt; and also very &lt;strong&gt;easy to use.&lt;/strong&gt; In this article I'm going to explain the &lt;strong&gt;3 main things you can do with &lt;code&gt;styled-components&lt;/code&gt;&lt;/strong&gt; so you can start using it right away.&lt;/p&gt;

&lt;h2&gt;
  
  
  But first... What exactly is styled-components?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;"styled-components is the result of wondering how we could enhance CSS for styling React component systems"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;styled-components&lt;/code&gt; is a library that allows you to write actual &lt;strong&gt;CSS code to style your components&lt;/strong&gt;, removing the mapping between components and styles. It uses tagged &lt;strong&gt;template literals&lt;/strong&gt; and &lt;strong&gt;CSS&lt;/strong&gt;. That's it.&lt;/p&gt;

&lt;p&gt;And this library does everything else for you:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It keeps &lt;strong&gt;track&lt;/strong&gt; of which components are rendered and &lt;strong&gt;injects&lt;/strong&gt; their styles.&lt;/li&gt;
&lt;li&gt;It uses &lt;strong&gt;code splitting&lt;/strong&gt; so your users load the least amount of code necessary.&lt;/li&gt;
&lt;li&gt;It generates &lt;strong&gt;unique class names&lt;/strong&gt; for your styles, so you don't have to worry about duplications or overlaps.&lt;/li&gt;
&lt;li&gt;It makes obvious which styles you are &lt;strong&gt;not using&lt;/strong&gt; (because they are tied to a specific component), so you can easily &lt;strong&gt;delete&lt;/strong&gt; them. This is usually hard when working with CSS.&lt;/li&gt;
&lt;li&gt;It automatically &lt;strong&gt;prefixes&lt;/strong&gt; your CSS for it to work with different vendors. You can just write your CSS to the &lt;strong&gt;current standard&lt;/strong&gt; and &lt;code&gt;styled-components&lt;/code&gt; will handle the rest.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Everything sounds great, but you may be thinking that writing CSS is not enough for you, that you need &lt;strong&gt;dynamic styles&lt;/strong&gt;. Well, don't worry, &lt;code&gt;styled-components&lt;/code&gt; allows you to &lt;strong&gt;style components based on props or global themes&lt;/strong&gt; very easily.&lt;/p&gt;

&lt;p&gt;Are you ready to start with it? I promise it'll be very easy! Let's see the &lt;strong&gt;3 essential things you can do with this library.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can find all the code we'll explain &lt;a href="https://codesandbox.io/s/get-started-styled-components-548v9"&gt;in this CodeSandbox&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  1- Create and use your first styled component
&lt;/h2&gt;

&lt;p&gt;First, you need to &lt;strong&gt;install&lt;/strong&gt; the library:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i --save styled-components
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now let's say we want to create a Home page where we display a title in the center of the screen. Something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RXk7lmbK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zezp6ruvmwpcq5nbanxv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RXk7lmbK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zezp6ruvmwpcq5nbanxv.png" alt="A simple page with a centered title to demonstrate the use of styled components"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Very simple. We need a &lt;code&gt;div&lt;/code&gt; and an &lt;code&gt;h1&lt;/code&gt; tag. The &lt;code&gt;div&lt;/code&gt; would have this CSS code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;display&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;flex&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;flex-direction&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;column&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;justify-content&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;center&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;align-items&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;center&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;height&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;100vh&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And let's say we want the &lt;code&gt;h1&lt;/code&gt; to be underlined:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="nt"&gt;text-decoration&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nt"&gt;underline&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's do it with &lt;code&gt;styled-components&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;In order to keep the code well structured, let's create a &lt;code&gt;components/Home&lt;/code&gt; folder, and two files inside of it: &lt;code&gt;index.js&lt;/code&gt; and &lt;code&gt;styles.js&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;styles&lt;/code&gt; file is where we will create our styled components.&lt;/p&gt;

&lt;p&gt;Let's start by &lt;strong&gt;importing the library&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;styled&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;styled-components&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, to create a styled component, we need to &lt;strong&gt;write &lt;code&gt;style.&lt;/code&gt; and then the HTML tag&lt;/strong&gt; we want to style. In this case, we first need a &lt;code&gt;div&lt;/code&gt;, so let's start with that. Also, let's save that component in a variable and &lt;strong&gt;export&lt;/strong&gt; it, so we can later import it from our &lt;code&gt;index&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;``&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The only thing left is to write our styles! And since we can write &lt;strong&gt;regular CSS within the backticks&lt;/strong&gt;, it's very easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Congratulations! You've now written your first styled component! 🎉&lt;/p&gt;

&lt;p&gt;Let's go ahead and also add our &lt;code&gt;Title&lt;/code&gt; component, which will be a styled &lt;code&gt;h1&lt;/code&gt; tag. The whole &lt;code&gt;styles&lt;/code&gt; file will end up looking like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;styled&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;styled-components&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="s2"&gt;`
  text-decoration: underline;
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great! Now we need to &lt;strong&gt;use&lt;/strong&gt; these components. In order to do that, we need to &lt;strong&gt;import&lt;/strong&gt; them from our &lt;code&gt;index&lt;/code&gt; files.&lt;/p&gt;

&lt;p&gt;We could do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="s2"&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Home&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="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Home&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But note that I now have &lt;strong&gt;two components named &lt;code&gt;Home&lt;/code&gt;&lt;/strong&gt;, so this will just not work. One option is to assign different names to your styled components. Instead of calling it &lt;code&gt;Home&lt;/code&gt;, you can call it &lt;code&gt;Wrapper&lt;/code&gt;, or &lt;code&gt;Container&lt;/code&gt;, or something like that.&lt;/p&gt;

&lt;p&gt;A lot of people take this approach, and it works. But personally I don't like it. I want to be able to name my styled component the same as the parent component. Fortunately, a lot of people think the same, so someone came up with this other approach, which is the one I like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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="s2"&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="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&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;&lt;strong&gt;We can import all of our styled components as a variable called &lt;code&gt;S&lt;/code&gt;&lt;/strong&gt;, and then we are able to render them using that variable. Remember that styled components are just like &lt;strong&gt;regular components&lt;/strong&gt;, so you render them using &lt;a href="https://francodalessio.com/understanding-react-elements-and-jsx/"&gt;JSX&lt;/a&gt; just as usual:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Home&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is the Home page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;&lt;strong&gt;We access our styled components writing &lt;code&gt;S.&lt;/code&gt; and then the name of the component&lt;/strong&gt;. I love this approach because now I can call my styled component &lt;code&gt;Home&lt;/code&gt;, and also &lt;strong&gt;with a quick look I can tell which of the returned components are in fact styled components&lt;/strong&gt; (all of them in this case, but that may not be the case for longer, more complex components).&lt;/p&gt;

&lt;h3&gt;
  
  
  Nesting styles
&lt;/h3&gt;

&lt;p&gt;Before going to step 2, a quick note on nesting. &lt;strong&gt;You can nest styles just like you do with SASS&lt;/strong&gt;. In this case, we could get rid of the &lt;code&gt;Title&lt;/code&gt; component, for example. We can just add styles for all &lt;code&gt;h1&lt;/code&gt; existing within our &lt;code&gt;Home&lt;/code&gt; component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;styled&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;styled-components&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="s2"&gt;`
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;

  h1 {
    text-decoration: underline;
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And if we change our parent component as well...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Home&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is the Home page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;This will work just fine. It doesn't always make sense to nest styles, but keep in mind that you can do it and you may not need to create a new component.&lt;/p&gt;

&lt;h2&gt;
  
  
  2- Use props for dynamic styling
&lt;/h2&gt;

&lt;p&gt;Remember that &lt;strong&gt;styled components are just components, so they can receive props&lt;/strong&gt;. And that props can be useful to &lt;strong&gt;dynamically change the styles&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Our example is very simple, but let's say we want to &lt;strong&gt;set the color of our title text based on a prop named &lt;code&gt;titleColor&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, let's pass that prop when we render our &lt;code&gt;Title&lt;/code&gt; styled component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Home&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt; &lt;span class="na"&gt;titleColor=&lt;/span&gt;&lt;span class="s2"&gt;"blue"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is the Home page&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;And now let's use that prop to change the text color. We need to use &lt;code&gt;${}&lt;/code&gt; to get access to the props. It's very simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="s2"&gt;`
  text-decoration: underline;
  color: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;titleColor&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That will make the text blue:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4gsMJ6yx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i0e3ol41x2gga9rkwgzj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4gsMJ6yx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/i0e3ol41x2gga9rkwgzj.png" alt="The text is now blue, and we styled it using props"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also use props for &lt;strong&gt;conditional styling&lt;/strong&gt;. For example, let's say we pass a &lt;code&gt;small&lt;/code&gt; prop:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Home&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt; &lt;span class="na"&gt;titleColor=&lt;/span&gt;&lt;span class="s2"&gt;"blue"&lt;/span&gt; &lt;span class="nt"&gt;small&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        This is the Home page
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;S&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;If it's there, we want to make the text smaller. But if it's not, we want the text size to be the regular size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;styled&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="s2"&gt;`
  text-decoration: underline;
  color: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;titleColor&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
  font-size: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;small&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1.25rem&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;;
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Awesome! You know now how to dynamically style components based on props.&lt;/p&gt;

&lt;h2&gt;
  
  
  3- Create global styles
&lt;/h2&gt;

&lt;p&gt;Creating styled components is great, but sometimes we need &lt;strong&gt;global styles&lt;/strong&gt;. You may want to apply styles using tags as selectors. For example, let's say we want to change the &lt;strong&gt;background color of our &lt;code&gt;body&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Fortunately &lt;code&gt;styled-components&lt;/code&gt; makes this very simple. Start by importing &lt;code&gt;createGlobalStyle&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createGlobalStyle&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="s2"&gt;styled-components&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;Write your global styles:&lt;br&gt;
&lt;/p&gt;

&lt;div class="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;GlobalStyle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createGlobalStyle&lt;/span&gt;&lt;span class="s2"&gt;`
  body {
    background: lightblue;
  }
`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then place this component at the &lt;strong&gt;top level&lt;/strong&gt; of your application, in my case my &lt;code&gt;index.js&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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;ReactDOM&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react-dom&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createGlobalStyle&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="s2"&gt;styled-components&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;App&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./App&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;GlobalStyles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createGlobalStyle&lt;/span&gt;&lt;span class="s2"&gt;`
  body {
    background: lightblue;
  }
`&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;rootElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&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;&amp;lt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;GlobalStyles&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;App&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;&amp;lt;/&amp;gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;rootElement&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that's it! That will change the background color:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--x4L9g7fG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zl7pfq0jsc3j99dwjzgi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--x4L9g7fG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/zl7pfq0jsc3j99dwjzgi.png" alt="Background color changed using global styles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want, &lt;strong&gt;you can also use props to dynamically change global styles&lt;/strong&gt; (for example, if you have a light and a dark theme in your application).&lt;/p&gt;

&lt;h2&gt;
  
  
  That's it!
&lt;/h2&gt;

&lt;p&gt;I hope this post was useful. You now know the &lt;strong&gt;essentials to get started with &lt;code&gt;styled-components&lt;/code&gt;&lt;/strong&gt;. Of course, there's more to it, so be sure to check the &lt;a href="https://www.styled-components.com/docs/"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Never call a React function component</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Fri, 01 May 2020 18:55:19 +0000</pubDate>
      <link>https://forem.com/francodalessio/never-call-a-react-function-component-1lmg</link>
      <guid>https://forem.com/francodalessio/never-call-a-react-function-component-1lmg</guid>
      <description>&lt;p&gt;When we write React code, we use JSX to create React elements. In case you don’t know, JSX is an &lt;strong&gt;extension to Javascript&lt;/strong&gt; to support syntax that &lt;strong&gt;&lt;em&gt;looks like&lt;/em&gt; the HTML code&lt;/strong&gt; you would write to create DOM elements.&lt;/p&gt;

&lt;p&gt;JSX allows us to create a &lt;strong&gt;React element&lt;/strong&gt; by just writing this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello World&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As the browser does not understand JSX natively, &lt;a href="https://babeljs.io/"&gt;&lt;strong&gt;Babel&lt;/strong&gt;&lt;/a&gt;  then  converts the JSX syntax  using &lt;code&gt;react.createElement&lt;/code&gt; API.&lt;/p&gt;

&lt;p&gt;If you want to know more about JSX and React elements, you can check &lt;a href="https://levelup.gitconnected.com/understanding-react-elements-and-jsx-c3b7af356db4"&gt;this post&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Then we have Components
&lt;/h2&gt;

&lt;p&gt;React allows us to &lt;strong&gt;group&lt;/strong&gt; React elements and create &lt;strong&gt;components&lt;/strong&gt;. They let you split the UI into &lt;strong&gt;independent&lt;/strong&gt;, &lt;strong&gt;reusable&lt;/strong&gt; &lt;strong&gt;pieces&lt;/strong&gt;, and think about each piece in isolation.&lt;/p&gt;

&lt;p&gt;Conceptually, components are like JavaScript functions. They &lt;strong&gt;return React elements describing what should appear on the screen.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As you may already know, a component can be written as a &lt;strong&gt;class&lt;/strong&gt;…&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&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;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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello World&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;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;p&gt;…or as a &lt;strong&gt;function&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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="s2"&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;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&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="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello World&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But when it comes to &lt;strong&gt;rendering&lt;/strong&gt; a component, you always write the same thing, both for classes and function components:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="p"&gt;...&lt;/span&gt;  
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;You render your components using JSX syntax.&lt;/strong&gt; And this works just fine.&lt;/p&gt;

&lt;p&gt;But if you have a function component, can’t you just &lt;strong&gt;&lt;em&gt;call&lt;/em&gt; the function&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Well, you can, but weird things will happen. Let’s take a look.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s start with a basic app
&lt;/h2&gt;

&lt;p&gt;Our example application will consist of two components: &lt;code&gt;App&lt;/code&gt; and &lt;code&gt;Item&lt;/code&gt;. The &lt;code&gt;App&lt;/code&gt; component will render a &lt;strong&gt;list&lt;/strong&gt; of items, and each item is an &lt;strong&gt;input&lt;/strong&gt; field. There is also a &lt;strong&gt;button&lt;/strong&gt; which allows us to &lt;strong&gt;add&lt;/strong&gt; a new item to the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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;useState&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="s2"&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;Item&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./Item&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;  
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setList&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&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;addItem&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="nx"&gt;setList&lt;/span&gt;&lt;span class="p"&gt;([...&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"App"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;  
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;addItem&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Add Item&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;  
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;This is the list:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;  
      &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;  
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;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;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Please note that inside the&lt;/strong&gt; &lt;code&gt;map&lt;/code&gt; &lt;strong&gt;we use to render the list items, we are calling the Item component as a function:&lt;/strong&gt; &lt;code&gt;Item()&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our &lt;code&gt;Item&lt;/code&gt; component is just an input field:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&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;useState&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="s2"&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;./styles.css&lt;/span&gt;&lt;span class="dl"&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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Item&lt;/span&gt;&lt;span class="p"&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;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&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;handleChange&lt;/span&gt; &lt;span class="o"&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="nx"&gt;setText&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="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;onChange&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;;&lt;/span&gt;  
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Please also note that both of our components are using the&lt;/strong&gt; &lt;code&gt;useState&lt;/code&gt; &lt;strong&gt;hook.&lt;/strong&gt; This is important.&lt;/p&gt;

&lt;p&gt;Here is the finished app so you can play with it:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/call-function-component-0xrqs"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;You may have noticed there is a warning because we are not passing the &lt;code&gt;key&lt;/code&gt; prop for our Item components.&lt;a href="https://francodalessio.com/understanding-the-importance-of-the-key-prop-in-react"&gt;The key prop is important&lt;/a&gt;, but &lt;strong&gt;not&lt;/strong&gt; the issue I want to talk about here.&lt;/p&gt;

&lt;p&gt;Go ahead and click the &lt;code&gt;Add Item&lt;/code&gt; button. You should get this error:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RbWwBYaF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/548/1%2AeSuU1eXBY2JOaswUWNh6ZA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RbWwBYaF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/548/1%2AeSuU1eXBY2JOaswUWNh6ZA.png" alt="React complains because all renders must have the same number of Hooks calls"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Ok, so we know that calling a function component produces some weird error related to Hooks, but what is it exactly? 🤔&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding the problem
&lt;/h2&gt;

&lt;p&gt;If you check the console, you will find more information:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Warning: React has detected a change in the order of Hooks called by App. This will lead to bugs and errors if not fixed. For more information, read the Rules of Hooks: [https://fb.me/rules-of-hooks](https://fb.me/rules-of-hooks)  

   Previous render            Next render  
   ------------------------------------------------------  
1\. useState                   useState  
2\. useState                   useState  
3\. undefined                  useState  
   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  

    in App (at src/index.js:9)  
    in StrictMode (at src/index.js:8)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are multiple &lt;strong&gt;rules for Hooks&lt;/strong&gt;, and an important one is this one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You need to make sure that the Hooks are always called the same number of times for a given component.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do you understand the problem now?&lt;/p&gt;

&lt;p&gt;The way we’re calling &lt;code&gt;Item&lt;/code&gt;, is &lt;strong&gt;not a component&lt;/strong&gt; at all, but a function. &lt;strong&gt;React cannot associate any&lt;/strong&gt; &lt;code&gt;useState&lt;/code&gt; &lt;strong&gt;calls&lt;/strong&gt; to the &lt;code&gt;Item&lt;/code&gt; function, just because it’s not rendered as a component.&lt;/p&gt;

&lt;p&gt;So the &lt;code&gt;useState&lt;/code&gt; call that should be associated to &lt;code&gt;Item&lt;/code&gt;, is actually &lt;strong&gt;associated with&lt;/strong&gt; &lt;code&gt;App&lt;/code&gt;. Now the error message &lt;em&gt;“Rendered more hooks than the previous render”&lt;/em&gt; finally makes sense.&lt;/p&gt;

&lt;p&gt;This is the reason we &lt;strong&gt;use JSX to render our components&lt;/strong&gt;, even if they are function components. That way, React can &lt;strong&gt;register&lt;/strong&gt; any Hooks that are used in a component, with the &lt;strong&gt;instance&lt;/strong&gt; of that component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Ok, got it. But why the app works just fine if you don’t press the button?
&lt;/h2&gt;

&lt;p&gt;You’ve probably noticed that when the app starts, it &lt;strong&gt;works just fine&lt;/strong&gt;. You can even write in the input field and you won’t see any error.&lt;/p&gt;

&lt;p&gt;This is because the &lt;code&gt;App&lt;/code&gt; component is &lt;strong&gt;not being re-rendered&lt;/strong&gt;. Once we click the button to add a new item, &lt;code&gt;App&lt;/code&gt; is re-rendered and then React notices that &lt;strong&gt;the number of Hooks calls doesn’t match with the one of the previous render.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So &lt;strong&gt;there will be cases in which you won’t see an error&lt;/strong&gt;, but doing this is still wrong. In our example, at the beginning it works, but &lt;strong&gt;the&lt;/strong&gt; &lt;code&gt;useState&lt;/code&gt; &lt;strong&gt;call of&lt;/strong&gt; &lt;code&gt;Item&lt;/code&gt; &lt;strong&gt;is being associated to&lt;/strong&gt; &lt;code&gt;App&lt;/code&gt;. This is wrong, and it can make your app behave in unexpected ways as you make changes.&lt;/p&gt;

&lt;h2&gt;
  
  
  That’s it!
&lt;/h2&gt;

&lt;p&gt;I hope this was useful! Thanks for reading ❤️&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The difference between Classes and Function components in React</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Wed, 22 Apr 2020 01:21:20 +0000</pubDate>
      <link>https://forem.com/francodalessio/the-difference-between-classes-and-function-components-in-react-5964</link>
      <guid>https://forem.com/francodalessio/the-difference-between-classes-and-function-components-in-react-5964</guid>
      <description>&lt;p&gt;In the &lt;strong&gt;early days&lt;/strong&gt; of React, &lt;strong&gt;Classes&lt;/strong&gt; were the only way to have &lt;strong&gt;functionality&lt;/strong&gt; in your components (like state). You’d only use &lt;strong&gt;Functions for dumb components&lt;/strong&gt; which only displayed stuff.&lt;/p&gt;
&lt;p&gt;This is no longer true, because we now have &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;&lt;strong&gt;React Hooks&lt;/strong&gt;&lt;/a&gt; and they allow us to have in Function components the same functionality we used to have with Classes.&lt;/p&gt;
&lt;p&gt;However, there is &lt;strong&gt;one key difference&lt;/strong&gt;, that not everyone is aware of 🔮&lt;/p&gt;
&lt;h3&gt;Let’s start with a simple example&lt;/h3&gt;
&lt;p&gt;The best way to understand this concept is by &lt;strong&gt;seeing it in action.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Let’s create a simple app where you can &lt;strong&gt;select&lt;/strong&gt; a Simpsons’ character from a list. Then when you press a &lt;strong&gt;button&lt;/strong&gt;, an &lt;strong&gt;alert&lt;/strong&gt; will show a greeting to that character &lt;strong&gt;after 4 seconds&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;We will create the SayHi&lt;strong&gt; &lt;/strong&gt;component&lt;strong&gt; &lt;/strong&gt;as a Class and also as a Function, to see the &lt;strong&gt;difference&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;Our two SayHi components&lt;/h3&gt;
&lt;p&gt;First, let’s write our Function component:&lt;/p&gt;



&lt;div class="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="s2"&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;SayHiFunction&lt;/span&gt; &lt;span class="o"&gt;=&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;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;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;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;showAlert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&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;showAlert&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;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &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="s2"&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="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;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;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;Say&lt;/span&gt; &lt;span class="nx"&gt;Hi&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nb"&gt;Function&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="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="nx"&gt;SayHiFunction&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can see, it’s a very simple component. It receives &lt;strong&gt;name as a prop&lt;/strong&gt;, and then when you &lt;strong&gt;click&lt;/strong&gt; the button, a &lt;strong&gt;timeout&lt;/strong&gt; is set to show the &lt;strong&gt;alert after 4 seconds.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now let’s transform this Function into a Class:&lt;/p&gt;



&lt;div class="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="s2"&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;class&lt;/span&gt; &lt;span class="nx"&gt;SayHiClass&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;setTimeout&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;showAlert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;showAlert&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;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &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="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;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;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;Say&lt;/span&gt; &lt;span class="nx"&gt;Hi&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;Class&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="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;SayHiClass&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Pretty easy, right? I’m sure almost anyone with some React knowledge would be able to do this transformation.&lt;/p&gt;
&lt;p&gt;You can play with the app here:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/class-vs-function-vppoj"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;this two components will not behave the same 😕&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;So what’s the difference?! 😠&lt;/h3&gt;
&lt;p&gt;Ok, the important part. Let’s do this:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Select “&lt;em&gt;Marge&lt;/em&gt;” from the list&lt;/li&gt;
&lt;li&gt;Press the button&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Change&lt;/strong&gt; the character to “&lt;em&gt;Homer&lt;/em&gt;” &lt;strong&gt;before&lt;/strong&gt; &lt;strong&gt;4 seconds&lt;/strong&gt; pass&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;What do you expect to happen? &lt;strong&gt;Do you expect the alert to say “&lt;em&gt;Hello Marge” &lt;/em&gt;or “&lt;em&gt;Hello Homer”&lt;/em&gt;?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Again, right now the important thing is not what you &lt;em&gt;think&lt;/em&gt; will happen, but what would you &lt;strong&gt;&lt;em&gt;expect&lt;/em&gt;&lt;/strong&gt; to happen in this case.&lt;/p&gt;
&lt;p&gt;Of course, &lt;strong&gt;we would expect to see “&lt;em&gt;Hello Marge”&lt;/em&gt;&lt;/strong&gt;. That was the &lt;strong&gt;selected&lt;/strong&gt; &lt;strong&gt;character&lt;/strong&gt; &lt;strong&gt;when we pressed the button.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now that we’re on the same page on that, what do you think &lt;strong&gt;&lt;em&gt;will&lt;/em&gt;&lt;/strong&gt; happen?&lt;/p&gt;
&lt;p&gt;Let’s take a look at the Function component first:&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--DmDc14C4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A1yqvTEO3OFCDnk1Ts52eQQ.gif"&gt;&lt;p&gt;Good! &lt;strong&gt;It behaves as we expected&lt;/strong&gt; and the alert says “&lt;em&gt;Hello Marge”&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;Let’s try the Class component now:&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--26XVHNPm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AWdd4sToiQhvpoB7bbyHJuA.gif"&gt;&lt;p&gt;Mmm, that was &lt;strong&gt;not what we expected&lt;/strong&gt;. We changed the selected character after pressing the button, and the alert message also changed 🤔&lt;/p&gt;
&lt;h3&gt;What the f*%! happened?&lt;/h3&gt;
&lt;p&gt;Well, &lt;strong&gt;our two components are not&lt;/strong&gt; &lt;strong&gt;the same. &lt;/strong&gt;Of course, one is a Class and the other is a Function, but &lt;strong&gt;we also made a mistake when we transformed&lt;/strong&gt; the Function into a Class.&lt;/p&gt;
&lt;p&gt;The example seemed obvious and straightforward, and that’s the transformation most people would do. &lt;strong&gt;It’s common to think those two pieces of code are equivalent.&lt;/strong&gt; However, it’s wrong because of the &lt;strong&gt;main difference between Classes and Function components&lt;/strong&gt;:&lt;/p&gt;
&lt;blockquote&gt;&lt;strong&gt;Function components capture the rendered values.&lt;/strong&gt;&lt;/blockquote&gt;
&lt;h3&gt;What does that mean?&lt;/h3&gt;
&lt;p&gt;Let’s analyze our example to understand this.&lt;/p&gt;
&lt;p&gt;When we used the Class component, &lt;strong&gt;the alert message&lt;/strong&gt; &lt;strong&gt;changed&lt;/strong&gt; after we changed the selected character.&lt;/p&gt;
&lt;p&gt;That happens because &lt;strong&gt;our method is reading from &lt;/strong&gt;&lt;strong&gt;this.props.name&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;But props are immutable in React, that’s not the issue.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;It’s true, props are immutable, but… &lt;strong&gt;this is mutable&lt;/strong&gt;. And it makes sense, because &lt;strong&gt;React mutates &lt;/strong&gt;&lt;strong&gt;this over time so you can read the fresh version&lt;/strong&gt; in render and lifecycle methods.&lt;/p&gt;
&lt;p&gt;So in our example, by changing the selected character the second time, we &lt;strong&gt;re-render&lt;/strong&gt; the component, so &lt;strong&gt;this.props is updated&lt;/strong&gt;. The method is &lt;strong&gt;reading from the latest, newest props.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;That’s a problem because we weren’t expecting that. &lt;strong&gt;We would expect our event handlers to be “linked” to a particular render, with particular props.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By setting a timeout whose callback reads this.props, we’re making that impossible. &lt;strong&gt;Our callback will not be “linked” to a particular render&lt;/strong&gt;, so it doesn’t know what the “correct” props are.&lt;/p&gt;
&lt;p&gt;We don’t have this issue with the Function component because, once again:&lt;/p&gt;
&lt;blockquote&gt;Function components capture the rendered values.&lt;/blockquote&gt;
&lt;p&gt;That means that even if we change the character a second time, the method &lt;strong&gt;will display the alert using&lt;/strong&gt; &lt;strong&gt;the selected character when the button was pressed&lt;/strong&gt;, because that value was &lt;em&gt;captured&lt;/em&gt;. Unlike this, the props&lt;strong&gt; &lt;/strong&gt;are&lt;strong&gt; immutable so the object is never mutated by React.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When the parent component renders SayHiFunction with different props, React will call the SayHiFunction again. But&lt;strong&gt; the event handler we already clicked is “linked” to the previous render with its own &lt;/strong&gt;&lt;strong&gt;name value and the &lt;/strong&gt;&lt;strong&gt;showAlert callback that reads it.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;So Classes are useless?&lt;/h3&gt;
&lt;p&gt;No! Not at all. Classes are fine, we’re having a bug here because &lt;strong&gt;our implementation is incorrect, different from the original function component.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you don’t want to use a Function component, there are many ways to fix this issue. For example, you could do this:&lt;/p&gt;



&lt;div class="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="s2"&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;class&lt;/span&gt; &lt;span class="nx"&gt;SayHiClass&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="kd"&gt;const&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;=&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="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&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;showAlert&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="mi"&gt;4000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nx"&gt;showAlert&lt;/span&gt; &lt;span class="o"&gt;=&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;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &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="s2"&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;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;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;Say&lt;/span&gt; &lt;span class="nx"&gt;Hi&lt;/span&gt; &lt;span class="kd"&gt;with&lt;/span&gt; &lt;span class="nx"&gt;Class&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="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;SayHiClass&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Our props were getting “lost” before, so in this case we are reading and &lt;strong&gt;capturing them early during the event.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Again, this is just one way of fixing it, there are many others but that’s not the focus of this post.&lt;/p&gt;
&lt;h3&gt;That’s it!&lt;/h3&gt;
&lt;p&gt;I hope this post was useful and now you understand the main key difference between Classes and Function components.&lt;/p&gt;
&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding React's useEffect Hook</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Wed, 15 Apr 2020 02:42:04 +0000</pubDate>
      <link>https://forem.com/francodalessio/understanding-react-s-useeffect-hook-lbg</link>
      <guid>https://forem.com/francodalessio/understanding-react-s-useeffect-hook-lbg</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zugv3teu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2Ax4_036pvFl4Pwopb" class="article-body-image-wrapper"&gt;&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--zugv3teu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2Ax4_036pvFl4Pwopb"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@sickhews?utm_source=medium&amp;amp;utm_medium=referral"&gt;Wes Hicks&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;React Hooks are awesome and definitely make our lives as React developers much easier. In case you don’t know what they are, Hooks&lt;strong&gt; &lt;/strong&gt;were&lt;strong&gt; introduced in React 16.8.0&lt;/strong&gt;, and they let you &lt;strong&gt;use state and other React features without writing a class &lt;/strong&gt;💃&lt;/p&gt;
&lt;p&gt;The most important thing is that&lt;strong&gt; Hooks don’t replace your knowledge of React concepts.&lt;/strong&gt; Instead, Hooks provide a &lt;strong&gt;more direct API&lt;/strong&gt; to the React concepts you already know: props, state, context, refs, and lifecycle.&lt;/p&gt;
&lt;blockquote&gt;Note: if you’re new to React, learn Hooks and functional components first. Only after that, learn the old way of doing things (class components)&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;This post is not about Hooks&lt;/strong&gt; in general, but if you want to know more about them and why they exist, the best thing you can do is watching the official &lt;strong&gt;React Hooks presentation&lt;/strong&gt;:&lt;/p&gt;
&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/dpw9EHDh2bM"&gt;
&lt;/iframe&gt;


&lt;p&gt;If you haven’t learned Hooks yet, you should do it as soon as possible. And if you’ve been reading about them, you may be a bit &lt;strong&gt;confused about the &lt;/strong&gt;&lt;strong&gt;useEffect Hook 😕&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;The useEffect Hook&lt;/h2&gt;
&lt;p&gt;Plain and simple, useEffect lets you perform &lt;strong&gt;side effects&lt;/strong&gt; in function components.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Ok… But what are side effects? &lt;/em&gt;😒&lt;/p&gt;
&lt;p&gt;Side effects are all the &lt;strong&gt;operations that affect your component and can’t be done during rendering&lt;/strong&gt;. Things like &lt;strong&gt;fetching data, subscriptions or manually changing the DOM&lt;/strong&gt; are all examples of side effects, and things you’ve most likely done in the past.&lt;/p&gt;
&lt;h3&gt;What are the benefits? ✅&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Avoiding duplication of code&lt;/li&gt;
&lt;li&gt;Bringing related code closer together&lt;/li&gt;
&lt;li&gt;Avoiding bugs&lt;/li&gt;
&lt;li&gt;Separating separate effects by their purpose, something impossible to do with classes&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All these benefits will become clearer once you read the following explanation and examples.&lt;/p&gt;
&lt;h3&gt;The old way of doing things 🔙&lt;/h3&gt;
&lt;p&gt;In the past, when we only had class components, we handled side effects using the &lt;strong&gt;lifecycle methods.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For example, among other stuff, we used &lt;strong&gt;componentDidMount&lt;/strong&gt; to fetch data from APIs, &lt;strong&gt;componentDidUpdate&lt;/strong&gt; to send data when something changed, and &lt;strong&gt;componentWillUnmount&lt;/strong&gt; to unsubscribe from events.&lt;/p&gt;



&lt;div class="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;MyComponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&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="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="nx"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Fetch data from API&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;componentDidUpdate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevProps&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Send data when props change&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;componentWillUnmount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Unsubscribe from events before the component is removed&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;useEffect&lt;em&gt; &lt;/em&gt;combines all those three methods into one convenient API.&lt;/p&gt;
&lt;h3&gt;A paradigm shift ⚡️&lt;/h3&gt;
&lt;p&gt;To really understanduseEffect, we need to &lt;strong&gt;change the way we think about components and state changes.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When we used &lt;strong&gt;classes&lt;/strong&gt;, we had to think about &lt;strong&gt;&lt;em&gt;when&lt;/em&gt;&lt;/strong&gt;. The question we needed to answer was &lt;strong&gt;&lt;em&gt;when does this effect run?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;useEffect is different.&lt;/p&gt;
&lt;blockquote&gt;&lt;strong&gt;It exists to synchronize the state of the world with the state of the component.&lt;/strong&gt;&lt;/blockquote&gt;
&lt;p&gt;So the question now is: &lt;strong&gt;&lt;em&gt;which state does this effect synchronize with?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You need to stop thinking about lifecycles and time, and &lt;strong&gt;start thinking about state and synchronization with the DOM.&lt;/strong&gt;&lt;/p&gt;
&lt;h2&gt;How does useEffect work? ⚙️&lt;/h2&gt;
&lt;p&gt;By default, useEffect&lt;em&gt; &lt;/em&gt;&lt;strong&gt;runs after every render, including the first one. React guarantees the DOM has been updated &lt;/strong&gt;by the time it runs the effects.&lt;/p&gt;
&lt;p&gt;When you use useEffect, you’re &lt;strong&gt;telling React that your component needs to do something after rendering&lt;/strong&gt;. You pass a &lt;strong&gt;function&lt;/strong&gt; (the “&lt;em&gt;effect&lt;/em&gt;”), and React will &lt;strong&gt;remember it and call it later&lt;/strong&gt;, after performing the DOM updates.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// this side effect will run after every render&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;h3&gt;What about cleanups? 🗑️&lt;/h3&gt;
&lt;p&gt;We said earlier that useEffectalso “replaces” &lt;strong&gt;componentWillUnmount&lt;/strong&gt; method.&lt;/p&gt;
&lt;p&gt;A common use case for this method is to &lt;strong&gt;unsubscribe&lt;/strong&gt; from events &lt;strong&gt;before the component is finally unmounted.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To replicate that behavior with useEffect, you just need to &lt;strong&gt;return a function. &lt;/strong&gt;React will take care of everything and &lt;strong&gt;run it when it is time to clean up.&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// this side effect will run after every render&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// this code will run before the component is unmounted&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;The dependencies array&lt;/h3&gt;
&lt;p&gt;Of course, &lt;strong&gt;running all effects on every render might lead to performance issues&lt;/strong&gt; (and even infinite loops in some cases).&lt;/p&gt;
&lt;p&gt;However, &lt;strong&gt;React doesn’t know what your function does&lt;/strong&gt; before calling it. It may seem obvious to you that it’s not necessary to run some effect function again, but not for React.&lt;/p&gt;
&lt;p&gt;So to fix that, you need to &lt;strong&gt;&lt;em&gt;tell&lt;/em&gt;&lt;/strong&gt; React. You can provide a second argument to useEffect, which is a &lt;strong&gt;dependencies array.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Think of it like telling React &lt;strong&gt;&lt;em&gt;“This function only needs to run if X is updated&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;”. &lt;/em&gt;If each of these values is the same between the current and the previous time this effect ran, there’s &lt;strong&gt;nothing to synchronize&lt;/strong&gt; and React will &lt;strong&gt;skip the effect.&lt;/strong&gt;&lt;/p&gt;
&lt;br&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// this side effect will run only when value1 changes&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value1&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;If you pass an &lt;strong&gt;empty array&lt;/strong&gt; as second argument, the function will &lt;strong&gt;run just once, after the first render&lt;/strong&gt;. This tells React that your effect &lt;strong&gt;doesn’t depend on any values&lt;/strong&gt; from props or state, so it &lt;strong&gt;never needs to re-run.&lt;/strong&gt;&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// this side effect will run just once, after the first 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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So a summary would be:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No dependencies array&lt;/strong&gt;: the function runs after &lt;strong&gt;every render&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Empty dependencies array&lt;/strong&gt;: the function runs only after&lt;strong&gt; &lt;/strong&gt;the&lt;strong&gt; first render&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependencies array with some values&lt;/strong&gt;: the function runs only if any of those values &lt;strong&gt;change&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you use an &lt;strong&gt;empty dependencies array&lt;/strong&gt;, don’t do it because you think “I need this to run just once, after the first render”; do it because you know that &lt;strong&gt;the stuff it’s doing will never get stale.&lt;/strong&gt;&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// this side effect will run just once, after the first render&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// this side effect will run only when value1 changes&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;value1&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// this side effect will run after every render&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;h3&gt;Be careful with your dependencies array ⚠️&lt;/h3&gt;
&lt;p&gt;If you use the dependencies array, make sure it includes &lt;strong&gt;all values from the component scope (such as props and state) that change over time and that are used by the effect&lt;/strong&gt;. It’s very common to forget a value or to think that you don’t need it in the array. If you do that, you will produce bugs, because your code will reference stale values from previous renders.&lt;/p&gt;
&lt;p&gt;To help with this, always use &lt;a href="https://www.npmjs.com/package/eslint-plugin-react-hooks"&gt;&lt;strong&gt;eslint-plugin-react-hooks&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;You can use multiple effects! 😏&lt;/h3&gt;
&lt;p&gt;You can calluseEffect &lt;strong&gt;as many times as you want&lt;/strong&gt;. In fact, it’s recommended to call it several times, in order to &lt;strong&gt;separate concerns.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For example, there are some things that you need to do only after the first render and never again, and other things that need to happen after every render.&lt;/p&gt;
&lt;br&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// I need to do this just once, after the first render&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// But this other thing needs to be done after every render&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;Goodbye code duplication! 👋🏼&lt;/h3&gt;
&lt;p&gt;useEffect helps to avoid code duplication in a very common case.&lt;/p&gt;
&lt;p&gt;Let’s say you need to &lt;strong&gt;run an effect after every render&lt;/strong&gt;. It doesn’t matter if the component has just been mounted or it has just been updated, you need to perform certain side effect in both cases.&lt;/p&gt;
&lt;p&gt;To do this with &lt;strong&gt;classes&lt;/strong&gt; you need to &lt;strong&gt;duplicate&lt;/strong&gt; &lt;strong&gt;code&lt;/strong&gt;:&lt;/p&gt;
&lt;br&gt;

&lt;div class="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;Mycomponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&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="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="nx"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do something&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;  
  &lt;span class="nx"&gt;componentDidUpdate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    
    &lt;span class="c1"&gt;// do the same thing again&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;But with useEffect, the code will run &lt;strong&gt;after every render, including the first one.&lt;/strong&gt;&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useEffect&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="c1"&gt;// do something&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;




&lt;h3&gt;Keep related code together 👫&lt;/h3&gt;
&lt;p&gt;Let’s say we have a component where we need to subscribe to an event, and then unsubscribe from it before the component is removed. Here’s how we would do that with classes:&lt;/p&gt;
&lt;br&gt;

&lt;div class="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;Mycomponent&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&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="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="nx"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// susbscribe to event X&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;componentWillUnmount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;    
    &lt;span class="c1"&gt;// unsubscribe from event X&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="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Note how&lt;strong&gt; lifecycle methods&lt;/strong&gt; force us to &lt;strong&gt;split&lt;/strong&gt; this logic, even though the code in both of them is &lt;strong&gt;related to the same effect.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;With useEffect, we can keep this code together in the same function:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;MyComponent&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;useEffect&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="c1"&gt;// subscribe to event X&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;cleanup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;       
      &lt;span class="c1"&gt;// unsubscribe from event X&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;h3&gt;useEffect(fn, []) vs. componentDidMount 🥊&lt;/h3&gt;
&lt;p&gt;For what I’ve told you so far, you may think that’s right. I’ve told you that &lt;strong&gt;if the dependencies array is empty, your effect will run only after the first render, &lt;/strong&gt;which is how componentDidMount works, right?&lt;/p&gt;
&lt;p&gt;Well, &lt;strong&gt;there is a difference.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;useEffect runs &lt;strong&gt;after&lt;/strong&gt; the paint has been committed to the screen. componentDidMount runs &lt;strong&gt;before&lt;/strong&gt; the paint has been commited to the screen.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;For most cases, you won’t need to worry&lt;/strong&gt; about this, because most of your effects will be &lt;strong&gt;asynchronous&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;But there are some things that require that you read the DOM and &lt;strong&gt;synchronously re-render&lt;/strong&gt;. For example, if you need to measure the layout.&lt;/p&gt;
&lt;p&gt;For these cases, there is a &lt;strong&gt;separate &lt;/strong&gt;&lt;a href="https://reactjs.org/docs/hooks-reference.html#uselayouteffect"&gt;&lt;strong&gt;useLayoutEffect&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; Hook&lt;/strong&gt; with an API identical to useEffect.&lt;/p&gt;
&lt;h2&gt;That’s all!&lt;/h2&gt;
&lt;p&gt;useEffect can be a little tricky, and it requires some practice. I hope this guide helps, and always remember: &lt;strong&gt;don’t think about lifecycles, think about synchronizing side effects to state.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you need more information, you can check &lt;a href="https://reactjs.org/docs/hooks-effect.html"&gt;React’s official documentation&lt;/a&gt; or &lt;a href="https://overreacted.io/a-complete-guide-to-useeffect/"&gt;this awesome guide&lt;/a&gt; by &lt;a href="https://twitter.com/dan_abramov"&gt;Dan Abramov&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--03FY6U3l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://medium.com/_/stat%3Fevent%3Dpost.clientViewed%26referrerSource%3Dfull_rss%26postId%3Df00cfbfbb7e" width="1" height="1"&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Basic accessibility for your HTML forms</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Sat, 11 Apr 2020 01:21:38 +0000</pubDate>
      <link>https://forem.com/francodalessio/basic-accessibility-for-your-html-forms-3en8</link>
      <guid>https://forem.com/francodalessio/basic-accessibility-for-your-html-forms-3en8</guid>
      <description>&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2A_FJ7rdJgXtmfR-dR" class="article-body-image-wrapper"&gt;&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2A_FJ7rdJgXtmfR-dR"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@glenncarstenspeters?utm_source=medium&amp;amp;utm_medium=referral" rel="noopener noreferrer"&gt;Glenn Carstens-Peters&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Today I want to write about a simple, yet powerful element you &lt;strong&gt;must&lt;/strong&gt; include when building an HTML form: &lt;strong&gt;labels.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You probably know what a label is. It tells the user &lt;strong&gt;what the field is all about.&lt;/strong&gt; For example, when you see this:&lt;/p&gt;
&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F506%2F1%2AxUCpjPEJSbkj7Bs4gacrCQ.png"&gt;&lt;p&gt;You know that &lt;strong&gt;you’re supposed to write your name&lt;/strong&gt; in this input field.&lt;/p&gt;
&lt;p&gt;You can write that code in two different ways. This is one option:&lt;/p&gt;
&lt;br&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;Name:
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;And this is the second option you have:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;Name: &lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;For a person who can see, there is no difference&lt;/strong&gt;. Both pieces of code will display the same on the screen.&lt;/p&gt;
&lt;p&gt;But blind users use a &lt;strong&gt;screen reader,&lt;/strong&gt; and &lt;strong&gt;without the &lt;/strong&gt;&lt;strong&gt;label tag, they won’t know&lt;/strong&gt; what the input expects them to write 😕&lt;/p&gt;
&lt;p&gt;So now we know that we need to use label tags for every field in our forms. These are the elements that use explicitly associated labels:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;input type="text"&lt;/li&gt;
&lt;li&gt;input type="checkbox"&lt;/li&gt;
&lt;li&gt;input type="radio"&lt;/li&gt;
&lt;li&gt;input type="file"&lt;/li&gt;
&lt;li&gt;input type="password"&lt;/li&gt;
&lt;li&gt;textarea&lt;/li&gt;
&lt;li&gt;select&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;But it’s not enough… 🛑&lt;/h3&gt;
&lt;p&gt;Just because we added a label tag it doesn’t mean our form is ready. We’re missing the most important part, which is &lt;strong&gt;associating the label to the input element.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;These are the two best ways to do it:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;1- Put the &lt;/strong&gt;&lt;strong&gt;input inside the &lt;/strong&gt;&lt;strong&gt;label tag&lt;/strong&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;
  Name
  &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2- Add &lt;/strong&gt;&lt;strong&gt;"for" attribute on the label and &lt;/strong&gt;&lt;strong&gt;"id" attribute on the input&lt;/strong&gt;&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"last-name"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Last name:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"last-name"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;value&lt;/strong&gt; must be the &lt;strong&gt;same&lt;/strong&gt; for both attributes.&lt;/p&gt;
&lt;p&gt;There is a third way of associating a label to the input, which is this one:&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;aria-label=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;p&gt;This works for screen readers, but it &lt;strong&gt;removes the visual label&lt;/strong&gt; from the screen. This is not good because the users &lt;strong&gt;won’t know what they are supposed to write&lt;/strong&gt; in the input.&lt;/p&gt;
&lt;p&gt;You may think that adding a placeholder can fix the issue, but only partially. When you type, the placeholder &lt;strong&gt;disappears&lt;/strong&gt;. Once you end filling out the form and you want to check what you wrote, you’ve again the same problem: &lt;strong&gt;you don’t know what each field is about&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;So it’s better to &lt;strong&gt;use one of the first two options&lt;/strong&gt;. When you successfully associate a label with an input element, the label area becomes clickable as well 🎉 This is a good way to check if you did a good job: &lt;strong&gt;click the label and the input should be focused.&lt;/strong&gt;&lt;/p&gt;
&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F332%2F1%2AHL-FFyeeV_FQqP-mL71CLQ.gif"&gt;&lt;h3&gt;One more thing&lt;/h3&gt;
&lt;p&gt;When you are done building a form, &lt;strong&gt;always&lt;/strong&gt; check if you can fill all the fields by &lt;strong&gt;just using your keyboard&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;This seems obvious, but a lot of forms (and I mean &lt;strong&gt;a lot&lt;/strong&gt;) are impossible to use without a mouse. You need to think there are lots of people that unfortunately &lt;strong&gt;cannot use a mouse&lt;/strong&gt;, and therefore they won’t be able to fill out your form.&lt;/p&gt;
&lt;h3&gt;That’s all!&lt;/h3&gt;
&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedium.com%2F_%2Fstat%3Fevent%3Dpost.clientViewed%26referrerSource%3Dfull_rss%26postId%3D65aab327dfb"&gt;

</description>
      <category>webdev</category>
      <category>html</category>
    </item>
    <item>
      <title>Understanding React elements and JSX</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Fri, 10 Apr 2020 17:41:04 +0000</pubDate>
      <link>https://forem.com/francodalessio/understanding-react-elements-and-jsx-4dc3</link>
      <guid>https://forem.com/francodalessio/understanding-react-elements-and-jsx-4dc3</guid>
      <description>&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%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2A9kLrAfGF2nbVGo9n" class="article-body-image-wrapper"&gt;&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F0%2A9kLrAfGF2nbVGo9n"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@flowforfrank?utm_source=medium&amp;amp;utm_medium=referral" rel="noopener noreferrer"&gt;Ferenc Almasi&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Let’s start by writing a simple piece of code to display a &lt;strong&gt;&lt;em&gt;“Hello World”&lt;/em&gt; &lt;/strong&gt;message, using just &lt;strong&gt;vanilla Javascript and browser APIs&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;First, let’s create a root div in our body section:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"root"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;We will now &lt;strong&gt;create a new &lt;/strong&gt;&lt;strong&gt;div&lt;/strong&gt; with the &lt;em&gt;Hello World&lt;/em&gt; message and &lt;strong&gt;append&lt;/strong&gt; it to our root div using Javascript.&lt;/p&gt;
&lt;p&gt;We need to &lt;strong&gt;get that root &lt;/strong&gt;&lt;strong&gt;div&lt;/strong&gt; element first:&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;rootElement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&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;Then&lt;strong&gt; create a new div&lt;/strong&gt; and add the text content:&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;And finally &lt;strong&gt;append&lt;/strong&gt; this new child element to the root element:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;rootElement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;That’s all we need to display a &lt;em&gt;Hello World&lt;/em&gt; message. Here is the final code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;




&lt;h3&gt;Let’s do the same with React&lt;/h3&gt;
&lt;p&gt;React &lt;strong&gt;controls and updates the DOM&lt;/strong&gt; using the same APIs we used in our previous example. But instead of creating DOM elements, we’ll create &lt;strong&gt;React elements&lt;/strong&gt;. react-dom will take care of &lt;strong&gt;converting them into DOM elements&lt;/strong&gt; and putting them into the page.&lt;/p&gt;
&lt;h4&gt;What is a React element?&lt;/h4&gt;
&lt;p&gt;Think about React elements as a &lt;strong&gt;description of what you see on the screen. &lt;/strong&gt;React &lt;strong&gt;reads these objects and uses them to construct the DOM&lt;/strong&gt; and keep it up to date.&lt;/p&gt;
&lt;blockquote&gt;Don’t confuse React elements with &lt;strong&gt;components&lt;/strong&gt;. Elements are what components are “made of”, and you must first understand what they are before jumping ahead into components.&lt;/blockquote&gt;
&lt;h4&gt;The createElement API&lt;/h4&gt;
&lt;p&gt;For this first React example we’ll use React’s &lt;strong&gt;createElement API.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;We need to get React on the page. We'll add &lt;strong&gt;react&lt;/strong&gt; and &lt;strong&gt;react-dom&lt;/strong&gt; directly from &lt;a href="https://unpkg.com/" rel="noopener noreferrer"&gt;unpkg&lt;/a&gt; as script tags.&lt;/p&gt;

&lt;p&gt;Then, instead of creating a div element we create a &lt;strong&gt;React element&lt;/strong&gt;. The API for react.createElement is:&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;function&lt;/span&gt; &lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elementType&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="nx"&gt;children&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;p&gt;We don’t have any props in our example, so we’d create our element like this:&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&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="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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;The text &lt;em&gt;Hello World&lt;/em&gt; is our element’s child.&lt;/p&gt;
&lt;p&gt;We can see this &lt;strong&gt;React element&lt;/strong&gt; by logging it to the console:&lt;/p&gt;
&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F389%2F1%2AI371zGvt8laD7-jcC54syg.png"&gt;&lt;p&gt;We don’t get a DOM element, but an &lt;strong&gt;object&lt;/strong&gt;. Remember we said earlier that React elements are &lt;strong&gt;objects that describe what you see on the screen.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You can see that &lt;strong&gt;children&lt;/strong&gt; is actually inside the &lt;strong&gt;props&lt;/strong&gt; object.&lt;/p&gt;
&lt;p&gt;These two lines are &lt;strong&gt;equivalent&lt;/strong&gt; and both correct:&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&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="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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;div&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;children&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;You can pass the &lt;strong&gt;children&lt;/strong&gt; inside the &lt;strong&gt;props&lt;/strong&gt; object or as the &lt;strong&gt;third parameter&lt;/strong&gt; of the function.&lt;/blockquote&gt;
&lt;p&gt;Ok, we’ve got our &lt;strong&gt;React element&lt;/strong&gt;. Now what?&lt;/p&gt;
&lt;p&gt;Instead of appending this element to the root element, as we did in the first example with our DOM element, we’ll let &lt;strong&gt;react-dom&lt;/strong&gt; handle all that:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;ReactDOM&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;rootElement&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


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

&lt;/div&gt;
&lt;p&gt;The end result will be the same as the first example. This is the final code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;




&lt;h3&gt;Finally, let’s use JSX! 🚀&lt;/h3&gt;
&lt;p&gt;This react.createElement syntax works, but is not the best. Fortunately, we have another option, which is also the &lt;strong&gt;standard way&lt;/strong&gt; the community uses to create React elements.&lt;/p&gt;
&lt;h4&gt;What is JSX? 🤔&lt;/h4&gt;
&lt;p&gt;JSX is an &lt;strong&gt;extension to Javascript&lt;/strong&gt; to support syntax that&lt;strong&gt; &lt;em&gt;looks like &lt;/em&gt;the HTML code&lt;/strong&gt; you would write to create these DOM elements.&lt;/p&gt;
&lt;p&gt;So we can create our element by just doing this:&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;element&lt;/span&gt; &lt;span class="o"&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="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Sweet, right?&lt;/p&gt;
&lt;h4&gt;Why JSX?&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;React doesn’t require you to use JSX&lt;/strong&gt;. You can use the createElement API and everything will work in the same way.&lt;/p&gt;
&lt;p&gt;However, JSX is of great help. React is based on the principle that &lt;strong&gt;rendering logic is coupled with UI logic&lt;/strong&gt;. Instead of separating them by putting markup and logic in separate files, React uses &lt;strong&gt;components &lt;/strong&gt;that contain both.&lt;/p&gt;
&lt;p&gt;Using JSX is helpful because it allows you to &lt;strong&gt;easily work with UI inside Javascript code.&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;JSX is awesome! But we still have a problem…&lt;/h4&gt;
&lt;p&gt;The browser does not understand JSX natively, so it needs to be &lt;strong&gt;compiled&lt;/strong&gt;. That’s where &lt;a href="https://babeljs.io/" rel="noopener noreferrer"&gt;&lt;strong&gt;Babel&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; &lt;/strong&gt;comes in. It will convert JSX syntax to &lt;strong&gt;something the browser can understand.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;What do you think that will be?&lt;/p&gt;
&lt;p&gt;Exactly: it will use &lt;strong&gt;react.createElement API&lt;/strong&gt;.&lt;/p&gt;
&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1002%2F1%2AHHliaZ5VAwrczGOAiiPWgA.png"&gt;&lt;p&gt;You can try this yourself in the &lt;a href="https://babeljs.io/repl" rel="noopener noreferrer"&gt;Babel’s website&lt;/a&gt;.&lt;/p&gt;
&lt;h4&gt;Adding props&lt;/h4&gt;
&lt;p&gt;In the last example, the second argument to createElement is null because we have no &lt;strong&gt;props&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;We can include a className and see how Babel translates it to be the &lt;strong&gt;second argument&lt;/strong&gt; of the createElement function:&lt;/p&gt;
&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1008%2F1%2AHmfyDwyeG-odk-nV2pQH9w.png"&gt;&lt;h4&gt;Adding children&lt;/h4&gt;
&lt;p&gt;We can also add more children. Right now, the only child is the &lt;em&gt;Hello World&lt;/em&gt; text, but we can add more:&lt;/p&gt;
&lt;img alt="" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F1024%2F1%2ALzAXHU9aZOMggf7RtkHHDg.png"&gt;&lt;p&gt;You can see that now the third argument of the createElement function is not just a string. Instead it’s &lt;strong&gt;a second React element of type &lt;/strong&gt;&lt;strong&gt;p&lt;/strong&gt;, which has the &lt;em&gt;Hello World&lt;/em&gt; string text as its child.&lt;/p&gt;
&lt;h3&gt;The power of JSX 💪&lt;/h3&gt;
&lt;p&gt;With JSX you can do more than just creating React elements. Let’s take a look at everything JSX has to offer.&lt;/p&gt;
&lt;h4&gt;Embedding expressions&lt;/h4&gt;
&lt;p&gt;Using &lt;strong&gt;curly braces&lt;/strong&gt;, you can embed any Javascript expression inside JSX.&lt;/p&gt;
&lt;p&gt;For example, you can do this:&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&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;element&lt;/span&gt; &lt;span class="o"&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="nx"&gt;Hello&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;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="nx"&gt;ReactDOM&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&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;p&gt;And Hello John will be displayed on the page.&lt;/p&gt;

&lt;p&gt;But it doesn’t have to be just a static string value, you can embed &lt;strong&gt;any Javascript expression:&lt;/strong&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;element&lt;/span&gt; &lt;span class="o"&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="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="mi"&gt;2&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&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="nx"&gt;ReactDOM&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="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
 &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&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;p&gt;In this case, 2+2 = 4 will be displayed on the page.&lt;/p&gt;
&lt;h4&gt;JSX is an expression&lt;/h4&gt;
&lt;p&gt;We know that JSX will be &lt;strong&gt;compiled&lt;/strong&gt; to be a react.createElement function, and that function will return an &lt;strong&gt;object&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;That means that we can &lt;strong&gt;assign JSX to variables:&lt;/strong&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;element&lt;/span&gt; &lt;span class="o"&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="nx"&gt;Hello&lt;/span&gt; &lt;span class="nx"&gt;World&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or, for example, &lt;strong&gt;return JSX from functions:&lt;/strong&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;getAgeDescription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;50&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;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;You&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;re young&amp;lt;/h1&amp;gt;;
  } 
  return &amp;lt;h1&amp;gt;You&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="nx"&gt;re&lt;/span&gt; &lt;span class="nx"&gt;old&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;


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

&lt;/div&gt;


&lt;h4&gt;Specifying attributes&lt;/h4&gt;
&lt;p&gt;You can use &lt;strong&gt;quotes&lt;/strong&gt; to specify &lt;strong&gt;string literals as attributes:&lt;/strong&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://xyz.com/img.jpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/img&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;

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

&lt;/div&gt;

&lt;p&gt;Or &lt;strong&gt;curly braces&lt;/strong&gt; to embed a &lt;strong&gt;Javascript expression:&lt;/strong&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;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;profileImgUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://xyz.com/img.jpg&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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&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;profileImgUrl&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/img&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;

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

&lt;/div&gt;

&lt;p&gt;JSX uses &lt;strong&gt;camelCase&lt;/strong&gt; for attributes, and there’re some changes you have to keep in mind. As some words are reserved by Javascript, &lt;strong&gt;some attributes change its name when using JSX.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;For example, you don’t use class in JSX, but className. You don’t use for in a label element, you use htmlFor .&lt;/p&gt;
&lt;p&gt;To read more about these exceptions and all the &lt;strong&gt;accepted attributes&lt;/strong&gt; in JSX you can check the &lt;a href="https://reactjs.org/docs/dom-elements.html" rel="noopener noreferrer"&gt;&lt;strong&gt;official documentation&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;Self-closing tags&lt;/h4&gt;
&lt;p&gt;If an element doesn’t have any children, you can close it with /&amp;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;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;img&lt;/span&gt; &lt;span class="nx"&gt;src&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;profileImgUrl&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;That’s all!&lt;/h3&gt;
&lt;p&gt;&lt;strong&gt;Understanding how JSX is compiled is important because it will make you more effective at using JSX.&lt;/strong&gt; I encourage you to take you React code to the Babel website to see how it is compiled and how it looks using the createElement API.&lt;/p&gt;
&lt;p&gt;I’ll leave this here. There are a lot of things I could continue writing about (rendering, components, etc.), but I’ll do so in separate articles. &lt;strong&gt;JSX is an important concept&lt;/strong&gt; and you should &lt;strong&gt;understand it well before moving&lt;/strong&gt; &lt;strong&gt;on&lt;/strong&gt; to other stuff.&lt;/p&gt;
&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedium.com%2F_%2Fstat%3Fevent%3Dpost.clientViewed%26referrerSource%3Dfull_rss%26postId%3Dc3b7af356db4"&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Understanding the importance of the key prop in React 🗝</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Wed, 01 Apr 2020 22:46:14 +0000</pubDate>
      <link>https://forem.com/francodalessio/understanding-the-importance-of-the-key-prop-in-react-3ag7</link>
      <guid>https://forem.com/francodalessio/understanding-the-importance-of-the-key-prop-in-react-3ag7</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TCo8xT2_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2Afo-qV_stnWqGeQ6Q" class="article-body-image-wrapper"&gt;&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--TCo8xT2_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/0%2Afo-qV_stnWqGeQ6Q"&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@sfhaneavery?utm_source=medium&amp;amp;utm_medium=referral"&gt;Shane Avery&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;If you’ve written some React code, you’ve most probably seen this warning:&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--Fd4Iuw9d--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/415/1%2AKbPoFSYQN9Sldi2GkHEDAQ.png"&gt;&lt;p&gt;Almost every React application displays an array list of some kind using the method &lt;strong&gt;map&lt;/strong&gt;. And React tells us that for &lt;strong&gt;each element of that list&lt;/strong&gt; that we return for rendering, we must provide a &lt;strong&gt;unique key prop&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;But do you know why it’s necessary?&lt;/p&gt;
&lt;h3&gt;Why does React need this key prop? 🔑&lt;/h3&gt;
&lt;p&gt;The official documentation clarifies this perfectly:&lt;/p&gt;
&lt;blockquote&gt;Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity.&lt;/blockquote&gt;
&lt;p&gt;When the state of your component changes, the render function will return a new tree of React elements, different to the previous/current one. &lt;strong&gt;React needs to figure out what are the differences to efficiently update the UI&lt;/strong&gt; with the most recent changes. This process of matching both element trees is called &lt;strong&gt;reconciliation.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;You can read more about React’s &lt;strong&gt;diffing algorithm &lt;/strong&gt;&lt;a href="https://reactjs.org/docs/reconciliation.html#recursing-on-children"&gt;here&lt;/a&gt;, but the important thing for us are the lists.&lt;/p&gt;
&lt;h4&gt;So… What’s exactly the problem with lists?&lt;/h4&gt;
&lt;p&gt;Well, imagine that you render a list and you don’t provide keys for its elements:&lt;/p&gt;
&lt;pre&gt;&amp;lt;li&amp;gt;Item 1&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Item 2&amp;lt;/li&amp;gt;&lt;/pre&gt;
&lt;p&gt;Then imagine a new &lt;strong&gt;Item 3 &lt;/strong&gt;is added. The new tree looks like this:&lt;/p&gt;
&lt;pre&gt;&amp;lt;li&amp;gt;Item 1&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Item 2&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Item 3&amp;lt;/li&amp;gt;&lt;/pre&gt;
&lt;p&gt;React now needs to &lt;strong&gt;compare these two trees&lt;/strong&gt; to identify the changes made. And to do that, React &lt;strong&gt;iterates over both lists of children&lt;/strong&gt; at the same time and &lt;strong&gt;generates a mutation whenever there’s a difference.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;So it will match the first two elements, and generate a mutation for the third one. Good. &lt;strong&gt;So far, no problems 👍&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Now, imagine that&lt;strong&gt; a new item is added, but at the beginning&lt;/strong&gt;. This is the new tree now:&lt;/p&gt;
&lt;pre&gt;&amp;lt;li&amp;gt;Item 0&amp;lt;/li&amp;gt; // &amp;lt;= New item&lt;br&gt;&amp;lt;li&amp;gt;Item 1&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Item 2&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li&amp;gt;Item 3&amp;lt;/li&amp;gt;&lt;/pre&gt;
&lt;p&gt;Once again, &lt;strong&gt;React will&lt;/strong&gt; &lt;strong&gt;iterate, compare and generate mutations&lt;/strong&gt; where needed.&lt;/p&gt;
&lt;p&gt;When comparing the first element of the old tree (Item 1) with the first element of the new tree (Item 0), &lt;strong&gt;they are different&lt;/strong&gt;, so a mutation is generated.&lt;/p&gt;
&lt;p&gt;When comparing the second element of the old tree (Item 2) with the second element of the new tree (Item 1), &lt;strong&gt;they are different&lt;/strong&gt;, so a mutation is generated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/26xBI73gWquCBBCDe/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/26xBI73gWquCBBCDe/source.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And so on… Do you see the problem? &lt;strong&gt;React will mutate every child instead of realizing it can keep items 1, 2 and 3 intact&lt;/strong&gt;. This inefficiency is a problem 😕&lt;/p&gt;
&lt;h4&gt;Key prop to the rescue!&lt;/h4&gt;
&lt;p&gt;This is why the key prop is needed, to solve this issue. When children have keys, &lt;strong&gt;React uses the key to match children in the old tree with children in the new tree.&lt;/strong&gt;&lt;/p&gt;
&lt;pre&gt;&amp;lt;li key={0}&amp;gt;Item 0&amp;lt;/li&amp;gt; // &amp;lt;= New item&lt;br&gt;&amp;lt;li key={1}&amp;gt;Item 1&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li key={2}&amp;gt;Item 2&amp;lt;/li&amp;gt;&lt;br&gt;&amp;lt;li key={3}&amp;gt;Item 3&amp;lt;/li&amp;gt;&lt;/pre&gt;
&lt;p&gt;Now React knows right away that the new item is &amp;lt;li&amp;gt;Item 0&amp;lt;/li&amp;gt;and the other items have just moved. 👌&lt;/p&gt;
&lt;h3&gt;Ok, got it. But will my app break if I don’t use keys? 🤔&lt;/h3&gt;
&lt;p&gt;It depends. Apart from the warning, &lt;strong&gt;your application won’t throw an error&lt;/strong&gt;, but &lt;strong&gt;you may produce bugs&lt;/strong&gt; if you don’t provide keys. If you change the order of the list items (either by sorting the list or removing an item, for example), weird things will happen. You’ll see it in the following example.&lt;/p&gt;
&lt;h3&gt;Let’s code to make it clearer&lt;/h3&gt;
&lt;p&gt;I’ve coded a very simple app where you are presented with a&lt;strong&gt; list of emojis&lt;/strong&gt; and you can &lt;strong&gt;rate&lt;/strong&gt; each of them.&lt;/p&gt;
&lt;p&gt;If the app was just that, the absence of keys wouldn’t produce any bug. The list is static and there’s no way the items are reordered.&lt;/p&gt;
&lt;p&gt;But to illustrate the problem, I’ve also added the possibility to &lt;strong&gt;remove&lt;/strong&gt; the emojis from the list.&lt;/p&gt;
&lt;p&gt;You can play with it here:&lt;/p&gt;
&lt;iframe src="https://codesandbox.io/embed/rateemojisno-key-th1g2"&gt;
&lt;/iframe&gt;

&lt;p&gt;If you check the console, you’ll see that &lt;strong&gt;React is warning us&lt;/strong&gt; about not having the key property for our list items.&lt;/p&gt;
&lt;p&gt;To see what happens when we don’t provide a key, do the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Rate only the 😘 emoji as “&lt;em&gt;Very good&lt;/em&gt;”&lt;/li&gt;
&lt;li&gt;Delete the 😘 emoji&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Do you see the problem now? In case you don’t want to play with the sandbox (or it doesn’t work), here is what happens:&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--xg5lIGk5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/311/1%2Aqt6TC_inDDGcR_h6Lg1BIA.gif"&gt;&lt;p&gt;After the deletion, the 🤪 emoji appears rated as “&lt;em&gt;Very good&lt;/em&gt;”, which is wrong because &lt;strong&gt;we haven’t changed its rating at all 😦&lt;/strong&gt;&lt;/p&gt;
&lt;h3&gt;Using index as key&lt;/h3&gt;
&lt;p&gt;You’ll see that for some simple applications, usually used to teach or explain React concepts, people will use the &lt;strong&gt;index as key&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Do you think this &lt;strong&gt;solves the problem?&lt;/strong&gt; 🤔&lt;/p&gt;
&lt;p&gt;Spoiler alert: &lt;strong&gt;it doesn’t&lt;/strong&gt;. You can go ahead and try it, but &lt;strong&gt;the same bug will be produced.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As component instances are updated and reused based on their key,&lt;strong&gt; if the key is an index, moving an item changes it.&lt;/strong&gt; As a result, the component will be updated in unexpected ways.&lt;/p&gt;
&lt;h3&gt;So what to do then?&lt;/h3&gt;
&lt;p&gt;You need to use some &lt;strong&gt;unique value as a key&lt;/strong&gt;. In this case, I’ve included an &lt;strong&gt;id &lt;/strong&gt;field to make it easier. It doesn’t have to be a number, you can use a string if you want. In this case we could even use the emoji itself as key, because each one of them appears only once in the array, but&lt;strong&gt; be careful &lt;/strong&gt;about that.&lt;/p&gt;
&lt;p&gt;However, don’t overthink it, because the data you’ll get in a &lt;strong&gt;real life application&lt;/strong&gt; will most likely have a unique id field to identify each element.&lt;/p&gt;
&lt;p&gt;If we rewrite our list item element to be like this:&lt;/p&gt;
&lt;pre&gt;&amp;lt;li className="emoji-item" key={emoji.id}&amp;gt;&lt;/pre&gt;
&lt;p&gt;Then both the warning in the console and the problem we had at runtime will be gone 🎉&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--syrmPdHK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/314/1%2AAKcLNnxXkT85mEmCvEPxJQ.gif"&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/10ERZqYioLWJ6U/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/10ERZqYioLWJ6U/source.gif"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;So… What do you think? 😃&lt;/h3&gt;
&lt;p&gt;Was this useful? I hope it was, and please forgive me (and let me know 🙏) if there is any error in the code.&lt;/p&gt;
&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--X1OvPJj8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://medium.com/_/stat%3Fevent%3Dpost.clientViewed%26referrerSource%3Dfull_rss%26postId%3Df2b92ce65f45" width="1" height="1"&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Creating custom React hooks for a simple To-do list app</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Wed, 01 Apr 2020 13:53:25 +0000</pubDate>
      <link>https://forem.com/francodalessio/creating-custom-react-hooks-for-a-simple-to-do-list-app-5580</link>
      <guid>https://forem.com/francodalessio/creating-custom-react-hooks-for-a-simple-to-do-list-app-5580</guid>
      <description>&lt;p&gt;In this post I’m going to do a &lt;strong&gt;refactor&lt;/strong&gt; to a simple To-do list app. The goal is to &lt;strong&gt;create some custom hooks&lt;/strong&gt; so you can see how simple it is.&lt;/p&gt;
&lt;blockquote&gt;If you don’t know what hooks are and how awesome they are, it’s a good idea to &lt;a href="https://reactjs.org/docs/hooks-intro.html"&gt;check the official documentation&lt;/a&gt; first&lt;/blockquote&gt;
&lt;h3&gt;But… Why bother in the first place? 😕&lt;/h3&gt;
&lt;p&gt;Coding a custom hook provides us with two very clear benefits. First, we can &lt;strong&gt;extract common functionality to be reused&lt;/strong&gt; by several components. We do that with components, so it makes sense to do the same with functions.&lt;/p&gt;
&lt;p&gt;And secondly, we get rid of a lot of boilerplate code in our components, which make them&lt;strong&gt; cleaner and easier to read.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;This two benefits will become clearer when you see the example ✍️&lt;/p&gt;
&lt;h3&gt;Let’s start with a simple To-do list app ✔️&lt;/h3&gt;
&lt;p&gt;I’ve coded a very simple app. It allows the user to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Type a to-do item and add it to the list&lt;/li&gt;
&lt;li&gt;See the list&lt;/li&gt;
&lt;li&gt;Mark a to-do as completed/uncompleted by clicking on it&lt;/li&gt;
&lt;li&gt;Removing a to-do from the list by clicking the red &lt;strong&gt;X &lt;/strong&gt;button next to it&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;See it in action:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/simplereacttodoapp-mn3l5"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;And here is the code:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now that we’re ready, let’s start building some custom hooks! 🚀&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/PoGg4p9nOR6yO6JvHW/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/PoGg4p9nOR6yO6JvHW/source.gif"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;
&lt;h3&gt;A custom hook to control the input field&lt;/h3&gt;
&lt;p&gt;Let’s start with a simple one. It’s a good idea to create a hook to manage the state of the input field we have. Why? Because it will be &lt;strong&gt;highly reusable&lt;/strong&gt;; every other component that has a form with input fields will be able to benefit from it.&lt;/p&gt;
&lt;p&gt;Right now we’re controlling the input field with this piece of state:&lt;/p&gt;

&lt;pre&gt;
const [todo, setTodo] = React.useState("");
&lt;/pre&gt;

&lt;p&gt;When the value is changed (the user types something) we’re calling this function:&lt;/p&gt;

&lt;pre&gt;
const handleChange = e =&amp;gt; {
  setTodo(e.target.value);
};
&lt;/pre&gt;

&lt;p&gt;And &lt;strong&gt;&lt;em&gt;onSubmit&lt;/em&gt;&lt;/strong&gt;, after the new to-do is added to the list, we’re cleaning the input field:&lt;/p&gt;
&lt;pre&gt;setTodo("");&lt;/pre&gt;
&lt;p&gt;In every component that has an input field, we would need to have these three things. &lt;strong&gt;Keep them in mind&lt;/strong&gt; because we’re going to use these 3 things in our hook.&lt;/p&gt;
&lt;h4&gt;Creating the new useInput hook&lt;/h4&gt;
&lt;p&gt;First, start by creating a new folder &lt;strong&gt;&lt;em&gt;hooks&lt;/em&gt;&lt;/strong&gt; (just to keep things organized) and create a new file called &lt;strong&gt;&lt;em&gt;useInput.js.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;The name of every official hook start with the word &lt;strong&gt;use&lt;/strong&gt;, so follow that rule when creating a custom one&lt;/blockquote&gt;
&lt;p&gt;You’ve seen that React’s &lt;strong&gt;useState&lt;/strong&gt; hook returns two things: the value and a function to update it. However, it’s not necessary that a hook returns just 2 things.&lt;/p&gt;
&lt;p&gt;In fact, we’re making &lt;strong&gt;our hook return 3 things&lt;/strong&gt;:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The value&lt;/li&gt;
&lt;li&gt;A function to handle the change (update the value)&lt;/li&gt;
&lt;li&gt;A function to reset the value (clean the input)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;As for the parameters, our hook function &lt;strong&gt;only needs one thing&lt;/strong&gt;: the initial value.&lt;/p&gt;
&lt;p&gt;This is the skeleton of our hook:&lt;/p&gt;

&lt;pre&gt;
function useInput(initialValue) { 
  // Code goes here
  return [state, handleChange, reset];
}
export default useInput;
&lt;/pre&gt;

&lt;p&gt;Now we have to add the body of our function. But actually we’ve already done it! Remember, &lt;strong&gt;we’re just extracting the logic from our component&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;So we’re going to use the 3 things we enumerated before (and I asked you to keep in mind 😉). The final hook should look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;So now we can go ahead and a&lt;strong&gt;dd the hook to our component&lt;/strong&gt;! 🚀 That includes importing it:&lt;/p&gt;
&lt;pre&gt;import useInput from './hooks/useInput';&lt;/pre&gt;
&lt;p&gt;Actually calling it:&lt;/p&gt;
&lt;pre&gt;const [todo, setTodo, resetTodo] = useInput(“”);&lt;/pre&gt;
&lt;p&gt;And also get rid of our &lt;strong&gt;handleChange&lt;/strong&gt; function and replace some of the code to use the two functions our hook provides. The component should now look like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Better, right? It’s cleaner; not a huge change because our hook is small, but remember that we’ll be able to use this hook in &lt;strong&gt;every other component that has an input field&lt;/strong&gt; 🎉&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/LqhynjraNMUL6jwkEF/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/LqhynjraNMUL6jwkEF/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;Let’s build a bigger hook 💪&lt;/h3&gt;
&lt;p&gt;Ok, so now that we’ve seen how to create a custom hook and how easy it is, let’s create a bigger one.&lt;/p&gt;
&lt;p&gt;Our app is really small and doesn’t have a lot of functionality, but it does &lt;strong&gt;share a&lt;/strong&gt; &lt;strong&gt;common thing with almost every application&lt;/strong&gt;, big or small. Can you guess what?&lt;/p&gt;
&lt;p&gt;Exactly, it uses a &lt;strong&gt;&lt;em&gt;list&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;. &lt;/em&gt;Have you ever coded a React application without using an array list? 🤔&lt;/p&gt;
&lt;p&gt;It would be great if we could create a hook to handle arrays. That would be useful not only for other components in this application, but also for virtually &lt;strong&gt;any other application&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;Creating the new useArray hook&lt;/h4&gt;
&lt;p&gt;We have 3 actions involving our to-do list:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We can add a to-do&lt;/li&gt;
&lt;li&gt;We can remove a to-do&lt;/li&gt;
&lt;li&gt;We can toggle the completed status of a to-do&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let’s move all that logic to a hook called &lt;strong&gt;useArray. &lt;/strong&gt;As this hook is going to return several functions (plus the list itself) and may even grow in the future, I’m not going to do it return an array as the previous hook. That would mean destructuring a lot of things and wouldn’t look nice.&lt;/p&gt;
&lt;p&gt;Instead, I’m going to make our function &lt;strong&gt;return an object&lt;/strong&gt;, containing all we need. There’s no rule saying that you must make your hook return an array, so this is not a problem.&lt;/p&gt;
&lt;p&gt;As with the previous hook, let’s begin with the skeleton. We know that we will be receiving the initial value for the list itself and that we’re returning that alongside with the 3 methods we mentioned:&lt;/p&gt;

&lt;pre&gt;
function useArray(initialList) {
  const [list, setList] = useState(initialList);
  return {
    list,
    addItem: () =&amp;gt; {},
    removeItem: () =&amp;gt; {},
    toggleItem: () =&amp;gt; {},
  };
}
export default useArray;
&lt;/pre&gt;

&lt;p&gt;Now we just need to extract those 3 functions from our component and add them to this new hook, which will end up looking like this:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now for the big moment 🥁 Let’s get rid of all these functions on the component and &lt;strong&gt;replace them with our brand new hook&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now that’s &lt;strong&gt;a lot cleaner&lt;/strong&gt;, right? 😎 And of course, it &lt;strong&gt;works the same as before&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/UTEwvH9aKvnlC/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/UTEwvH9aKvnlC/source.gif"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;p&gt;We got rid of a lot of code! Our component is definitely &lt;strong&gt;easier to read&lt;/strong&gt; now. And the best part, once again: &lt;strong&gt;we’ll be able to use our hook in any other component&lt;/strong&gt; that handles a list like this.&lt;/p&gt;
&lt;h3&gt;So… What do you think? 😃&lt;/h3&gt;
&lt;p&gt;What’s your opinion on custom hooks? Have you ever used it? If you haven’t yet, hopefully you will after reading this!&lt;/p&gt;
&lt;p&gt;I hope it was useful and please forgive me (and let me know 🙏) if there is any error in the code.&lt;/p&gt;
&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2wwsU7cc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://medium.com/_/stat%3Fevent%3Dpost.clientViewed%26referrerSource%3Dfull_rss%26postId%3D4842bfea04dc" width="1" height="1"&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Speed Matters: Designing for Mobile Performance - A summary</title>
      <dc:creator>Franco D'Alessio</dc:creator>
      <pubDate>Mon, 30 Mar 2020 15:38:31 +0000</pubDate>
      <link>https://forem.com/francodalessio/speed-matters-designing-for-mobile-performance-a-summary-1028</link>
      <guid>https://forem.com/francodalessio/speed-matters-designing-for-mobile-performance-a-summary-1028</guid>
      <description>&lt;p&gt;&lt;a href="https://www.awwwards.com/"&gt;&lt;strong&gt;Awwwards&lt;/strong&gt;&lt;/a&gt;, in collaboration with &lt;strong&gt;Google&lt;/strong&gt;, produced a great eBook called &lt;a href="https://www.awwwards.com/brainfood-mobile-performance-vol3.pdf"&gt;&lt;strong&gt;Speed Matters. Designing for Mobile Performance&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;.&lt;/strong&gt; It explains what mobile web users expect and how they behave, and then provides great insights on how to &lt;strong&gt;optimize your sites to provide a better experience&lt;/strong&gt;.&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--gOJYCGLM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/819/1%2AP8zxZGHwOeg9y9NaOBewmw.png"&gt;The cover page of the eBook Awwwards produced in collaboration with Google&lt;p&gt;This article is a summary of that eBook, which contains the things that I considered to be interesting and useful. Please &lt;strong&gt;read &lt;/strong&gt;&lt;a href="https://www.awwwards.com/brainfood-mobile-performance-vol3.pdf"&gt;&lt;strong&gt;the full eBook&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; for more details&lt;/strong&gt;. On the last pages you’ll also find an extensive list of articles, videos and tools for more information.&lt;/p&gt;
&lt;h3&gt;The need for speed&lt;/h3&gt;
&lt;blockquote&gt;&lt;em&gt;53% of mobile site visits are abandoned if the pages take longer than 3 seconds to load.&lt;/em&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;The need for speed&lt;/em&gt;&lt;/strong&gt; is a research carried out by Google which studies the relation between &lt;strong&gt;real and perceived performance&lt;/strong&gt; by users.&lt;/p&gt;
&lt;p&gt;The most important insight this study provides is this one:&lt;/p&gt;
&lt;blockquote&gt;
&lt;br&gt;
&lt;em&gt;Speed matters on the mobile web, but &lt;/em&gt;&lt;strong&gt;&lt;em&gt;perception&lt;/em&gt;&lt;/strong&gt;&lt;em&gt; of speed is just as important.&lt;/em&gt;&lt;br&gt;
&lt;/blockquote&gt;
&lt;h4&gt;How important is speed for users?&lt;/h4&gt;
&lt;p&gt;Speed was rated as &lt;strong&gt;the most important UX factor&lt;/strong&gt; for users when visiting a website: &lt;strong&gt;75% &lt;/strong&gt;of them mentioned it 🏆&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--fb2-Esch--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/437/1%2AbDJlbAOAAym20Quy8vYc9w.png"&gt;The list of UX factors considered most important by the users&lt;p&gt;In general, &lt;strong&gt;real downloading speed is quite fast&lt;/strong&gt;. 7 out of 10 sites loaded in less than 4 seconds, and around &lt;strong&gt;33% &lt;/strong&gt;loaded in less than 1 second.&lt;/p&gt;
&lt;blockquote&gt;Google’s &lt;a href="https://developers.google.com/speed/pagespeed/insights/"&gt;PageSpeed Insights&lt;/a&gt; is a great tool where you can test your website and get results for several speed metrics, both for desktop and mobile&lt;/blockquote&gt;
&lt;h4&gt;Mobile browsing in context&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Mobile web browsing mainly happens at home&lt;/strong&gt; (&lt;strong&gt;82%&lt;/strong&gt; of the total) by people that are relaxed and calmed 😮 This fact was very surprising to me. If someone had asked me, I would have answered the opposite.&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--S3BFff5i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/445/1%2AcYqEWYSj7-0hZBwXEKCI3A.png"&gt;Distribution of mobile web browsing by location&lt;p&gt;Which is not surprising is that in this context, &lt;strong&gt;page load speeds are perceived as being faster&lt;/strong&gt;. For people that are at work or traveling/elsewhere, load times are perceived as being slower.&lt;/p&gt;
&lt;p&gt;But even for fast loading sites, &lt;strong&gt;29% of people still don’t actually perceive them as fast 🤔&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;So what affects the perception of speed?&lt;/h4&gt;
&lt;p&gt;These are the 4 external factors that affect speed:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Effective use of the site 👆&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Web visits where actual speed was slow but felt to be fast tended to be retail sites. We know that &lt;strong&gt;long scroll sites are designed to populate the page “above the fold”&lt;/strong&gt;, giving the impression that the page is complete, even though loading is still ongoing “below the fold”.&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--MU5v0Gnp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/333/1%2AsENjldwHcot-UfQrWJ29nQ.gif"&gt;This GIF shows how Amazon loads content above the fold first&lt;p&gt;This can explain some of the distortion between the real and the perceived speed: &lt;strong&gt;the difference between the time after which a site can effectively be used &lt;em&gt;vs&lt;/em&gt;. the time it takes to fully load.&lt;/strong&gt; This is a prime example of design thought to optimize the perception of performance.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Age 👴&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Younger users are more demanding. &lt;strong&gt;Users between 18 and 24 years old only perceived 50% of the sites as fast loading sites&lt;/strong&gt;. This number goes up to &lt;strong&gt;73%&lt;/strong&gt; for users between 35 and 44 years old.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;State of mind 💆‍♂&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As previously said, &lt;strong&gt;when users are calm and relaxed, sites are perceived as being faster&lt;/strong&gt;. In this context around &lt;strong&gt;80%&lt;/strong&gt; of the sites were perceived as fast loading sites.&lt;/p&gt;
&lt;p&gt;When users are feeling rushed or anxious, the perceived speed slows down. In this context only around &lt;strong&gt;40%&lt;/strong&gt; of the sites were perceived as fast loading sites.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Place of activity 🏠&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;On the move things feel slower&lt;/strong&gt;. For people sat down, &lt;strong&gt;75%&lt;/strong&gt; of the sites were perceived as fast. But for people on the move, only &lt;strong&gt;52%&lt;/strong&gt; of them. When we’re not at home and need the information as soon as possible, things feel slower.&lt;/p&gt;
&lt;h4&gt;Outcomes of speed&lt;/h4&gt;
&lt;p&gt;When perceptions of speed were thought to be fast, a higher percentage of users reported that:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;They could &lt;strong&gt;achieve their goals&lt;/strong&gt; (&lt;strong&gt;92%&lt;/strong&gt; vs &lt;strong&gt;73%&lt;/strong&gt; for slow perceived sites)&lt;/li&gt;
&lt;li&gt;They were more likely to &lt;strong&gt;return to the website again&lt;/strong&gt; (&lt;strong&gt;95%&lt;/strong&gt; vs &lt;strong&gt;62%&lt;/strong&gt; for slow perceived sites)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Speed also significantly impacted &lt;a href="https://www.netpromoter.com/know/"&gt;&lt;strong&gt;brand NPS (Net Promoter Score)&lt;/strong&gt;&lt;/a&gt;: an average of &lt;strong&gt;92&lt;/strong&gt; points vs &lt;strong&gt;44&lt;/strong&gt; for slow perceived sites.&lt;/p&gt;
&lt;h3&gt;Improve the perception of speed 🏃&lt;/h3&gt;
&lt;h4&gt;Understanding interface response time&lt;/h4&gt;
&lt;p&gt;Not only the waiting time during loading is important, but also &lt;strong&gt;the time that the interface takes to respond to each interaction of the user&lt;/strong&gt;. If the user has to wait &lt;strong&gt;8&lt;/strong&gt; &lt;strong&gt;seconds&lt;/strong&gt; for anything, he/she will most probably &lt;strong&gt;abandon the task 😰&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Users have less patience when they first enter a site than when they make successive interactions within the same site. Some users will tolerate waiting up to &lt;strong&gt;5 seconds&lt;/strong&gt; for tasks that are part of the &lt;strong&gt;user flow&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;First Meaningful Paint and Time to interactive&lt;/h4&gt;
&lt;p&gt;· &lt;strong&gt;First Meaningful Paint&lt;/strong&gt; is the time required to show something useful in the screen to engage the user.&lt;/p&gt;
&lt;p&gt;· &lt;strong&gt;Time to interactive&lt;/strong&gt; is the time required by the browser to finishing building the DOM so the user can begin to interact with the application.&lt;/p&gt;
&lt;p&gt;As we already discussed, most retail sites use a very good trick that addresses these two metrics: they produce a &lt;strong&gt;quick FMP&lt;/strong&gt; and prioritize the load of &lt;strong&gt;necessary scripts&lt;/strong&gt; so that the content becomes interactive.&lt;/p&gt;
&lt;h4&gt;Put the user in active mode&lt;/h4&gt;
&lt;blockquote&gt;SOME years ago, executives at a Houston airport faced a troubling customer-relations issue. Passengers were lodging an inordinate number of complaints about the long waits at baggage claim. In response, the executives increased the number of baggage handlers working that shift. The plan worked: the average wait fell to eight minutes, well within industry benchmarks. But the complaints persisted.&lt;/blockquote&gt;
&lt;blockquote&gt;Puzzled, the airport executives undertook a more careful, on-site analysis. They found that it took passengers a minute to walk from their arrival gates to baggage claim and seven more minutes to get their bags. Roughly 88 percent of their time, in other words, was spent standing around waiting for their bags.&lt;/blockquote&gt;
&lt;blockquote&gt;So the airport decided on a new approach: instead of reducing wait times, it moved the arrival gates away from the main terminal and routed bags to the outermost carousel. Passengers now had to walk six times longer to get their bags. Complaints dropped to near zero.&lt;/blockquote&gt;
&lt;p&gt;&lt;em&gt;You can see the full story &lt;/em&gt;&lt;a href="https://www.nytimes.com/2012/08/19/opinion/sunday/why-waiting-in-line-is-torture.html"&gt;&lt;em&gt;here&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;There are two forms of waiting: passive and active. In &lt;strong&gt;passive waiting&lt;/strong&gt;, the user simply watches something loading, &lt;strong&gt;without doing anything&lt;/strong&gt;. When this happens, &lt;strong&gt;people overestimate waiting times by 36%&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;You want to get people to active mode. Just like in the Houston airport story, you have to &lt;strong&gt;occupy users to hack their perception&lt;/strong&gt;. If you make them wait while interacting with a game or answering a little questionnaire, the same amount of waiting time &lt;strong&gt;will feel shorter.&lt;/strong&gt;&lt;/p&gt;
&lt;h4&gt;Create instant interactions&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Instant feedback &lt;/strong&gt;from the interface make the experience feel &lt;strong&gt;fluid&lt;/strong&gt; and generate the illusion of &lt;strong&gt;continuity&lt;/strong&gt;. They entertain the user while the “real” action is pushed into the &lt;strong&gt;background&lt;/strong&gt; on a second thread.&lt;/p&gt;
&lt;blockquote&gt;Check these &lt;a href="https://www.awwwards.com/awwwards/collections/animation/"&gt;&lt;strong&gt;UI animations and microinteractions&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt; &lt;/strong&gt;for some prime examples.&lt;/blockquote&gt;
&lt;h4&gt;Use the right loader ⏳&lt;/h4&gt;
&lt;p&gt;Use &lt;strong&gt;spinners&lt;/strong&gt; for short waiting times, and &lt;strong&gt;progress bars&lt;/strong&gt; for longer ones.&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--WtHgjFNR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/572/1%2AE1EEhKfWt2ZM2I8UEfKZTw.png"&gt;&lt;p&gt;It can also be more informative to &lt;strong&gt;show the length of the remaining waiting&lt;/strong&gt; &lt;strong&gt;time&lt;/strong&gt; than the percentage of data still to be loaded.&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--xBuiNkaE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/682/1%2A2n7OvJAOaIQy7MOv6wLVjQ.png"&gt;Alitalia does this when you search for a flight&lt;h4&gt;UX Patterns&lt;/h4&gt;
&lt;p&gt;You can also use the following UX patterns to &lt;strong&gt;improve speed perception&lt;/strong&gt; and &lt;strong&gt;minimize time in passive wait&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Preemptive start&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;It is the realization of the beginning of a task &lt;strong&gt;before the user demands it&lt;/strong&gt;. For example, by showing a questionnaire you distract the user while other assets are loaded.&lt;/p&gt;
&lt;p&gt;Other examples are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Loading assets on login screen&lt;/li&gt;
&lt;li&gt;Preload assets on rollover&lt;/li&gt;
&lt;li&gt;Preload content on previous states (preload the content of the next screen)&lt;/li&gt;
&lt;li&gt;Preload assets while the user is on a multi-step forms&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Early completion 🕐&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;In this case we partially &lt;strong&gt;show content that is not yet complete&lt;/strong&gt;. The most common example is video streaming.&lt;/p&gt;
&lt;p&gt;🖼 Other techniques regarding images include:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Progressive images&lt;/strong&gt;. Images are loaded in successive states. The user can see a low resolution representation of the image that continues downloading.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Placeholders&lt;/strong&gt; &lt;strong&gt;or low-res blurred images&lt;/strong&gt; like used here in Medium.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Optimistic UI&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Optimistic design, which can sometimes be dangerous, is a model where &lt;strong&gt;an incomplete task is presented as complete&lt;/strong&gt;. It is based on the fact that &lt;strong&gt;only&lt;/strong&gt; &lt;strong&gt;between 1% and 3% of client requests get a server error 🎉 &lt;/strong&gt;It helps to create a smooth and interrupted experience for the user.&lt;/p&gt;
&lt;blockquote&gt;Only between 1% and 3% of client requests get a server error&lt;/blockquote&gt;
&lt;p&gt;A great example of this are &lt;strong&gt;Instagram likes&lt;/strong&gt;. When you like a post, the action &lt;strong&gt;appears to be immediate&lt;/strong&gt;, but in reality it is stored in the database at a later stage.&lt;/p&gt;
&lt;h4&gt;Smooth and optimized animations&lt;/h4&gt;
&lt;p&gt;To get a smooth motion, animations must maintain &lt;strong&gt;60fps&lt;/strong&gt; and each frame needs to be &lt;strong&gt;rendered in less than 16ms&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;Easing functions&lt;/h4&gt;
&lt;p&gt;Choosing the right “easing” is decisive to produce a smooth animation. Easing functions are &lt;strong&gt;curves that describe the acceleration or deceleration&lt;/strong&gt; of a motion in a period of time.&lt;/p&gt;
&lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--YOh1bkr5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/752/1%2AgHhGE7_MukeOBVodJCSw6g.png"&gt;The different ease functions curves&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;Ease-out&lt;/strong&gt; for &lt;strong&gt;UI elements&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Avoid &lt;strong&gt;Ease-in and Ease-in-out&lt;/strong&gt; with long times. They are perceived as too slow for the users. Use them only for &lt;strong&gt;short times&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;Ease-out and Ease-in&lt;/strong&gt; use a duration of between &lt;strong&gt;200–500ms&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;Bounce and Elastic effects&lt;/strong&gt; use a duration of &lt;strong&gt;800–1200ms&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Animation optimization&lt;/h4&gt;
&lt;p&gt;&lt;strong&gt;Animate the correct properties 🎯&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;When using &lt;strong&gt;CSS transitions or animations&lt;/strong&gt;, try to &lt;strong&gt;only use opacity and transform&lt;/strong&gt; properties. All other properties are very slow to animate, especially on mobile devices.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;CSS or JS animations?&lt;/strong&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;CSS&lt;/strong&gt; &lt;strong&gt;for&lt;/strong&gt; &lt;strong&gt;simple&lt;/strong&gt; &lt;strong&gt;animations&lt;/strong&gt;, transitions and to animate DOM elements.&lt;/li&gt;
&lt;li&gt;Use &lt;strong&gt;JS for complex animations&lt;/strong&gt;, to animate sequences, independent transformations and path animations.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Designing for mobile performance&lt;/h3&gt;
&lt;h4&gt;Content strategy ✍️&lt;/h4&gt;
&lt;p&gt;Optimization isn’t just about compressing assets, optimizing server architecture or cache. It should begin in the &lt;strong&gt;analysis part of the project&lt;/strong&gt;, where you understand &lt;strong&gt;the user path and the way the users will interact&lt;/strong&gt; with the site. This way you can establish viable goals and design the content based on your &lt;strong&gt;target audience behavior and needs&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;A very useful tip is to always create a &lt;strong&gt;Performance Budget&lt;/strong&gt; to avoid an excess of unnecessary resources.&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;The performance budget 💰&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Establish a series of &lt;strong&gt;limitations&lt;/strong&gt; based on the analysis of our target, their devices and possible access conditions to the site.&lt;/p&gt;
&lt;p&gt;The performance budget should include the following data:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Page weight&lt;/strong&gt; in kilobytes, including all assets&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Percentage&lt;/strong&gt; assigned to each &lt;strong&gt;type&lt;/strong&gt; of asset (images, CSS, JS, etc.)&lt;/li&gt;
&lt;li&gt;Number of &lt;strong&gt;HTTP requests&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Max time&lt;/strong&gt; to load over &lt;strong&gt;3G connections&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;Image strategy 🖼&lt;/h4&gt;
&lt;p&gt;A very high percentage (around &lt;strong&gt;70%&lt;/strong&gt;) of a site is &lt;strong&gt;composed of images&lt;/strong&gt;. You really need to &lt;strong&gt;understand which graphic material is actually useful &lt;/strong&gt;or significant for your brand.&lt;/p&gt;
&lt;p&gt;Separate your images in these 4 categories:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Navigation and action images&lt;/li&gt;
&lt;li&gt;Branding and priority images&lt;/li&gt;
&lt;li&gt;Decorative images&lt;/li&gt;
&lt;li&gt;Informative images&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The first two types are very important and are the images that &lt;strong&gt;need to be always present&lt;/strong&gt;. You need to work on &lt;strong&gt;decorative and informative images&lt;/strong&gt;. While they’re nice to have, they are &lt;strong&gt;not critical&lt;/strong&gt;, so try to&lt;strong&gt; replace them whenever possible&lt;/strong&gt; with alternative designs (color blocks, text, etc.).&lt;/p&gt;
&lt;h4&gt;&lt;strong&gt;Deliver the minimal code needed&lt;/strong&gt;&lt;/h4&gt;
&lt;p&gt;Most websites ship all the JavaScript a website needs upfront. This is bad, because all that also needs to be parsed, compiled and executed by the browser. This is especially important when working with mobile devices.&lt;/p&gt;
&lt;p&gt;You want to deliver the &lt;strong&gt;minimal code to make a page useful upfront&lt;/strong&gt;. Consider &lt;strong&gt;splitting your JS&lt;/strong&gt; and only serve down the minimal code to make the current page useful for the user. &lt;strong&gt;Non-critical code&lt;/strong&gt; should be &lt;strong&gt;lazily loaded &lt;/strong&gt;in or &lt;strong&gt;added on the pages that absolutely need it&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;Preload critical resources ❗️&lt;/h4&gt;
&lt;p&gt;Browsers like Chrome load resources like JS, fonts and images with &lt;strong&gt;different priorities&lt;/strong&gt;. They try to guess what the user’ll need first.&lt;/p&gt;
&lt;p&gt;But nobody knows more about your assets than you, the author of the site. You need to know which of them are &lt;strong&gt;critical to load early&lt;/strong&gt; on each page, and design/code them according to that.&lt;/p&gt;
&lt;h4&gt;Inline critical CSS&lt;/h4&gt;
&lt;p&gt;Try to put the CSS that you expect the current page to use (critical CSS) inside a&lt;strong&gt; style tag in the header section&lt;/strong&gt;. This allows the browser to paint the page without any network call and therefore it’s &lt;strong&gt;super quick&lt;/strong&gt;.&lt;/p&gt;
&lt;h4&gt;Eliminate render-blocking resources in above-the-fold content&lt;/h4&gt;
&lt;p&gt;In web development, the term “above the fold” refers to the portion of a webpage that is &lt;strong&gt;visible without scrolling&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Loading contents that are not directly involved in the rendering of this prioritized visible content causes a delay that directly &lt;strong&gt;impacts conversion rates and user’s speed perception of the website&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;So… What do you think? 😃&lt;/h3&gt;
&lt;p&gt;I found these insights and tips really interesting and useful. Some of the data was even surprising to me. What are your thoughts? Please leave a comment and let me know!&lt;/p&gt;
&lt;p&gt;Thanks for reading ❤️&lt;/p&gt;
&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zIgm6SWm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://medium.com/_/stat%3Fevent%3Dpost.clientViewed%26referrerSource%3Dfull_rss%26postId%3Ded5d04c3b208" width="1" height="1"&gt;

</description>
      <category>webdev</category>
      <category>webperf</category>
    </item>
  </channel>
</rss>
