<?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: Luca Mellano</title>
    <description>The latest articles on DEV Community by Luca Mellano (@lum3ll_96).</description>
    <link>https://dev.to/lum3ll_96</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%2F579697%2Fc5cb4910-5e66-41d2-b564-de1423bf3163.jpeg</url>
      <title>DEV Community: Luca Mellano</title>
      <link>https://dev.to/lum3ll_96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/lum3ll_96"/>
    <language>en</language>
    <item>
      <title>React: useState or useRef?</title>
      <dc:creator>Luca Mellano</dc:creator>
      <pubDate>Fri, 03 Nov 2023 17:37:02 +0000</pubDate>
      <link>https://dev.to/lum3ll_96/react-usestate-or-useref-4gog</link>
      <guid>https://dev.to/lum3ll_96/react-usestate-or-useref-4gog</guid>
      <description>&lt;p&gt;In React, useState and useRef are two hooks that allow us to manage state and references in functional components. Here I want to describe the main differences between them:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Re-rendering&lt;/strong&gt;&lt;br&gt;
This is maybe the main difference between useState and useRef. useState triggers a re-render when its state changes. On the other hand, useRef does not cause a re-render when its current value changes. This could save a lot of time spent in unuseful re-render caused by simple state change.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// useState
setCount(count + 1); // triggers re-render

// useRef
inputRef.current = 'Hello'; // does not trigger re-render
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Return Values&lt;/strong&gt;&lt;br&gt;
useState returns an array with the current state value and a function to update it ([value, setValue]), while useRef returns an object with a current property ({current: initialValue}).&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// useState
const [count, setCount] = useState(0);

// useRef
const inputRef = useRef(null);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Immutability vs Mutability&lt;/strong&gt;&lt;br&gt;
useState is “immutable”, meaning you must use the state setter function to modify state variables and queue a re-render. In contrast, useRef is “mutable”, allowing you to modify and update the current value outside of the rendering process.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// useState
setCount(count + 1); // correct way to update state

// useRef
inputRef.current = 'Hello'; // correct way to update ref
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Reading Values&lt;/strong&gt;&lt;br&gt;
You can read state at any time with useState, but each render has its own snapshot of state which does not change. Conversely, with useRef, you shouldn’t read (or write) the current value during rendering.&lt;/p&gt;

&lt;p&gt;Ex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// useState
console.log(count); // logs the count state

// useRef
console.log(inputRef.current); // logs the current ref value (not during render)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These differences may help us to select effectively which one use in our projects.&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
  </channel>
</rss>
