<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Alex Chuev on Medium]]></title>
        <description><![CDATA[Stories by Alex Chuev on Medium]]></description>
        <link>https://medium.com/@alex-chuev?source=rss-58cc0e2afc61------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*-4IGkOjCM5JYZ42S.</url>
            <title>Stories by Alex Chuev on Medium</title>
            <link>https://medium.com/@alex-chuev?source=rss-58cc0e2afc61------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 18:18:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@alex-chuev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React useState hook feature that is often missed]]></title>
            <link>https://alex-chuev.medium.com/react-usestate-hook-feature-that-is-often-missed-631a88735c3f?source=rss-58cc0e2afc61------2</link>
            <guid isPermaLink="false">https://medium.com/p/631a88735c3f</guid>
            <category><![CDATA[usestate]]></category>
            <category><![CDATA[react-hook]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Alex Chuev]]></dc:creator>
            <pubDate>Sun, 22 Nov 2020 14:12:01 GMT</pubDate>
            <atom:updated>2020-11-23T11:56:32.849Z</atom:updated>
            <content:encoded><![CDATA[<p>The set function of <em>useState</em> hook can take a callback as an argument.</p><pre>setCount(count =&gt; count + 1);</pre><p>Common use of the state hook is:</p><pre>const Counter = () =&gt; {<br>  const [count, setCount] = <em>useState</em>(0);<br><br>  return (<br>    &lt;button onClick={() =&gt; setCount(count + 1)}&gt;<br>      Increment<br>    &lt;/button&gt;<br>  );<br>};</pre><p>From a performance standpoint, it is good to wrap all callbacks with <em>useCallback</em> hook:</p><pre>const Counter = () =&gt; {<br>  const [count, setCount] = useState(0);<br>  const increment = useCallback(() =&gt; setCount(count + 1), [count])</pre><pre>  return (<br>    &lt;button onClick={increment}&gt;<br>      Increment<br>    &lt;/button&gt;<br>  );<br>};</pre><p>The increment function will be created each time the count value changes. In this case, using useCallback doesn’t make much sense.</p><p>This can be fixed by passing a function to setCount which takes the current value.</p><pre>const Counter = () =&gt; {<br>  const [count, setCount] = useState(0);<br>  const increment = useCallback(() =&gt; setState(count =&gt; count + 1), []);</pre><pre>  return (<br>    &lt;button onClick={increment}&gt;<br>      Increment<br>    &lt;/button&gt;<br>  );<br>}</pre><p>Now the function has no dependencies. It will only be created once on the first render.</p><p>As a small reminder, the original value can also be initialized using a function in case it is non-trivial.</p><pre>const [count, setCount] = useState(() =&gt; getCount(props));</pre><p>Do you know why React hooks cannot be called <a href="https://reactjs.org/docs/hooks-rules.html#explanation">in conditions</a>? :-)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=631a88735c3f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>