<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ speedrun - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ speedrun - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Mon, 25 May 2026 05:06:54 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/speedrun/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ Learn React Hooks in 5 minutes - A Beginner's Tutorial ]]>
                </title>
                <description>
                    <![CDATA[ By Bob Ziroll Sometimes 5 minutes is all you've got. So in this article, we're just going to touch on two of the most used hooks in React: useState and useEffect. If you're not famliar with hooks, here's the TL;DR: because of hooks, there's almost no... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-hooks-in-5-minutes/</link>
                <guid isPermaLink="false">66d45ddd182810487e0ce106</guid>
                
                    <category>
                        <![CDATA[ speedrun ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ react hooks ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 06 Nov 2019 18:14:18 +0000</pubDate>
                <media:content url="https://cdn-media-2.freecodecamp.org/w1280/5f9c9f8f740569d1a4ca4339.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Bob Ziroll</p>
<p>Sometimes 5 minutes is all you've got. So in this article, we're just going to touch on two of the most used hooks in React: <code>useState</code> and <code>useEffect</code>.</p>
<p>If you're not famliar with hooks, here's the TL;DR: because of hooks, there's almost no more need for class-based components. Hooks let you "hook into" the underlying lifecycle and state changes of a component within a functional component. More than that, they often also improve readability and organization of your components.</p>
<p>If you want a proper introduction to this subject, you can join the waitlist for my <a target="_blank" href="https://scrimba.com/g/greact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=hooks_article">upcoming advanced React course</a>, or if you're still a beginner, check out my <a target="_blank" href="https://scrimba.com/g/glearnreact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=hooks_article">introductory course on React.</a></p>
<h2 id="heading-usestate"><code>useState</code></h2>
<p>Let's begin with a functional component.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Change!<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/sj6psapai8j9pawqx8hd.png" alt="Counter at 0" width="506" height="422" loading="lazy"></p>
<p>As you can see, nothing fancy at the moment. We're just rendering some text and a (useless) button.</p>
<p>Now let's import our very first hook, <code>useState</code> to learn how to handle state in our functional component.</p>
<p>As this hook is a function, let's <code>console.log</code> what we get returned from it.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> value = useState();
  <span class="hljs-built_in">console</span>.log(value);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Change!<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>In the console, we get an array</p>
<pre><code class="lang-js">&gt; [<span class="hljs-literal">null</span>, ƒ()]
</code></pre>
<p>And when we pass an argument to <code>useState</code></p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> value = useState(<span class="hljs-literal">true</span>);
</code></pre>
<p>In the console, we get an array with our value as the first member.</p>
<pre><code class="lang-js">&gt; [<span class="hljs-literal">true</span>, ƒ()]
</code></pre>
<p>Now, in our component, we can access our state at <code>value[0]</code> and render it in <code>&lt;h1&gt;</code> instead of a hardcoded value.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> value = useState(<span class="hljs-number">0</span>);
  <span class="hljs-built_in">console</span>.log(value); <span class="hljs-comment">// [0, ƒ()]</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{value[0]}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Change!<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/sj6psapai8j9pawqx8hd.png" alt="Counter at 0" width="506" height="422" loading="lazy"></p>
<p>We can improve our code by using array destructuring to store the value from <code>useState</code> hook. It's similar to object destructuring, which tends to be a bit more commonly seen. In case you're not super familiar with object destructuring, here's a quick recap:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> person = {
  <span class="hljs-attr">name</span>: <span class="hljs-string">'Joe'</span>,
  <span class="hljs-attr">age</span>: <span class="hljs-number">42</span>
};

<span class="hljs-comment">// creates 2 const values from person object</span>
<span class="hljs-keyword">const</span> { name, age } = person;
<span class="hljs-built_in">console</span>.log(name); <span class="hljs-comment">// 'Joe'</span>
<span class="hljs-built_in">console</span>.log(age); <span class="hljs-comment">// 42</span>
</code></pre>
<p>Array destructing is almost the same, but uses square brackets <code>[]</code> instead of curly braces <code>{}</code>.</p>
<p>A quick tip: in object destructuring, the names of created variables must match the names of properties in the object. For array destructuring, that's not the case. It's all about the order. The benefit here is we can name the items whatever we want.</p>
<p>Using array destructuring, we can get the initial value of state from the <code>useState()</code> hook.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// remember, there's a second item from the array that's missing here, but we'll come right back to use it soon</span>
  <span class="hljs-keyword">const</span> [count] = useState(<span class="hljs-number">0</span>);  

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{count}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span>&gt;</span>Change!<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>OK, we've got the initial state value. How do we change the value in the state with hooks?</p>
<p>Remember that <code>useState()</code> hook returns an array with 2 members. The second member is a function that updates the state!</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);
</code></pre>
<p>You can, of course, call it what you wish, but by convention, it's normally called with prefix "set-", and then whatever state variable we wish to update was called, so <code>setCount</code> it is.</p>
<p>It's simple to use this function. Just call it and pass the new value you want that state to have! Or, just like <code>this.setState</code> in a class component, you can pass a function that receives the old state and returns the new state. Rule of thumb: do this anytime you need to rely on the past state to determine the new state.</p>
<p>To call it, we'll pass it to the <code>onClick</code> event listener. And just like with a regular <code>setState</code> in a class-based component, we can pass our state update to <code>setCount</code>.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{count}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setCount(prevCount =&gt; prevCount + 1)}&gt;
        Change!
      <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>We can clean this up a bit, by extracting our state update to a separate function.</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">change</span>(<span class="hljs-params"></span>) </span>{
    setCount(<span class="hljs-function"><span class="hljs-params">prevCount</span> =&gt;</span> prevCount + <span class="hljs-number">1</span>);
  }

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{count}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{change}</span>&gt;</span>Change!<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>Great! And now when we can see the counter going up when we click the button.</p>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/c7hobmvn77cp79bs4n4k.png" alt="Counter at 1" width="528" height="470" loading="lazy"></p>
<p>Of course, <code>useState</code> can get a lot more complicated than this, but we've only got 5 minutes here, so let's move on to the next hook for now.</p>
<h2 id="heading-useeffect"><code>useEffect</code></h2>
<p>Hooks have simplified quite a few things, compared to the way things were in class-based components. Previously we needed to know a bit about lifecycle methods and which one is best suited for which situation. <code>useEffect</code> hook simplified this situation. If you wish to perform side effects, network request, manual DOM manipulation, event listeners or timeouts and intervals.</p>
<p><code>useEffect</code> hook can be imported just like <code>useState</code>.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
</code></pre>
<p>To make <code>useEffect</code> do something, we pass it an anonymous function as an argument. Whenever React re-renders this component, it will run the function we pass to <code>useEffect</code>.</p>
<pre><code class="lang-js">useEffect(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-comment">/* any update can happen here */</span>
});
</code></pre>
<p>This is what the whole code might look like.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { useState, useEffect } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">change</span>(<span class="hljs-params"></span>) </span>{
    setCount(<span class="hljs-function"><span class="hljs-params">prevCount</span> =&gt;</span> prevCount + <span class="hljs-number">1</span>);
  }

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">/* any update can happen here */</span>
  });

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>{count}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{change}</span>&gt;</span>Change!<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>As an example, we will use a nice <code>npm</code> package that generates a random color. Feel free to write your own if you wish of course, but for this tutorial, we will just install it, <code>npm i randomcolor</code>, and import.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> randomcolor <span class="hljs-keyword">from</span> <span class="hljs-string">'randomcolor'</span>;
</code></pre>
<p>Let's now use our knowledge about <code>useState</code> hook to store some random color in the state.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> [color, setColor] = useState(<span class="hljs-string">''</span>); <span class="hljs-comment">// initial value can be an empty string</span>
</code></pre>
<p>We then can then assign the color of the counter we already have.</p>
<pre><code class="lang-js">&lt;h1 style={{ <span class="hljs-attr">color</span>: color }}&gt;{count}&lt;/h1&gt;
</code></pre>
<p>Now, just for the sake of it, let's change the color of the counter on every click of the <code>Change!</code> button. <code>useEffect</code> will run every time the component re-renders, and the component will re-render every time the state is changed.</p>
<p>So if we write the following code, it would get us stuck in an infinite loop! This is a very common gotcha with <code>useEffect</code></p>
<pre><code class="lang-js">useEffect(<span class="hljs-function">() =&gt;</span> {
  setColor(randomcolor());
});
</code></pre>
<p><code>setColor</code> updates state, which re-renders the component, which calls <code>useEffect</code>, which runs <code>setColor</code> to update the state, which re-renders the component... Yikes!</p>
<p>We probably <em>only</em> want to run this <code>useEffect</code> when the <code>count</code> variable changes.</p>
<p>To tell <code>useEffect</code> which variable(s) to keep track of, we give an array of such variables as a second argument.</p>
<pre><code class="lang-js">useEffect(<span class="hljs-function">() =&gt;</span> {
  setColor(randomcolor());
}, [count]);
</code></pre>
<p><img src="https://thepracticaldev.s3.amazonaws.com/i/pqxm4uxhbi2sygovu3gn.png" alt="Counter at 2" width="512" height="484" loading="lazy"></p>
<p>This basically says "only run this effect <strong>if</strong> the <code>count</code> state changes. This way we can change the color and not cause our effect to run infinitely.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>There's a lot more to learn about hooks, but I hope you've enjoyed this quick 5-minute peek into hooks.</p>
<p>To learn more about React Hooks and other great features of React, you can join the waitlist for my <a target="_blank" href="https://scrimba.com/g/greact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=hooks_article">upcoming advanced React course.</a> Or if you're looking for a more beginner friendly  you can check out my <a target="_blank" href="https://scrimba.com/g/greact?utm_source=freecodecamp.org&amp;utm_medium=referral&amp;utm_campaign=hooks_article">introductory course on React.</a></p>
<p>Happy coding ?</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
