<?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: kazuki</title>
    <description>The latest articles on DEV Community by kazuki (@kazuki_2025).</description>
    <link>https://dev.to/kazuki_2025</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%2F3316632%2F0389f521-fbd0-4019-8c3f-c26d747883f4.png</url>
      <title>DEV Community: kazuki</title>
      <link>https://dev.to/kazuki_2025</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kazuki_2025"/>
    <language>en</language>
    <item>
      <title>Understanding useCallback in React</title>
      <dc:creator>kazuki</dc:creator>
      <pubDate>Wed, 02 Jul 2025 13:43:23 +0000</pubDate>
      <link>https://dev.to/kazuki_2025/understanding-usecallback-in-react-18gb</link>
      <guid>https://dev.to/kazuki_2025/understanding-usecallback-in-react-18gb</guid>
      <description>&lt;p&gt;If you've been working with React function components, you might have heard about &lt;code&gt;useCallback&lt;/code&gt;. But what exactly does it do and why is it useful? Let's break it down simply.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Challenge with Function Components&lt;/strong&gt;&lt;br&gt;
In React, &lt;strong&gt;function components re-run entirely on every re-render&lt;/strong&gt;. This means that any functions you define inside your component are also recreated from scratch each time the component updates.&lt;/p&gt;

&lt;p&gt;While this behavior is usually fine, it can lead to performance issues, especially when you pass these functions down as props to child components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enter &lt;code&gt;useCallback&lt;/code&gt;: Memoizing Your Functions&lt;/strong&gt;&lt;br&gt;
This is where &lt;code&gt;useCallback&lt;/code&gt; comes in handy. It's a React Hook that lets you memoize a function. In simple terms, it "remembers" your function.&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;MyParentComponent&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;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&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 function will only be recreated if its dependencies change&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Button clicked!&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="c1"&gt;// Empty dependency array means it's created once&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;MyChildComponent&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="sr"&gt;/&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;p&gt;By wrapping your function with &lt;code&gt;useCallback&lt;/code&gt;, React will provide the same function instance on subsequent renders, as long as the values in its dependency array haven't changed.&lt;/p&gt;

&lt;p&gt;Why Memoize Functions?&lt;br&gt;
The primary benefits of using useCallback include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Preventing Unnecessary Child Re-renders:&lt;/strong&gt; If you pass a function to a child component that is itself memoized (e.g., with &lt;code&gt;React.memo&lt;/code&gt;), &lt;code&gt;useCallback&lt;/code&gt; ensures that the child doesn't re-render just because the parent re-rendered and created a "new" function instance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Optimizing &lt;code&gt;useEffect&lt;/code&gt; and &lt;code&gt;useMemo&lt;/code&gt;:&lt;/strong&gt; When functions are part of the dependency arrays for &lt;code&gt;useEffect&lt;/code&gt; or &lt;code&gt;useMemo&lt;/code&gt;, &lt;code&gt;useCallback&lt;/code&gt; helps prevent those hooks from running unnecessarily, as it ensures the function reference remains stable.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When to Use It?&lt;/strong&gt;&lt;br&gt;
You don't need &lt;code&gt;useCallback&lt;/code&gt; for every function. It adds a small overhead. Focus on using it when:&lt;/p&gt;

&lt;p&gt;You're passing callback functions to &lt;strong&gt;memoized child components&lt;/strong&gt; (e.g., using &lt;code&gt;React.memo&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The function is part of a &lt;strong&gt;dependency array for &lt;code&gt;useEffect&lt;/code&gt; or &lt;code&gt;useMemo&lt;/code&gt;&lt;/strong&gt;, and its stability is crucial.&lt;/p&gt;

&lt;p&gt;By understanding and selectively applying &lt;code&gt;useCallback&lt;/code&gt;, you can write more performant and stable React applications.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
