<?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[React Hooks Explained]]></title><description><![CDATA[React Hooks Explained]]></description><link>https://vivekgour1.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 15:13:11 GMT</lastBuildDate><atom:link href="https://vivekgour1.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Mastering React Hooks: Real-World Examples and Best Practices for useState, useEffect, and useRef]]></title><description><![CDATA[When I first started learning React, I thought I was doing fine with class components. Sure, I had to remember lifecycle methods like componentDidMount and componentDidUpdate, and I always messed up the this keyword, but hey — it worked.
Then came Re...]]></description><link>https://vivekgour1.hashnode.dev/mastering-react-hooks-real-world-examples-and-best-practices-for-usestate-useeffect-and-useref</link><guid isPermaLink="true">https://vivekgour1.hashnode.dev/mastering-react-hooks-real-world-examples-and-best-practices-for-usestate-useeffect-and-useref</guid><category><![CDATA[react js]]></category><category><![CDATA[Frontend Development]]></category><category><![CDATA[ReactHooks]]></category><category><![CDATA[hooks]]></category><category><![CDATA[development]]></category><category><![CDATA[DevelopmentJourney]]></category><dc:creator><![CDATA[Vivek Gour]]></dc:creator><pubDate>Wed, 03 Sep 2025 18:10:25 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/UYsBCu9RP3Y/upload/3d20b6751a95933bc10f76b1f7eeb857.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When I first started learning React, I thought I was doing fine with <strong>class components</strong>. Sure, I had to remember lifecycle methods like <code>componentDidMount</code> and <code>componentDidUpdate</code>, and I always messed up the <code>this</code> keyword, but hey — it worked.</p>
<p>Then came <strong>React Hooks</strong>, and suddenly everyone was talking about them. At first, they felt intimidating. I remember looking at <code>useEffect</code> and thinking: <em>“Wait, is this supposed to replace all lifecycle methods? How?”</em></p>
<p>Fast forward a few projects later — Hooks are now my go-to way of building React apps. In this blog, I’ll share the <strong>three most important hooks</strong> (<code>useState</code>, <code>useEffect</code>, <code>useRef</code>) with examples that actually make sense in real-world coding.</p>
<hr />
<h2 id="heading-1-usestate-making-components-remember-things">🔹 1. useState – Making Components Remember Things</h2>
<p>Think of <code>useState</code> as your component’s memory. Without it, every time React re-renders, your variables would reset.</p>
<p>Here’s a classic example — a simple counter:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useState } <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">Counter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Count: {count}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> setCount(count + 1)}&gt;Increase<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<p>💡 Every time you click the button, <code>setCount</code> updates the state and React re-renders the UI. Easy and powerful.</p>
<p>I still remember my first bug: I tried doing <code>count++</code> instead of <code>setCount(count + 1)</code> and spent an hour wondering why the UI didn’t change. Lesson learned: <strong>state must be updated through the setter function</strong>.</p>
<h2 id="heading-2-useeffect-handling-side-effects">🔹 2. useEffect – Handling Side Effects</h2>
<p>In real apps, you don’t just store values. You fetch data, set timers, or sync with APIs. That’s where <code>useEffect</code> comes in.</p>
<p>Example: fetching a list of users from an API:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useState, useEffect } <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">Users</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [users, setUsers] = useState([]);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    fetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/users"</span>)
      .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())
      .then(<span class="hljs-function"><span class="hljs-params">data</span> =&gt;</span> setUsers(data));
  }, []); <span class="hljs-comment">// run once after initial render</span>

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
      {users.map(u =&gt; <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{u.id}</span>&gt;</span>{u.name}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>)}
    <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span></span>
  );
}
</code></pre>
<p>⚠️ My first mistake with <code>useEffect</code> was forgetting the dependency array (<code>[]</code>). Without it, the API call ran <strong>on every re-render</strong>, and I ended up spamming the server. Always remember:</p>
<ul>
<li><p><code>[]</code> → run once.</p>
</li>
<li><p><code>[someVar]</code> → run when <code>someVar</code> changes.</p>
</li>
</ul>
<h2 id="heading-3-useref-keeping-values-without-re-rendering">🔹 3. useRef – Keeping Values Without Re-Rendering</h2>
<p>Here’s something cool: sometimes you need to keep a value between renders without triggering a re-render. That’s what <code>useRef</code> does.</p>
<p>Example: auto-focusing an input box:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useRef, useEffect } <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">InputFocus</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> inputRef = useRef(<span class="hljs-literal">null</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    inputRef.current.focus();
  }, []);

  <span class="hljs-keyword">return</span> <span class="xml"><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">placeholder</span>=<span class="hljs-string">"Type here..."</span> /&gt;</span></span>;
}
</code></pre>
<p>💡 The <code>inputRef</code> here gives you direct access to the DOM element. It’s like saying, <em>“Hey React, don’t re-render this, just let me poke it when I need to.”</em></p>
<p>I use <code>useRef</code> often for timers (<code>setInterval</code>, <code>setTimeout</code>) or storing previous values without causing UI updates.</p>
<h2 id="heading-bonus-a-custom-hook">🔹 Bonus: A Custom Hook</h2>
<p>Once you get comfortable, you can even build your own hooks. Here’s a small one I use for API calls:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> { useState, useEffect } <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">useFetch</span>(<span class="hljs-params">url</span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    fetch(url)
      .then(<span class="hljs-function"><span class="hljs-params">res</span> =&gt;</span> res.json())
      .then(setData);
  }, [url]);

  <span class="hljs-keyword">return</span> data;
}

<span class="hljs-comment">// Usage</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> users = useFetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/users"</span>);
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">pre</span>&gt;</span>{JSON.stringify(users, null, 2)}<span class="hljs-tag">&lt;/<span class="hljs-name">pre</span>&gt;</span></span>;
}
</code></pre>
<p>This way, you can reuse logic without repeating code across components.</p>
<hr />
<h2 id="heading-common-mistakes-i-made-so-you-dont-have-to">🔹 Common Mistakes I Made (So You Don’t Have To)</h2>
<ul>
<li><p>❌ Forgetting the dependency array in <code>useEffect</code>.</p>
</li>
<li><p>❌ Trying to update state directly instead of using setter.</p>
</li>
<li><p>❌ Using <code>useState</code> for values that don’t affect rendering (better use <code>useRef</code>).</p>
</li>
</ul>
<hr />
<h2 id="heading-wrapping-up">🎯 Wrapping Up</h2>
<p>React Hooks might look scary at first, but they simplify React more than anything else. If you’re new to them, start with <code>useState</code>, <code>useEffect</code>, and <code>useRef</code>. Once you get comfortable, explore <code>useReducer</code>, <code>useContext</code>, and even build your own custom hooks.</p>
<p>When I switched from class components to hooks, it felt like a <strong>weight off my shoulders</strong> — fewer bugs, cleaner code, and much more fun.</p>
<p>👉 What about you? Which React Hook gave you the biggest “aha!” moment? Drop it in the comments!</p>
]]></content:encoded></item></channel></rss>