<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://lakpahana.github.io/feed.xml" rel="self" type="application/atom+xml" /><link href="https://lakpahana.github.io/" rel="alternate" type="text/html" /><updated>2026-11-15T05:07:08+00:00</updated><id>https://lakpahana.github.io/feed.xml</id><title type="html">Sanjula Lakpahana</title><subtitle>Notes on software engineering</subtitle><entry><title type="html">Session vs JWT</title><link href="https://lakpahana.github.io/blog/2023/08/session-vs-jwt/" rel="alternate" type="text/html" title="Session vs JWT" /><published>2023-08-03T15:16:35+00:00</published><updated>2023-08-03T15:16:35+00:00</updated><id>https://lakpahana.github.io/blog/2023/08/session-vs-jwt</id><content type="html" xml:base="https://lakpahana.github.io/blog/2023/08/session-vs-jwt/"><![CDATA[<p>Session is a server-side storage of information that is created when a user logs in to a web application. The session is stored on the server and the session ID is stored in a cookie on the client-side. The server uses the session ID to retrieve the session data.</p>

<p>JWT is</p>]]></content><author><name></name></author><category term="auth" /><summary type="html"><![CDATA[Session is a server-side storage of information that is created when a user logs in to a web application. The session is stored on the server and the session ID is stored in a cookie on the client-side. The server uses the session ID to retrieve the session data.]]></summary></entry><entry><title type="html">useLayoutEffect and useId</title><link href="https://lakpahana.github.io/blog/2023/07/uselayouteffect-and-useid/" rel="alternate" type="text/html" title="useLayoutEffect and useId" /><published>2023-07-25T17:54:36+00:00</published><updated>2023-07-25T17:54:36+00:00</updated><id>https://lakpahana.github.io/blog/2023/07/uselayouteffect-and-useid</id><content type="html" xml:base="https://lakpahana.github.io/blog/2023/07/uselayouteffect-and-useid/"><![CDATA[<p>useLayoutEffect is almost the same as useEffect except that it’s synchronous to render as opposed to scheduled like useEffect is. If you’re migrating from a class component to a hooks-using function component, this can be helpful too because useLayoutEffect runs at the same time as componentDidMount and componentDidUpdate whereas useEffect is scheduled after. This should be a temporary fix.</p>

<p>The only time you should be using useLayoutEffect is to measure DOM nodes for things like animations. In the example, I measure the textarea after every time you click on it (the onClick is to force a re-render.) This means you’re running render twice but it’s also necessary to be able to capture the correct measurments.</p>

<p>If you make the useLayoutEffect into a useEffect it will have a janky re-render where it’ll flash before it renders correctly. This is exactly why we need useLayoutEffect.</p>

<p>A new hook for version 18 of React is useId. Frequently in React you need unique identifiers to associate two objects together. An example of this would be making sure a label and an input are associated together by the htmlFor attribute.</p>

<p>Previously you could maintain some sort of unique counter that was tracked across renders. With concurrent React and batching in version 18 that’s no longer possible. useId will give you a consistent via a hook so that they can always be the same.</p>

<p>This is useful for the thing we see above: we have a label which needs a for attribute that corresponds to an input. We would either need to use some piece of data/parameter that we’d pass into the component that would serve as the key or we can use this hook to give it a unique ID.</p>

<p>If you need multiple IDs in the same component just do {id}-name, {id}-address, ``{id}-number, etc. No need to call useId` multiple times.</p>

<p>This is safe across server-side renders and client-side.</p>]]></content><author><name></name></author><category term="react" /><summary type="html"><![CDATA[useLayoutEffect is almost the same as useEffect except that it’s synchronous to render as opposed to scheduled like useEffect is. If you’re migrating from a class component to a hooks-using function component, this can be helpful too because useLayoutEffect runs at the same time as componentDidMount and componentDidUpdate whereas useEffect is scheduled after. This should be a temporary fix.]]></summary></entry><entry><title type="html">useCallback</title><link href="https://lakpahana.github.io/blog/2023/07/usecallback/" rel="alternate" type="text/html" title="useCallback" /><published>2023-07-23T11:39:35+00:00</published><updated>2023-07-23T11:39:35+00:00</updated><id>https://lakpahana.github.io/blog/2023/07/usecallback</id><content type="html" xml:base="https://lakpahana.github.io/blog/2023/07/usecallback/"><![CDATA[<p>In React, <code class="language-plaintext highlighter-rouge">useCallback</code> is a hook that helps you optimize the performance of your components by memoizing (caching) functions. It is particularly useful when you pass functions down to child components, as it prevents unnecessary re-renders of those child components when the parent re-renders.</p>

<p>The basic idea behind <code class="language-plaintext highlighter-rouge">useCallback</code> is similar to <code class="language-plaintext highlighter-rouge">useMemo</code>, but instead of memoizing a value, it memoizes a function. It returns a memoized version of the callback function, which only changes if one of the dependencies in the dependency array changes. This is especially beneficial when you use callbacks as props in child components, as it ensures that child components don’t re-render if the callback hasn’t changed.</p>

<p>The syntax of <code class="language-plaintext highlighter-rouge">useCallback</code> is as follows:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useCallback</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="nx">someFunction</span> <span class="o">=</span> <span class="nx">useCallback</span><span class="p">((</span><span class="nx">param</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="c1">// Function logic</span>
    <span class="c1">// This function will be memoized and reused if dependencies don't change</span>
  <span class="p">},</span> <span class="p">[</span><span class="nx">dependency1</span><span class="p">,</span> <span class="nx">dependency2</span><span class="p">,</span> <span class="p">...]);</span> <span class="c1">// List of dependencies that the function relies on</span>

  <span class="c1">// Rest of the component code</span>
<span class="p">};</span>
</code></pre></div></div>

<p>Here’s a breakdown of how <code class="language-plaintext highlighter-rouge">useCallback</code> works:</p>

<ol>
  <li>You wrap a function inside <code class="language-plaintext highlighter-rouge">useCallback</code>.</li>
  <li>The argument to <code class="language-plaintext highlighter-rouge">useCallback</code> is the function you want to memoize.</li>
  <li>The second argument is an array of dependencies that the memoized function relies on. If any of these dependencies change between renders, the memoized function will be recalculated. If the dependencies haven’t changed, the previously cached version of the function is returned, avoiding unnecessary function recreation.</li>
</ol>

<p>Here’s a simple example:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span><span class="p">,</span> <span class="nx">useCallback</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

  <span class="c1">// Function to increment the count</span>
  <span class="kd">const</span> <span class="nx">increment</span> <span class="o">=</span> <span class="nx">useCallback</span><span class="p">(()</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="nx">setCount</span><span class="p">((</span><span class="nx">prevCount</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="nx">prevCount</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
  <span class="p">},</span> <span class="p">[]);</span> <span class="c1">// No dependencies, the function doesn't rely on anything</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Count: <span class="si">{</span><span class="nx">count</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">increment</span><span class="si">}</span><span class="p">&gt;</span>Increment<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>In this example, we use <code class="language-plaintext highlighter-rouge">useCallback</code> to prevent the <code class="language-plaintext highlighter-rouge">increment</code> function from being recreated on each render. Since the <code class="language-plaintext highlighter-rouge">increment</code> function doesn’t rely on any external variables or state (indicated by an empty dependency array), we can safely memoize it. As a result, the child components that receive the <code class="language-plaintext highlighter-rouge">increment</code> function as a prop won’t re-render unnecessarily when the component re-renders.</p>

<p>In short, <code class="language-plaintext highlighter-rouge">useCallback</code> is used to optimize the performance of your React components by memoizing functions. It ensures that the memoized function is only recreated when the dependencies change, preventing unnecessary re-renders and improving performance, especially when passing functions down to child components.</p>

<h2 id="in-simple-terms">In simple terms</h2>

<p>In React, <code class="language-plaintext highlighter-rouge">useCallback</code> is like a special container for functions that helps you avoid creating new functions unnecessarily. When a component renders, functions defined inside it get recreated every time. This can lead to extra work for React, especially when passing those functions to child components as props.</p>

<p><code class="language-plaintext highlighter-rouge">useCallback</code> “remembers” the function you give it and returns a memoized version of that function. This memoized function only changes if the dependencies (things the function uses) change. If the dependencies remain the same, <code class="language-plaintext highlighter-rouge">useCallback</code> gives you back the previously memoized function, saving unnecessary function creations.</p>

<p>Here’s a straightforward example:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span><span class="p">,</span> <span class="nx">useCallback</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

  <span class="c1">// Function to increment the count</span>
  <span class="kd">const</span> <span class="nx">increment</span> <span class="o">=</span> <span class="nx">useCallback</span><span class="p">(()</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
  <span class="p">},</span> <span class="p">[</span><span class="nx">count</span><span class="p">]);</span> <span class="c1">// We add "count" as a dependency here</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Count: <span class="si">{</span><span class="nx">count</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">increment</span><span class="si">}</span><span class="p">&gt;</span>Increment<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>In this example, we use <code class="language-plaintext highlighter-rouge">useCallback</code> to create the <code class="language-plaintext highlighter-rouge">increment</code> function. We also provide a dependency array containing <code class="language-plaintext highlighter-rouge">count</code>, as the function relies on it. By doing this, React knows to only create a new <code class="language-plaintext highlighter-rouge">increment</code> function when the <code class="language-plaintext highlighter-rouge">count</code> changes. Otherwise, it gives you back the previously memoized <code class="language-plaintext highlighter-rouge">increment</code> function, avoiding unnecessary function creations.</p>

<p>So, <code class="language-plaintext highlighter-rouge">useCallback</code> helps you optimize your components by ensuring that functions passed down to child components don’t get recreated when they don’t need to, which can improve performance and efficiency. It’s like having a memory for functions to save resources and work more efficiently.</p>]]></content><author><name></name></author><category term="react" /><summary type="html"><![CDATA[In React, useCallback is a hook that helps you optimize the performance of your components by memoizing (caching) functions. It is particularly useful when you pass functions down to child components, as it prevents unnecessary re-renders of those child components when the parent re-renders.]]></summary></entry><entry><title type="html">useMemo</title><link href="https://lakpahana.github.io/blog/2023/07/usememo/" rel="alternate" type="text/html" title="useMemo" /><published>2023-07-23T11:39:35+00:00</published><updated>2023-07-23T11:39:35+00:00</updated><id>https://lakpahana.github.io/blog/2023/07/usememo</id><content type="html" xml:base="https://lakpahana.github.io/blog/2023/07/usememo/"><![CDATA[<p>In React, <code class="language-plaintext highlighter-rouge">useMemo</code> is a hook that allows you to optimize and control the execution of expensive computations within functional components. It is useful when you have a function or value that takes some time to calculate, and you want to prevent unnecessary recalculations, especially during re-renders.</p>

<p>The basic idea behind <code class="language-plaintext highlighter-rouge">useMemo</code> is to memorize the result of a function call and return that cached result if the dependencies (inputs) to the function have not changed since the last render. This helps avoid redundant computations and improves performance, especially in scenarios where the function’s calculation is computationally intensive or involves heavy operations.</p>

<p>The syntax of <code class="language-plaintext highlighter-rouge">useMemo</code> is as follows:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useMemo</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="c1">// Define a function or a value that you want to memoize</span>
  <span class="kd">const</span> <span class="nx">expensiveResult</span> <span class="o">=</span> <span class="nx">useMemo</span><span class="p">(()</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="c1">// Expensive calculation or operation here</span>
    <span class="c1">// This result will be cached and reused if dependencies don't change</span>
    <span class="k">return</span> <span class="nx">computeExpensiveValue</span><span class="p">();</span>
  <span class="p">},</span> <span class="p">[</span><span class="nx">dependency1</span><span class="p">,</span> <span class="nx">dependency2</span><span class="p">,</span> <span class="p">...]);</span> <span class="c1">// List of dependencies (inputs) to the function</span>

  <span class="c1">// Rest of the component code</span>
<span class="p">};</span>
</code></pre></div></div>

<p>Here’s a breakdown of how <code class="language-plaintext highlighter-rouge">useMemo</code> works:</p>

<ol>
  <li>You wrap a function (or a value) inside <code class="language-plaintext highlighter-rouge">useMemo</code>.</li>
  <li>The first argument to <code class="language-plaintext highlighter-rouge">useMemo</code> is a function that performs the expensive computation.</li>
  <li>The second argument is an array of dependencies that the memoized function relies on. If any of these dependencies change between renders, the memoized result will be recalculated when needed. If the dependencies haven’t changed, the previously cached result is returned, avoiding unnecessary computation.</li>
</ol>

<p>Here’s an example demonstrating the use of <code class="language-plaintext highlighter-rouge">useMemo</code>:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span><span class="p">,</span> <span class="nx">useMemo</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">number</span><span class="p">,</span> <span class="nx">setNumber</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

  <span class="c1">// An expensive computation function</span>
  <span class="kd">const</span> <span class="nx">computeExpensiveValue</span> <span class="o">=</span> <span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Performing expensive calculation...</span><span class="dl">'</span><span class="p">);</span>
    <span class="k">return</span> <span class="nx">value</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span>
  <span class="p">};</span>

  <span class="c1">// Memoizing the result of the expensive computation</span>
  <span class="kd">const</span> <span class="nx">doubledValue</span> <span class="o">=</span> <span class="nx">useMemo</span><span class="p">(()</span> <span class="o">=&gt;</span> <span class="nx">computeExpensiveValue</span><span class="p">(</span><span class="nx">number</span><span class="p">),</span> <span class="p">[</span><span class="nx">number</span><span class="p">]);</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Number: <span class="si">{</span><span class="nx">number</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Doubled Value: <span class="si">{</span><span class="nx">doubledValue</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=&gt;</span> <span class="nx">setNumber</span><span class="p">(</span><span class="nx">number</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="si">}</span><span class="p">&gt;</span>Increment<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>In this example, the <code class="language-plaintext highlighter-rouge">computeExpensiveValue</code> function performs a costly calculation of doubling a number. By using <code class="language-plaintext highlighter-rouge">useMemo</code>, we ensure that the computation is only executed when the <code class="language-plaintext highlighter-rouge">number</code> state changes. On each increment, the component re-renders, but since the <code class="language-plaintext highlighter-rouge">number</code> state is the same as before, the memoized value of <code class="language-plaintext highlighter-rouge">doubledValue</code> is reused, and the expensive calculation is not performed again until <code class="language-plaintext highlighter-rouge">number</code> changes.</p>

<p>In short, <code class="language-plaintext highlighter-rouge">useMemo</code> is used to optimize the performance of React components by caching the results of expensive computations and preventing unnecessary recalculations when the inputs (dependencies) to those computations remain the same between renders. It is a valuable tool for improving performance in scenarios where you need to handle heavy calculations or data transformations in your components.</p>

<h2 id="in-simple-terms">In simple terms</h2>

<p>Imagine you have a function that does a lot of math or complex calculations. Every time your component renders, this function gets called again, even if the inputs to that function haven’t changed. This can slow down your app unnecessarily.</p>

<p><code class="language-plaintext highlighter-rouge">useMemo</code> is like a memory bank for your function’s results. It remembers the output of the function the first time it’s called and checks if the inputs are the same during the next renders. If the inputs haven’t changed, it gives you back the cached result, avoiding the need to recalculate everything.</p>

<p>Here’s a simple example:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span><span class="p">,</span> <span class="nx">useMemo</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">number</span><span class="p">,</span> <span class="nx">setNumber</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

  <span class="c1">// An expensive calculation function</span>
  <span class="kd">const</span> <span class="nx">doubleTheNumber</span> <span class="o">=</span> <span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="nx">console</span><span class="p">.</span><span class="nx">log</span><span class="p">(</span><span class="dl">'</span><span class="s1">Performing expensive calculation...</span><span class="dl">'</span><span class="p">);</span>
    <span class="k">return</span> <span class="nx">value</span> <span class="o">*</span> <span class="mi">2</span><span class="p">;</span>
  <span class="p">};</span>

  <span class="c1">// Memoizing the result of the expensive calculation</span>
  <span class="kd">const</span> <span class="nx">doubledValue</span> <span class="o">=</span> <span class="nx">useMemo</span><span class="p">(()</span> <span class="o">=&gt;</span> <span class="nx">doubleTheNumber</span><span class="p">(</span><span class="nx">number</span><span class="p">),</span> <span class="p">[</span><span class="nx">number</span><span class="p">]);</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Number: <span class="si">{</span><span class="nx">number</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Doubled Value: <span class="si">{</span><span class="nx">doubledValue</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=&gt;</span> <span class="nx">setNumber</span><span class="p">(</span><span class="nx">number</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span><span class="si">}</span><span class="p">&gt;</span>Increment<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>In this example, we use <code class="language-plaintext highlighter-rouge">useMemo</code> to avoid unnecessary recalculation of the <code class="language-plaintext highlighter-rouge">doubleTheNumber</code> function. When you click the “Increment” button, the <code class="language-plaintext highlighter-rouge">number</code> increases, and the component re-renders. But because the <code class="language-plaintext highlighter-rouge">number</code> didn’t change in this render, <code class="language-plaintext highlighter-rouge">useMemo</code> gives you back the previously calculated <code class="language-plaintext highlighter-rouge">doubledValue</code>, without calling the expensive <code class="language-plaintext highlighter-rouge">doubleTheNumber</code> function again.</p>

<p>In short, <code class="language-plaintext highlighter-rouge">useMemo</code> helps you save time and improve performance by caching the results of expensive calculations and avoiding redundant computations when the inputs remain the same between renders. It’s like a smart calculator that remembers previous results to save you from doing the same calculations over and over.</p>]]></content><author><name></name></author><category term="react" /><summary type="html"><![CDATA[In React, useMemo is a hook that allows you to optimize and control the execution of expensive computations within functional components. It is useful when you have a function or value that takes some time to calculate, and you want to prevent unnecessary recalculations, especially during re-renders.]]></summary></entry><entry><title type="html">useReducer</title><link href="https://lakpahana.github.io/blog/2023/07/usereducer/" rel="alternate" type="text/html" title="useReducer" /><published>2023-07-22T18:33:39+00:00</published><updated>2023-07-22T18:33:39+00:00</updated><id>https://lakpahana.github.io/blog/2023/07/usereducer</id><content type="html" xml:base="https://lakpahana.github.io/blog/2023/07/usereducer/"><![CDATA[<p>In React, <code class="language-plaintext highlighter-rouge">useReducer</code> is a tool that helps you manage state in a way that’s more suitable for complex scenarios. Instead of using separate <code class="language-plaintext highlighter-rouge">useState</code> calls for individual pieces of state, <code class="language-plaintext highlighter-rouge">useReducer</code> uses a single function called a “reducer” to handle state changes based on actions.</p>

<p>Here’s a simple analogy to understand it better:</p>

<p>Imagine you have a “counter” with buttons to increase and decrease the count. With <code class="language-plaintext highlighter-rouge">useState</code>, you might do this:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useState</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">count</span><span class="p">,</span> <span class="nx">setCount</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

  <span class="kd">const</span> <span class="nx">increment</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span><span class="p">);</span>
  <span class="p">};</span>

  <span class="kd">const</span> <span class="nx">decrement</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="nx">setCount</span><span class="p">(</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span><span class="p">);</span>
  <span class="p">};</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Count: <span class="si">{</span><span class="nx">count</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">increment</span><span class="si">}</span><span class="p">&gt;</span>Increment<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="nx">decrement</span><span class="si">}</span><span class="p">&gt;</span>Decrement<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>Now, with <code class="language-plaintext highlighter-rouge">useReducer</code>, we can manage this state more efficiently:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useReducer</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="c1">// Reducer function: Handles state updates based on actions</span>
<span class="kd">const</span> <span class="nx">reducer</span> <span class="o">=</span> <span class="p">(</span><span class="nx">state</span><span class="p">,</span> <span class="nx">action</span><span class="p">)</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="k">switch</span> <span class="p">(</span><span class="nx">action</span><span class="p">.</span><span class="nx">type</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">case</span> <span class="dl">'</span><span class="s1">INCREMENT</span><span class="dl">'</span><span class="p">:</span>
      <span class="k">return</span> <span class="p">{</span> <span class="na">count</span><span class="p">:</span> <span class="nx">state</span><span class="p">.</span><span class="nx">count</span> <span class="o">+</span> <span class="mi">1</span> <span class="p">};</span>
    <span class="k">case</span> <span class="dl">'</span><span class="s1">DECREMENT</span><span class="dl">'</span><span class="p">:</span>
      <span class="k">return</span> <span class="p">{</span> <span class="na">count</span><span class="p">:</span> <span class="nx">state</span><span class="p">.</span><span class="nx">count</span> <span class="o">-</span> <span class="mi">1</span> <span class="p">};</span>
    <span class="nl">default</span><span class="p">:</span>
      <span class="k">return</span> <span class="nx">state</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">};</span>

<span class="kd">const</span> <span class="nx">initialState</span> <span class="o">=</span> <span class="p">{</span> <span class="na">count</span><span class="p">:</span> <span class="mi">0</span> <span class="p">};</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="p">[</span><span class="nx">state</span><span class="p">,</span> <span class="nx">dispatch</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useReducer</span><span class="p">(</span><span class="nx">reducer</span><span class="p">,</span> <span class="nx">initialState</span><span class="p">);</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Count: <span class="si">{</span><span class="nx">state</span><span class="p">.</span><span class="nx">count</span><span class="si">}</span><span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=&gt;</span> <span class="nx">dispatch</span><span class="p">({</span> <span class="na">type</span><span class="p">:</span> <span class="dl">'</span><span class="s1">INCREMENT</span><span class="dl">'</span> <span class="p">})</span><span class="si">}</span><span class="p">&gt;</span>Increment<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">button</span> <span class="na">onClick</span><span class="p">=</span><span class="si">{</span><span class="p">()</span> <span class="o">=&gt;</span> <span class="nx">dispatch</span><span class="p">({</span> <span class="na">type</span><span class="p">:</span> <span class="dl">'</span><span class="s1">DECREMENT</span><span class="dl">'</span> <span class="p">})</span><span class="si">}</span><span class="p">&gt;</span>Decrement<span class="p">&lt;/</span><span class="nt">button</span><span class="p">&gt;</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>In this version, we use <code class="language-plaintext highlighter-rouge">useReducer</code> instead of multiple <code class="language-plaintext highlighter-rouge">useState</code> calls. The <code class="language-plaintext highlighter-rouge">reducer</code> function takes the current state and an <code class="language-plaintext highlighter-rouge">action</code> as input and returns the updated state based on the action type. We then use <code class="language-plaintext highlighter-rouge">useReducer</code> to manage the state and get a <code class="language-plaintext highlighter-rouge">dispatch</code> function, which we use to send actions to the reducer.</p>

<p>So, <code class="language-plaintext highlighter-rouge">useReducer</code> helps us organize and handle state in a more centralized way, making it easier to manage more complex state logic in our components. It can be especially useful when dealing with multiple actions that need to update the same state.</p>]]></content><author><name></name></author><category term="react" /><summary type="html"><![CDATA[In React, useReducer is a tool that helps you manage state in a way that’s more suitable for complex scenarios. Instead of using separate useState calls for individual pieces of state, useReducer uses a single function called a “reducer” to handle state changes based on actions.]]></summary></entry><entry><title type="html">useRef</title><link href="https://lakpahana.github.io/blog/2023/07/useref/" rel="alternate" type="text/html" title="useRef" /><published>2023-07-22T18:24:02+00:00</published><updated>2023-07-22T18:24:02+00:00</updated><id>https://lakpahana.github.io/blog/2023/07/useref</id><content type="html" xml:base="https://lakpahana.github.io/blog/2023/07/useref/"><![CDATA[<p>Refs can be used for a variety of purposes. One particular use for them is if you need to interact with the actual DOM (as opposed to the React virtualization of it) directly. This is pretty rare and really only when you need to derive measurements from the DOM (like width) or you’re using an external library and it needs a real DOM node to interact with.</p>

<p>In our example, let’s integrate Three.js with React. Three.js is a library that allows you to do 3D graphics in the browser and has its own runtime outside of React. React never guarantees that a DOM node isn’t going to re-render at any given time and in general this is a good thing: we don’t have to care about actually updating the DOM: React does it for us. However it’s a problem with Three.js: we need to insert a DOM node directly into a DOM element (which React would control.) That’s where the ref is useful: it allows to get an actual hold on the DOM node underneath so we can interact with it.</p>

<h2 id="in-simple-terms">In simple terms</h2>

<p>In React, <code class="language-plaintext highlighter-rouge">useRef</code> is a tool that helps you with two main things:</p>

<ol>
  <li>
    <p><strong>Referring to HTML elements</strong>: When you want to work with an HTML element directly, like reading its value, changing its appearance, or giving it focus, you can use <code class="language-plaintext highlighter-rouge">useRef</code> to “grab” that element and save it for later use.</p>
  </li>
  <li>
    <p><strong>Keeping data between renders</strong>: If you have some data that you want to store and access between renders of a component without causing the component to update, you can use <code class="language-plaintext highlighter-rouge">useRef</code> for that purpose.</p>
  </li>
</ol>

<p>Here’s a basic example for each use case:</p>

<ol>
  <li><strong>Referring to HTML elements</strong>:</li>
</ol>

<p>Suppose you have an input field and you want to focus on it automatically when the component loads:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useRef</span><span class="p">,</span> <span class="nx">useEffect</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="nx">inputRef</span> <span class="o">=</span> <span class="nx">useRef</span><span class="p">();</span>

  <span class="nx">useEffect</span><span class="p">(()</span> <span class="o">=&gt;</span> <span class="p">{</span>
    <span class="c1">// This runs after the component renders</span>
    <span class="nx">inputRef</span><span class="p">.</span><span class="nx">current</span><span class="p">.</span><span class="nx">focus</span><span class="p">();</span> <span class="c1">// This focuses the input field</span>
  <span class="p">},</span> <span class="p">[]);</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">input</span> <span class="na">ref</span><span class="p">=</span><span class="si">{</span><span class="nx">inputRef</span><span class="si">}</span> <span class="na">type</span><span class="p">=</span><span class="s">"text"</span> <span class="p">/&gt;</span>
      <span class="si">{</span><span class="cm">/* Other JSX content */</span><span class="si">}</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<ol>
  <li><strong>Keeping data between renders</strong>:</li>
</ol>

<p>Suppose you want to count how many times the component has rendered, but you don’t want the count to trigger a re-render:</p>

<div class="language-jsx highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">import</span> <span class="nx">React</span><span class="p">,</span> <span class="p">{</span> <span class="nx">useRef</span> <span class="p">}</span> <span class="k">from</span> <span class="dl">'</span><span class="s1">react</span><span class="dl">'</span><span class="p">;</span>

<span class="kd">const</span> <span class="nx">MyComponent</span> <span class="o">=</span> <span class="p">()</span> <span class="o">=&gt;</span> <span class="p">{</span>
  <span class="kd">const</span> <span class="nx">renderCount</span> <span class="o">=</span> <span class="nx">useRef</span><span class="p">(</span><span class="mi">0</span><span class="p">);</span>

  <span class="nx">renderCount</span><span class="p">.</span><span class="nx">current</span><span class="o">++</span><span class="p">;</span> <span class="c1">// Increase the count without causing a re-render</span>

  <span class="k">return</span> <span class="p">(</span>
    <span class="p">&lt;</span><span class="nt">div</span><span class="p">&gt;</span>
      <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>Component has rendered <span class="si">{</span><span class="nx">renderCount</span><span class="p">.</span><span class="nx">current</span><span class="si">}</span> times.<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span>
      <span class="si">{</span><span class="cm">/* Other JSX content */</span><span class="si">}</span>
    <span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span>
  <span class="p">);</span>
<span class="p">};</span>
</code></pre></div></div>

<p>In short, <code class="language-plaintext highlighter-rouge">useRef</code> is a handy tool in React that helps you work with DOM elements directly and maintain data between renders without causing unnecessary re-renders. It’s useful for managing things that don’t need to be part of the component’s state but still need to be accessed and preserved across renders.</p>

<p>Why memo?
React is very fast at re-rendering and 99.999% of the time you never have to worry when React re-renders, just that your view is a function of your state.</p>

<p>The .001% of times you care is when you have something that is either very performance sensitive and you want to have a tighter grip on the performance or something like our Three.js app running in it where re-rendering it will destroy and re-create a pretty expensive thing to re-render. It also looks bad because it’ll reset the animation.</p>

<p>React.memo tells React “as long as the parameters being passed into this component don’t change, do not re-render it ever. You might be tempted to do this on every component but believe me, don’t. Things will no re-render when you expect them to and you will forget you memoized them. Only use memo where you need to.</p>]]></content><author><name></name></author><category term="react" /><summary type="html"><![CDATA[Refs can be used for a variety of purposes. One particular use for them is if you need to interact with the actual DOM (as opposed to the React virtualization of it) directly. This is pretty rare and really only when you need to derive measurements from the DOM (like width) or you’re using an external library and it needs a real DOM node to interact with.]]></summary></entry></feed>