<?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[Srighnan's Blog]]></title><description><![CDATA[Srighnan's Blog]]></description><link>https://blog.srighnan.me</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 20:41:17 GMT</lastBuildDate><atom:link href="https://blog.srighnan.me/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[React Hooks]]></title><description><![CDATA[Let’s be honest:React Hooks looked scary when they first came out.
Everyone pretended they understood them.Everyone nodded like, “Ah yes, useEffect with dependency arrays… totally makes sense.”
Meanwhile, inside:“Why does this run 57 times???”
But do...]]></description><link>https://blog.srighnan.me/react-hooks</link><guid isPermaLink="true">https://blog.srighnan.me/react-hooks</guid><category><![CDATA[React]]></category><category><![CDATA[hooks]]></category><category><![CDATA[ReactHooks]]></category><category><![CDATA[hooks in react]]></category><category><![CDATA[react js]]></category><category><![CDATA[useState]]></category><category><![CDATA[useEffect]]></category><category><![CDATA[useContext]]></category><category><![CDATA[useRef]]></category><category><![CDATA[useMemo]]></category><category><![CDATA[useCallback]]></category><category><![CDATA[useReducer]]></category><dc:creator><![CDATA[Srighnan Pitchika]]></dc:creator><pubDate>Thu, 11 Dec 2025 09:43:37 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/xkBaqlcqeb4/upload/2ca54ab482a583cdaa4a9f0530931845.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Let’s be honest:<br />React Hooks looked scary when they first came out.</p>
<p>Everyone pretended they understood them.<br />Everyone nodded like, “Ah yes, <code>useEffect</code> with dependency arrays… totally makes sense.”</p>
<p>Meanwhile, inside:<br /><strong>“Why does this run 57 times???”</strong></p>
<p>But don’t worry — Hooks are not here to destroy your sanity.<br />(Okay, maybe just a little.)<br />They actually make React <em>so</em> much nicer once you know what’s going on.</p>
<p>Let’s break them down in the most friendly, slightly sarcastic way possible.</p>
<hr />
<h1 id="heading-what-even-are-react-hooks">🧩 <strong>What Even Are React Hooks?</strong></h1>
<p>Hooks are functions that let you “hook into” React’s brain — state, lifecycle, context, and the usual chaos — <strong>without writing class components</strong>.</p>
<p>Yes, no more <code>this.state</code>, no more binding functions, no more pretending you like classes.</p>
<p>React Hooks = functional components that can actually do things.</p>
<hr />
<h1 id="heading-the-essential-hooks-aka-reacts-greatest-hits">🔥 <strong>The Essential Hooks (a.k.a. React’s Greatest Hits)</strong></h1>
<h2 id="heading-1-usestate-the-i-just-need-to-store-a-number-bro-hook">1️⃣ <strong>useState — The “I just need to store a number, bro” Hook</strong></h2>
<p>This is the hook you’ll use 500 times a week.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);
</code></pre>
<p>Boom.<br />You now have state.<br />You are powerful.<br />You are unstoppable.<br />(Until you accidentally mutate the state and React ignores it.)</p>
<h3 id="heading-when-to-use-it">When to use it:</h3>
<ul>
<li><p>Toggling a modal</p>
</li>
<li><p>Updating forms</p>
</li>
<li><p>Tracking button rage-clicks</p>
</li>
</ul>
<hr />
<h2 id="heading-2-useeffect-the-why-is-this-running-endlessly-hook">2️⃣ <strong>useEffect — The “Why is this running endlessly?” Hook</strong></h2>
<p>Ah yes, the hook responsible for:</p>
<ul>
<li><p>API calls</p>
</li>
<li><p>Subscriptions</p>
</li>
<li><p>40% of your debugging time</p>
</li>
<li><p>60% of your existential crises</p>
</li>
</ul>
<pre><code class="lang-javascript">useEffect(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Running..."</span>);
}, []);
</code></pre>
<h3 id="heading-dependency-arrays">Dependency Arrays:</h3>
<ul>
<li><p><code>[]</code> → run once</p>
</li>
<li><p><code>[value]</code> → run when value changes</p>
</li>
<li><p>nothing → good luck soldier</p>
</li>
</ul>
<hr />
<h2 id="heading-3-usecontext-the-stop-prop-drilling-before-you-lose-your-mind-hook">3️⃣ <strong>useContext — The “Stop Prop-Drilling Before You Lose Your Mind” Hook</strong></h2>
<p>Passing props three levels deep?<br />Just use context.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> user = useContext(UserContext);
</code></pre>
<p>Use it for:</p>
<ul>
<li><p>Themes</p>
</li>
<li><p>User authentication</p>
</li>
<li><p>Anything you want to access without screaming into your component tree</p>
</li>
</ul>
<hr />
<h2 id="heading-4-useref-the-i-just-need-to-grab-an-input-field-okay-hook">4️⃣ <strong>useRef — The “I Just Need to Grab an Input Field, Okay?” Hook</strong></h2>
<p>Two main uses:</p>
<ul>
<li><p>Getting DOM elements</p>
</li>
<li><p>Storing values that don’t cause re-renders</p>
</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> inputRef = useRef();
</code></pre>
<p>You can now do <em>real</em> things like focusing inputs without hacks.</p>
<hr />
<h2 id="heading-5-usememo-the-please-dont-calculate-this-again-hook">5️⃣ <strong>useMemo — The “Please Don’t Calculate This Again” Hook</strong></h2>
<p>When your component re-renders, everything re-runs.<br />React doesn’t care about your CPU.</p>
<p>So you use <code>useMemo</code> to make it chill:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> value = useMemo(<span class="hljs-function">() =&gt;</span> expensiveWork(data), [data]);
</code></pre>
<p>Great for:</p>
<ul>
<li><p>Heavy calculations</p>
</li>
<li><p>Filtering large lists</p>
</li>
<li><p>Pretending your app is optimized</p>
</li>
</ul>
<hr />
<h2 id="heading-6-usecallback-the-why-are-my-child-components-re-rendering-hook">6️⃣ <strong>useCallback — The “Why Are My Child Components Re-rendering?” Hook</strong></h2>
<p>React creates new functions on every render.<br />Children re-render.<br />Performance drops.<br />You cry.</p>
<p>Solution:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> fn = useCallback(<span class="hljs-function">() =&gt;</span> doSomething(), []);
</code></pre>
<p>Now functions stay the same between renders.</p>
<p>Children are happy.<br />You are happy.<br />Everyone wins.</p>
<hr />
<h2 id="heading-7-usereducer-the-my-state-is-too-complicated-for-usestate-hook">7️⃣ <strong>useReducer — The “My State Is Too Complicated for useState” Hook</strong></h2>
<p>If your state starts looking like this:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [state, setState] = useState({ 
  <span class="hljs-attr">items</span>: [], 
  <span class="hljs-attr">loading</span>: <span class="hljs-literal">true</span>, 
  <span class="hljs-attr">error</span>: <span class="hljs-literal">null</span> 
});
</code></pre>
<p>React gently whispers:</p>
<p><strong>“Use a reducer, my child.”</strong></p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> [state, dispatch] = useReducer(reducer, initialState);
</code></pre>
<p>It’s basically mini-Redux but without the crying.</p>
<hr />
<h1 id="heading-common-hooks-mistakes-dont-worry-weve-all-been-there">🤦 Common Hooks Mistakes (Don’t Worry, We’ve All Been There)</h1>
<h3 id="heading-1-useeffect-dependencies-missing">❌ 1. useEffect dependencies missing</h3>
<p>Result: Infinite loops<br />Your laptop: <em>starts sounding like a helicopter</em></p>
<h3 id="heading-2-using-usememousecallback-everywhere">❌ 2. Using useMemo/useCallback everywhere</h3>
<p>Congratulations, you now wasted more time optimizing than your app actually needed.</p>
<h3 id="heading-3-mutating-state-directly">❌ 3. Mutating state directly</h3>
<p>React: “I didn’t see anything. Not re-rendering.”</p>
<h3 id="heading-4-forgetting-cleanup-functions">❌ 4. Forgetting cleanup functions</h3>
<p>Timers keep running.<br />APIs keep fetching.<br />Your app becomes haunted.</p>
<hr />
<h1 id="heading-final-thoughts-hooks-are-your-friends">🎉 Final Thoughts: Hooks Are Your Friends</h1>
<p>React Hooks are like gym workouts:</p>
<ul>
<li><p>Confusing at first</p>
</li>
<li><p>Painful when done wrong</p>
</li>
<li><p>Life-changing when done right</p>
</li>
</ul>
<p>Once you get them, your React code becomes:</p>
<ul>
<li><p>cleaner</p>
</li>
<li><p>easier to read</p>
</li>
<li><p>easier to manage</p>
</li>
<li><p>10x more “senior developer vibes”</p>
</li>
</ul>
<p>And hey — if a hook makes you scream once in a while…<br />That just means you're learning React the <em>real</em> way.</p>
]]></content:encoded></item></channel></rss>