<?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>DEV Community: Christine Schorn (she/her)</title>
    <description>The latest articles on DEV Community by Christine Schorn (she/her) (@enitschorn).</description>
    <link>https://dev.to/enitschorn</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%2F256750%2F437dafb9-673e-49a1-bfe8-99e48599ff0d.jpeg</url>
      <title>DEV Community: Christine Schorn (she/her)</title>
      <link>https://dev.to/enitschorn</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/enitschorn"/>
    <language>en</language>
    <item>
      <title>3 takeaways from Dan Abramov's Just JavaScript course</title>
      <dc:creator>Christine Schorn (she/her)</dc:creator>
      <pubDate>Mon, 06 Dec 2021 10:41:32 +0000</pubDate>
      <link>https://dev.to/enitschorn/3-takeaways-from-dan-abramovs-just-javascript-course-229d</link>
      <guid>https://dev.to/enitschorn/3-takeaways-from-dan-abramovs-just-javascript-course-229d</guid>
      <description>&lt;p&gt;A while ago, I came across &lt;em&gt;Just JavaScript&lt;/em&gt;. I was intrigued by the banner image and its title: &lt;em&gt;Explore the JavaScript Universe&lt;/em&gt; but a little overwhelmed by the amount of text that followed if I'm being honest.&lt;br&gt;
Nevertheless, I started reading and kept reading and reading. I was hooked, but hesitant whether I should pay for &lt;em&gt;another&lt;/em&gt; course that could potentially have me end up in tutorial hell 📚 &lt;/p&gt;

&lt;p&gt;So I decided against taking that journey through the promising JS universe for now and went on with my life 👩‍💻&lt;/p&gt;

&lt;p&gt;A few weeks later, however, a colleague mentioned the course and spoke very highly of it. I took it as a sign from the universe (pun intended) and signed up 🔥&lt;/p&gt;

&lt;p&gt;While I won't go through the content of the course, you can read up on that &lt;a href="https://justjavascript.com/"&gt;here&lt;/a&gt;, I want to share what I learned from it and why I think it's well worth the money 💰&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Mental models
&lt;/h2&gt;

&lt;p&gt;Before starting out, I wasn't even aware of how wrong my mental models around JavaScript were. Dan Abramov and Maggie Appleton who did all the illustrations for the course did an amazing job at visualising and bringing to life a wonderful JavaScript universe as I had never seen or imagined before. It helped me immensely to correct my mental models and gain a deeper and better understanding of JavaScript basics.&lt;/p&gt;

&lt;p&gt;So even though a lot of the content wasn't completely new to me, I benefited so much from it and came out at the other end with a much clearer vision.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Knowledge check
&lt;/h2&gt;

&lt;p&gt;I don't know about you, but I have watched and read endless tutorials where I came out at the other end not being entirely sure if I really understood the content and it sometimes left me feeling more clueless and insecure than before.&lt;/p&gt;

&lt;p&gt;With &lt;em&gt;Just JavaScript&lt;/em&gt; the opposite was the case. I finished this course feeling like I really understood the concepts, and the 10 quizzes like exercises after each module played a huge role in that.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Text over video
&lt;/h2&gt;

&lt;p&gt;&lt;em&gt;Just JavaScript&lt;/em&gt; was the first course I took that was completely text-based and I was surprised how much I liked it if I'm being honest 💡 The length of the modules was perfect,  and the text was broken up with code snippets and  illustrations which made it much easier to digest 🍔&lt;/p&gt;

&lt;p&gt;I always knew that I'd prefer reading over watching or listening, but this course made it even clearer to me that there's just something to seeing a concept black on white explained and being able to go over it again and again without having to press a rewind button 🎉&lt;/p&gt;

&lt;p&gt;I'm so happy I took this course and invested in myself. I learned new things and corrected old mental models, I learned to think about JavaScript in a way I had never done before and so many concepts finally started to click 💥&lt;/p&gt;

&lt;p&gt;And if you're still wondering if this course is for you, let me say this: If you have read this far I'd say it is 😉&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Cover photo by &lt;a href="https://unsplash.com/@gferla?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Guillermo Ferla&lt;/a&gt; on &lt;a href="https://unsplash.com/s/photos/universe?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>React's useEffect &amp; useState hooks</title>
      <dc:creator>Christine Schorn (she/her)</dc:creator>
      <pubDate>Thu, 06 May 2021 08:05:40 +0000</pubDate>
      <link>https://dev.to/enitschorn/react-s-useeffect-usestate-hooks-55fe</link>
      <guid>https://dev.to/enitschorn/react-s-useeffect-usestate-hooks-55fe</guid>
      <description>&lt;p&gt;Disclaimer: This is not a basic introduction to hooks. There are many great tutorials out there that cover that part, like the &lt;a href="https://reactjs.org/docs/hooks-reference.html" rel="noopener noreferrer"&gt;React docs&lt;/a&gt; themselves.&lt;/p&gt;




&lt;p&gt;As part of the Junior Engineering Program at 99designs, I did a little deep dive into the &lt;code&gt;useEffect&lt;/code&gt; and &lt;code&gt;useState&lt;/code&gt; hooks of React. &lt;/p&gt;

&lt;p&gt;It was quite interesting for me to learn on a topic that I was relatively familiar with (I've used both those hooks heaps of times) and to see how much there still was that I didn't quite properly understand yet.&lt;/p&gt;

&lt;p&gt;My starting point was the &lt;code&gt;why&lt;/code&gt; of things. I looked through the original motivation behind introducing hooks, &lt;code&gt;useEffect&lt;/code&gt; and &lt;code&gt;useState&lt;/code&gt; in general. You can find this doc &lt;a href="https://reactjs.org/docs/hooks-intro.html#motivation" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Why useEffect?
&lt;/h3&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%2Fi.imgur.com%2FKajbief.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%2Fi.imgur.com%2FKajbief.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The main motivation behind introducing the &lt;code&gt;useEffect&lt;/code&gt; hook was to make complex components easier to manage and read. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hooks let you split one component into smaller functions based on what pieces are related.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Before the &lt;code&gt;useEffect&lt;/code&gt; hook and &lt;code&gt;function components&lt;/code&gt; were introduced, the standard way of managing &lt;code&gt;side effects&lt;/code&gt; inside &lt;code&gt;class components&lt;/code&gt; were lifecycle methods.&lt;/p&gt;

&lt;p&gt;However, they presented you with a particular problem where you had to split your code logic based on &lt;strong&gt;when&lt;/strong&gt; something was happening, not &lt;strong&gt;what&lt;/strong&gt; was happening. As a result, your code was hard to read and difficult to test as well.&lt;/p&gt;

&lt;p&gt;Here you can see a very conceptual example of this problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt; &lt;span class="nf"&gt;componentDidMount&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// do x immediately after component has mounted &lt;/span&gt;
    &lt;span class="c1"&gt;// also do y immediately after component mounted&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nf"&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;// only do y when component has updated (but not on initial render)&lt;/span&gt;
 &lt;span class="p"&gt;}&lt;/span&gt;
 &lt;span class="nf"&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;// cleanup x immediately before component has unmounted&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 see that our code is all over the place. &lt;code&gt;componentDidMount&lt;/code&gt; contains logic related to functionality &lt;code&gt;x&lt;/code&gt; &lt;strong&gt;AND&lt;/strong&gt; &lt;code&gt;y&lt;/code&gt;, while &lt;code&gt;componentDidUpdate&lt;/code&gt; just contains logic related to functionality &lt;code&gt;y&lt;/code&gt;, and &lt;code&gt;componentWillUnmount&lt;/code&gt; on the other hand again contains logic only related to &lt;code&gt;x&lt;/code&gt;. This makes code hard to read and test as I mentioned earlier.&lt;/p&gt;

&lt;p&gt;So in comes our &lt;code&gt;useEffect&lt;/code&gt; hook which helps us solve this issue with a much cleaner approach that allows us to split our logic based on the &lt;strong&gt;what&lt;/strong&gt; of things, not the &lt;strong&gt;when&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By default, &lt;code&gt;useEffect&lt;/code&gt; runs after the first render and after every update as well, so basically after every render, to put it simpler.&lt;/p&gt;

&lt;p&gt;Let's return to our conceptual example from before and see how &lt;code&gt;useEffect&lt;/code&gt; is solving our previously described problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&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 x immediately after component has mounted&lt;/span&gt;
   &lt;span class="c1"&gt;// cleanup x immediately before component has unmounted&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&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;// only do y when component has updated (but not on initial render)&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 see how we are now able to group based on the different things that are happening and &lt;code&gt;x&lt;/code&gt; and &lt;code&gt;y&lt;/code&gt; are no longer mingled and mixed up. &lt;/p&gt;

&lt;p&gt;The result: easier to read and much easier to test as well.&lt;/p&gt;

&lt;p&gt;At this point, it is also worth noting that React strongly encourages you to use several effects in your component if you have a lot of different things happening. So don't worry if you end up with 3 different &lt;code&gt;useEffect&lt;/code&gt; inside your component, that's actually considered good practice.&lt;/p&gt;



&lt;h3&gt;
  
  
  The dependency array of useEffect
&lt;/h3&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%2Fi.imgur.com%2FmNEya74.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%2Fi.imgur.com%2FmNEya74.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we've seen the first argument that our &lt;code&gt;useEffect&lt;/code&gt; hook takes, a function where you'll outline all the magical things you want to happen. But the &lt;code&gt;useEffect&lt;/code&gt; hook also takes in a second argument, often called &lt;code&gt;dependency array&lt;/code&gt;, which is extremely important, and for me, this deep dive really helped me better understand how this second argument works, why it's so important, and what are some gotchas.&lt;/p&gt;

&lt;p&gt;React introduced the dependency array to improve performance. The way it works is relatively straightforward if you're working with primitive values such as &lt;code&gt;booleans&lt;/code&gt;, &lt;code&gt;numbers&lt;/code&gt;, or &lt;code&gt;strings&lt;/code&gt;. There are three scenarios that you can create:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Not passing the dependency array - not really recommended&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you don't pass a second argument (even if it's empty) your effect will re-run on every re-render, which isn't great for performance&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&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;// no dependency array - runs on every re-render&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;2. Passing an empty dependency array&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you just pass an empty array as a second argument, you're basically telling React that your effect has NO dependencies and it'll never re-run&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&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;// empty dependency array - effect has NO dependencies and never re-runs&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;3. Passing values to your dependency array - probably the most used use-case&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The rule of thumb is that if you are using any props or state variables in your effect, you should pass them again to your dependency array.&lt;br&gt;
This way React can keep track of when one of these values has updated and consequently will re-run your effect on re-render.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nf"&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;// dependency array with values - if one of the values has changed, &lt;/span&gt;
    &lt;span class="c1"&gt;// effect will re-run&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;value2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As I mentioned earlier, this works pretty well when you're dealing with primitive values. With more complex values like objects, arrays, and functions, however, you need to pay a bit more attention to detail and might come across some use cases that need a bit of extra work.&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%2Fi.imgur.com%2F1V1eVyN.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%2Fi.imgur.com%2F1V1eVyN.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The reason why complex values don't work the same way as primitive values lies in the way React, or rather JavaScript handles those values. Under the hood, React uses the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is#description" rel="noopener noreferrer"&gt;Object.is method&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So what does that mean exactly?&lt;/p&gt;

&lt;p&gt;When you have an object, array, or function in your component (whether that's a state variable or props) React stores a reference to that object in memory (like an address where that object lives in memory).&lt;/p&gt;

&lt;p&gt;The problem is that you don't have any guarantees that on the next re-render the reference to your object will be the same, in fact, it's pretty likely that it won't be.&lt;/p&gt;

&lt;p&gt;As a consequence, when React compares the value you have passed to the dependency array in your &lt;code&gt;useEffect&lt;/code&gt;, to the original one, they won't be the same because their "address" in memory has changed on the re-render and thus, even if your value hasn't been updated, your effect will re-run again and again because the two values reference a different object in memory (even though to you they look the same).&lt;/p&gt;

&lt;p&gt;Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Team&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;team&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;players&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPlayers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

  &lt;span class="nf"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;getPlayers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setPlayers&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;team&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;Players&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&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;So let's say you have an object that you pass to your component as props. Here we have a &lt;code&gt;Team&lt;/code&gt; component that takes in a &lt;code&gt;team&lt;/code&gt; object that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;team&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="mi"&gt;1&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;Bulldogs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;active&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On every re-render, the reference to your team object will most likely be different. &lt;/p&gt;

&lt;p&gt;So when you pass it to your dependency array and React checks whether this object has changed or not and whether to run the effect again or not, the comparison will return &lt;code&gt;false&lt;/code&gt; causing your effect to re-run on every re-render. &lt;/p&gt;

&lt;p&gt;So what can you do to avoid this? There are several possible approaches and I'm just listing a few of them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Only pass what you really need and use in your &lt;code&gt;useEffect&lt;/code&gt; hook:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's have a look at our Team component again:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Team&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;team&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;players&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPlayers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

  &lt;span class="nf"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;getPlayers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setPlayers&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;team&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;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&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;Players&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&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;Inside our effect, we're really just using properties from our team object, namely &lt;code&gt;team.active&lt;/code&gt; and &lt;code&gt;team.id&lt;/code&gt; which are primitive values again.&lt;/p&gt;

&lt;p&gt;As a result, we can just pass those exact values to our dependency array and thus avoid all the references/address comparison complications mentioned above. Now our effect will only re-run if &lt;code&gt;team.id&lt;/code&gt; or &lt;code&gt;team.active&lt;/code&gt; have changed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Recreate the object to use inside of our effect:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's have a look at another example and assume that for some reason we need the whole &lt;code&gt;team&lt;/code&gt; object in our &lt;code&gt;useEffect&lt;/code&gt; and also in our component.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Team&lt;/span&gt; &lt;span class="o"&gt;=&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;active&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;players&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPlayers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

  &lt;span class="nf"&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt; &lt;span class="o"&gt;=&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;active&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;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;getPlayers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setPlayers&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;id&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="nx"&gt;active&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;team&lt;/span&gt; &lt;span class="o"&gt;=&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;active&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;Players&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&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 can just recreate the object twice, once inside of our &lt;code&gt;useEffect&lt;/code&gt; hook and once in our component. It's not very expensive to do that, so you don't have to worry about performance issues when using this approach. It's actually not a bad practice to move everything you need into your effect where possible since this way you clearly know what you're using and depending on.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Memoisation - last resort:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As a very last resort, if you have some very expensive calculations that you want to avoid re-running on every re-render, you can use React's &lt;code&gt;useMemo&lt;/code&gt; hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Team&lt;/span&gt; &lt;span class="o"&gt;=&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;active&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;team&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useMemo&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;createTeam&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;active&lt;/span&gt; &lt;span class="p"&gt;}),&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;active&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;players&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPlayers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

  &lt;span class="nf"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;getPlayers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setPlayers&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;team&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;Players&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&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;Be aware though that using this hook itself is quite expensive, so you should think twice before using it. You can learn more about the &lt;code&gt;useMemo&lt;/code&gt; hook &lt;a href="https://reactjs.org/docs/hooks-reference.html#usememo" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;



&lt;h3&gt;
  
  
  Cleaning your effect up
&lt;/h3&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%2Fuploads%2Farticles%2Fo08gjdtmp84c61obv1bh.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%2Fuploads%2Farticles%2Fo08gjdtmp84c61obv1bh.gif" alt="giphy (11)"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Especially when you run timers, events, or subscriptions inside your effect, it can be useful to clean those up before the next effect and when the component unmounts to avoid memory leaks.&lt;/p&gt;

&lt;p&gt;The way to go about this is to return a function from your effect that will act as a cleanup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Team&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;team&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;players&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setPlayers&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt;

  &lt;span class="nf"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;getPlayers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&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="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;setPlayers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;subscribePlayers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;players&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;gt;&lt;/span&gt; &lt;span class="nf"&gt;unsubscribePlayers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="c1"&gt;// 'cleans up' our subscription&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;active&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;team&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="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Players&lt;/span&gt; &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;players&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;h3&gt;
  
  
  Why useState?
&lt;/h3&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%2Fuploads%2Farticles%2Fzmnb8zn96fgolyc323y0.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%2Fuploads%2Farticles%2Fzmnb8zn96fgolyc323y0.gif" alt="giphy (12)"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In a very simple way, &lt;code&gt;useState&lt;/code&gt; lets you add React state to function components (like &lt;code&gt;setState&lt;/code&gt; for class components).&lt;/p&gt;

&lt;p&gt;A little tip when using this hook: split state into multiple state variables based on which values tend to change together (especially helpful when dealing with objects or arrays) and use it for simple state management. &lt;/p&gt;

&lt;p&gt;If things get more complex in the way you manage state, there are other tools for that.&lt;/p&gt;

&lt;p&gt;While I didn't find &lt;code&gt;useState&lt;/code&gt; as complex as &lt;code&gt;useEffect&lt;/code&gt;, there are some important characteristics to keep in mind when working with it:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Updating a state variable with the &lt;code&gt;useState&lt;/code&gt; hook always &lt;strong&gt;replaces&lt;/strong&gt; that variable instead of merging it (like setState does).&lt;/strong&gt;&lt;br&gt;
This is quite important when you're dealing with objects or arrays, for example.&lt;/p&gt;

&lt;p&gt;If you're just updating one item in an array or one property value of an object, you will always have to spread in the original object or array to not overwrite it with just the part that you're updating.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTeam&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nf"&gt;setTeam&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="na"&gt;active&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. It's asynchronous.&lt;/strong&gt;&lt;br&gt;
Quite important to keep in mind that when you call your function that sets state (&lt;code&gt;setTeam&lt;/code&gt;, for example) it behaves asynchronously, so it just adds your value update request to a queue and you might not see the result immediately.&lt;/p&gt;

&lt;p&gt;That's where the &lt;code&gt;useEffect&lt;/code&gt; hook comes in very handy and lets you access your updated state variable immediately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. You can update state with a callback.&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;useState&lt;/code&gt; hook gives you access to a so-called functional update form that allows you to access your previous state and use it to update your new state.&lt;/p&gt;

&lt;p&gt;This is handy when your new state is calculated using the previous state, so for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&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="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;prevState&lt;/span&gt; &lt;span class="o"&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;&lt;strong&gt;4. Only call &lt;code&gt;useState&lt;/code&gt; at the top level.&lt;/strong&gt;&lt;br&gt;
You cannot call it in loops, conditions, nested functions, etc. When you have multiple &lt;code&gt;useState&lt;/code&gt; calls, the order in which they are invoked needs to be the same between renderings.&lt;/p&gt;

&lt;p&gt;There's so much more to hooks than what I've written down here, but those were the things that I think will help me most moving forward.&lt;/p&gt;

&lt;p&gt;I've really enjoyed diving deeper into this topic and realised again just how powerful hooks are. I also feel way more confident using them now and hope that after reading this article you do too.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>How to set up Cypress code coverage with TypeScript and ts-loader</title>
      <dc:creator>Christine Schorn (she/her)</dc:creator>
      <pubDate>Wed, 26 Aug 2020 06:44:57 +0000</pubDate>
      <link>https://dev.to/enitschorn/how-to-set-up-cypress-code-coverage-with-typescript-and-ts-loader-2ema</link>
      <guid>https://dev.to/enitschorn/how-to-set-up-cypress-code-coverage-with-typescript-and-ts-loader-2ema</guid>
      <description>

&lt;p&gt;This post first appeared on the &lt;a href="https://www.ie.com.au/blog/ldylrbvpjyyb8qo20mwm0981y4k4sp"&gt;IE Digital Blog&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this short tutorial, I want to show you how you can set up Cypress code coverage using the &lt;a href="https://docs.cypress.io/guides/tooling/code-coverage.html#Introduction"&gt;@cypress/code-coverage plugin&lt;/a&gt; in a TypeScript project. &lt;/p&gt;

&lt;p&gt;Note: If you are using babel-loader in your project, this tutorial might not be necessary for you. Instead, I recommend you to follow &lt;a href="https://github.com/lluia/cypress-typescript-coverage-example"&gt;this tutorial&lt;/a&gt;. However, if your project is using the ts-loader, stick with me. &lt;/p&gt;

&lt;p&gt;To begin with, let's assume that you already have a project set up with webpack and TypeScript and that you are using Cypress as your front end testing tool. So you are now at a point where you want to generate a nicely formatted code coverage report.&lt;/p&gt;

&lt;p&gt;As it turns out, at the point of writing this, it's a bit trickier to integrate Cypress code-coverage if you are using the ts-loader, so just following the @cypress/code-coverage plugin setup won't work. Luckily though, with some more steps, we can get it working for our project as well. So let's start.&lt;/p&gt;

&lt;h4&gt;
  
  
  Installing babel dependencies
&lt;/h4&gt;

&lt;p&gt;First, we have to install some dependencies that we'll need to get things rolling. Since Cypress code-coverage doesn't just work with the ts-loader (as mentioned), we have to add the babel-loader and its necessary dependencies to our project.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn add -D @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript babel-loader&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up webpack
&lt;/h4&gt;

&lt;p&gt;Since webpack is managing &lt;a href="https://webpack.js.org/loaders/"&gt;our loaders&lt;/a&gt; and we're only using the ts-loader at the moment, we'll have to update our &lt;strong&gt;webpack.config.js&lt;/strong&gt; file and add the babel-loader to our rules as well.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module: {
    rules: [
      {
        test: /\.tsx?$/,
        use: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /\.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/,
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.tsx', '.scss', '.js'],
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You might be wondering why we're using two transpilers now. In my case, the project was already set up using the ts-loader, so I choose to just add the babel-loader instead of changing the whole project to only use the babel-loader.&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up babel
&lt;/h4&gt;

&lt;p&gt;Setting up babel is pretty simple. Babel will help us transpile our TypeScript to the right Javascript standard. So let's create our &lt;strong&gt;.babelrc&lt;/strong&gt; file if you don't have one already and add the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "presets": [
    "@babel/react",
    "@babel/typescript",
    [
      "@babel/env",
      {
        "modules": false,
        "targets": {
          "chrome": "58",
          "ie": "11"
        }
      }
    ]
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All we do here, for now, is to tell Babel to use it's React and TypeScript presets and to transpile to a minimum version of IE 11 and Chrome 58.&lt;/p&gt;

&lt;h4&gt;
  
  
  Installing Cypress dependencies
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;add -D @istanbuljs/nyc-config-typescript source-map-support ts-node&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Proceed to your &lt;strong&gt;package.json&lt;/strong&gt; file and add the following lines&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"nyc": {
    "extends": "@istanbuljs/nyc-config-typescript",
    "all": true,
    "report-dir": "cypress-coverage"
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you want to save your reports in another folder, you can just change &lt;em&gt;cypress-coverage&lt;/em&gt; to the name of your target folder.&lt;/p&gt;

&lt;p&gt;I know this is a lot but hang in there. As our next step, let's install the Cypress code coverage plugin.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn add -D @cypress/code-coverage nyc istanbul-lib-coverage&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Update your &lt;strong&gt;.babelrc&lt;/strong&gt; file, so it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "plugins": [
    "istanbul"
  ],
  "presets": [
    "@babel/react",
    "@babel/typescript",
    [
      "@babel/env",
      {
        "modules": false,
        "targets": {
          "chrome": "58",
          "ie": "11"
        }
      }
    ]
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because the @cypress/code-coverage plugin DOES NOT instrument your code, we have to instrument it ourselves. We are using the Istanbul.js tool for that and since we are now using Babel to transpile our code, we can just add the babel-plugin-istanbul to our .babelrc file and we're good to go.&lt;/p&gt;

&lt;p&gt;Now we're almost there. Add the following line to your &lt;strong&gt;cypress/support/index.js&lt;/strong&gt; file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import '@cypress/code-coverage/support'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;And the following snippet to your &lt;strong&gt;cypress/plugins/index.js:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const cypressTypeScriptPreprocessor = require('./cy-ts-preprocessor')

module.exports = (on, config) =&amp;gt; {
  require('@cypress/code-coverage/task')(on, config)
  on('file:preprocessor', cypressTypeScriptPreprocessor);
  return config
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As our final step, let's create a new file &lt;strong&gt;cy-ts-preprocessor.js&lt;/strong&gt; in your &lt;strong&gt;cypress/plugins/&lt;/strong&gt; folder and paste the following snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const wp = require('@cypress/webpack-preprocessor')

const webpackOptions = {
  resolve: {
    extensions: ['.ts', '.js']
  },
  module: {
    rules: [
      {
        test: /\.ts$/,
        exclude: [/node_modules/],
        use: [
          {
            loader: 'ts-loader'
          }
        ]
      }
    ]
  }
}

const options = {
  webpackOptions
}

module.exports = wp(options)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you should be able to run your tests and get a nicely formatted code-coverage report.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;yarn run cypress run&lt;br&gt;
open cypress-coverage/lcov-report/index.html&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I really hope this helps you all to get your Cypress code-coverage going.&lt;/p&gt;

&lt;p&gt;Special thanks to all the folks out there who have put out amazing tutorials as well. I would not have been able to write this tutorial without them. If you want to have a further look, check them out below&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/swlh/setting-up-a-react-typescript-sass-webpack-and-babel-7-project-in-6-steps-b4d172d1d0d6"&gt;Max Bause | Setting up a React + TypeScript + SASS + Webpack and Babel project in 6 Steps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/cssoldiervoif/code-coverage-with-cypress-angular-45pb"&gt;Robert Dale Morris | Code Coverage With Cypress &amp;amp; Angular&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/lluia/cypress-typescript-coverage-example"&gt;Lluis Agusti | Cypress + Typescript App + Code Coverage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/bahmutov/cypress-and-jest"&gt;Gleb Bahmutov | Cypress and Jest&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/cypress-io/code-coverage"&gt;@cypress/code-coverage&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://docs.cypress.io/guides/tooling/code-coverage.html#Introduction"&gt;Official Cypress Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>typescript</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
