<?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"><channel><title><![CDATA[UseRef in React ⚛️]]></title><description><![CDATA[UseRef in React ⚛️]]></description><link>https://useref-in-react.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Mon, 21 Sep 2026 19:01:37 GMT</lastBuildDate><atom:link href="https://useref-in-react.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🚀 Mastering useRef in React – A Beginner's Guide]]></title><description><![CDATA[When you're learning React, one of the most confusing yet powerful hooks you’ll come across is useRef. I recently explored this concept during my React journey at Devsync, and here’s a simple explanation that helped me understand it better.

🔍 What ...]]></description><link>https://useref-in-react.hashnode.dev/mastering-useref-in-react-a-beginners-guide</link><guid isPermaLink="true">https://useref-in-react.hashnode.dev/mastering-useref-in-react-a-beginners-guide</guid><dc:creator><![CDATA[Pranay Manusmare]]></dc:creator><pubDate>Mon, 09 Jun 2025 13:22:51 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1749474913672/b56fbd64-cee7-4e19-ac0f-46bda117649e.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When you're learning React, one of the most confusing yet powerful hooks you’ll come across is <code>useRef</code>. I recently explored this concept during my React journey at <a target="_blank" href="https://devsync.in"><strong>Devsync</strong></a>, and here’s a simple explanation that helped me understand it better.</p>
<hr />
<h2 id="heading-what-is-useref">🔍 What is <code>useRef</code>?</h2>
<p><code>useRef</code> is a built-in React hook that helps you:</p>
<ol>
<li><p>Access and interact with <strong>DOM elements</strong> directly.</p>
</li>
<li><p>Store <strong>mutable values</strong> without causing re-renders.</p>
</li>
</ol>
<hr />
<h2 id="heading-how-to-use-useref-in-react">📦 How to Use <code>useRef</code> in React</h2>
<p>First, import it:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useRef } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
</code></pre>
<h3 id="heading-1-accessing-dom-elements">✅ 1. Accessing DOM Elements</h3>
<p>A common use case is focusing an input field:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useRef } <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">FocusInput</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> inputRef = useRef(<span class="hljs-literal">null</span>);

  <span class="hljs-keyword">const</span> handleClick = <span class="hljs-function">() =&gt;</span> {
    inputRef.current.focus(); <span class="hljs-comment">// Focuses the input</span>
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{inputRef}</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleClick}</span>&gt;</span>Focus<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/&gt;</span></span>
  );
}
</code></pre>
<blockquote>
<p><code>inputRef.current</code> points to the actual HTML element!</p>
</blockquote>
<hr />
<h3 id="heading-2-keeping-mutable-values">✅ 2. Keeping Mutable Values</h3>
<p>Sometimes, you need to store a value that changes but doesn't cause a re-render.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useRef } <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">ClickCounter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> count = useRef(<span class="hljs-number">0</span>);

  <span class="hljs-keyword">const</span> increment = <span class="hljs-function">() =&gt;</span> {
    count.current += <span class="hljs-number">1</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Clicked:'</span>, count.current);
  };

  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{increment}</span>&gt;</span>Click Me<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span>;
}
</code></pre>
<blockquote>
<p>Unlike <code>useState</code>, changing <code>count.current</code> won’t re-render the component.</p>
</blockquote>
<hr />
<h2 id="heading-why-is-useref-useful">🧠 Why is <code>useRef</code> Useful?</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td><code>useRef</code></td></tr>
</thead>
<tbody>
<tr>
<td>DOM access</td><td>✅ Yes</td></tr>
<tr>
<td>Value persistence</td><td>✅ Yes</td></tr>
<tr>
<td>Re-render on change</td><td>❌ No</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-final-thoughts">💡 Final Thoughts</h2>
<p><code>useRef</code> might seem tricky at first, but once you get used to it, it becomes a valuable tool in your React toolkit. Thanks to <strong>Devsync</strong>, I was able to grasp it with ease and confidence.</p>
<p>If you're also on your learning journey, keep exploring and building — and remember, practice is key!</p>
<hr />
<h3 id="heading-useful-resources">🔗 Useful Resources</h3>
<ul>
<li><p>React Official Docs – useRef</p>
</li>
<li><p><a target="_blank" href="https://devsync.in">Devsync – Learn React the Right Way</a></p>
</li>
</ul>
]]></content:encoded></item></channel></rss>