<?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[tekytips]]></title><description><![CDATA[tekytips]]></description><link>https://teky.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 03:33:13 GMT</lastBuildDate><atom:link href="https://teky.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Mastering React Hooks: A Complete Guide with Real-World Examples]]></title><description><![CDATA[Have you ever started a new web project with React and had to struggle with how to handle things such as state and lifecycle methods?
React provides Hooks which are a more direct API to the React concepts you already know: props, state, context, refs...]]></description><link>https://teky.hashnode.dev/mastering-react-hooks-a-complete-guide-with-real-world-examples</link><guid isPermaLink="true">https://teky.hashnode.dev/mastering-react-hooks-a-complete-guide-with-real-world-examples</guid><category><![CDATA[React]]></category><category><![CDATA[ReactHooks]]></category><category><![CDATA[Next.js]]></category><category><![CDATA[useState]]></category><category><![CDATA[useEffect]]></category><category><![CDATA[useRef]]></category><category><![CDATA[useCallback]]></category><dc:creator><![CDATA[Jubril Folajimi]]></dc:creator><pubDate>Wed, 08 Oct 2025 23:00:36 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/xkBaqlcqeb4/upload/94e9d2cce1e8282567c7316a80f88b27.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Have you ever started a new web project with React and had to struggle with how to handle things such as state and lifecycle methods?</p>
<p>React provides Hooks which are a more direct API to the React concepts you already know: props, state, context, refs, and lifecycle. Every time I work on a web project with React, I notice how much I need to use one or more of these hooks to build a robust web app.</p>
<p>I'll use this article to show how you can use the most popular React hooks in a real-world example. If you follow this article to the very end, you'll have learnt how to use hooks such as useState, useEffect, useMemo, useCallback, useContext, useRef and custom hooks.</p>
<h2 id="heading-what-are-react-hooks">What are React Hooks?</h2>
<p>React Hooks are functions that let you "hook into" React features from function components. They allow you to use state and other React features without writing a class component. Hooks were introduced in React 16.8 and have revolutionized how we write React applications.</p>
<h2 id="heading-1-usestate-managing-component-state">1. useState - Managing Component State</h2>
<p>The <code>useState</code> hook is the most fundamental hook for managing state in functional components.</p>
<pre><code class="lang-jsx"><span class="hljs-keyword">import</span> React, { 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">const</span> [name, setName] = useState(<span class="hljs-string">''</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;
        Increment
      <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> 
        <span class="hljs-attr">value</span>=<span class="hljs-string">{name}</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e)</span> =&gt;</span> setName(e.target.value)}
        placeholder="Enter your name"
      /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h3 id="heading-real-world-example-search-and-filter-state"><strong>Real-world Example: Search and Filter State</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TransactionPage</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [searchTerm, setSearchTerm] = useState(<span class="hljs-string">''</span>);
  <span class="hljs-keyword">const</span> [filterObj, setFilterObj] = useState({
    <span class="hljs-attr">amount</span>: <span class="hljs-literal">undefined</span>,
    <span class="hljs-attr">dateFrom</span>: <span class="hljs-literal">undefined</span>,
    <span class="hljs-attr">dateTo</span>: <span class="hljs-literal">undefined</span>,
    <span class="hljs-attr">status</span>: <span class="hljs-string">''</span>,
    <span class="hljs-attr">locations</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">input</span> 
        <span class="hljs-attr">value</span>=<span class="hljs-string">{searchTerm}</span>
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e)</span> =&gt;</span> setSearchTerm(e.target.value)}
        placeholder="Search transactions..."
      /&gt;
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-2-useeffect-handling-side-effects"><strong>2. useEffect - Handling Side Effects</strong></h2>
<p>The useEffect hook lets you perform side effects in function components. It serves the same purpose as componentDidMount, componentDidUpdate, and componentWillUnmount combined.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { 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">UserProfile</span>(<span class="hljs-params">{ userId }</span>) </span>{
  <span class="hljs-keyword">const</span> [user, setUser] = useState(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">true</span>);

  <span class="hljs-comment">// Effect runs after every render</span>
  useEffect(<span class="hljs-function">() =&gt;</span> {
    fetchUser(userId)
      .then(<span class="hljs-function"><span class="hljs-params">userData</span> =&gt;</span> {
        setUser(userData);
        setLoading(<span class="hljs-literal">false</span>);
      });
  }, [userId]); <span class="hljs-comment">// Dependency array - effect runs when userId changes</span>

  <span class="hljs-comment">// Cleanup effect</span>
  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> timer = <span class="hljs-built_in">setInterval</span>(<span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Timer tick'</span>);
    }, <span class="hljs-number">1000</span>);

    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">clearInterval</span>(timer); <span class="hljs-comment">// Cleanup function</span>
  }, []);

  <span class="hljs-keyword">if</span> (loading) <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Loading...<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Welcome, {user?.name}!<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
}
</code></pre>
<h3 id="heading-real-world-example-data-fetching-and-cleanup"><strong>Real-world Example: Data Fetching and Cleanup</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TransactionList</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [transactions, setTransactions] = useState([]);
  <span class="hljs-keyword">const</span> [totalCount, setTotalCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-comment">// Fetch data when component mounts or dependencies change</span>
  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> fetchTransactions = <span class="hljs-keyword">async</span> () =&gt; {
      <span class="hljs-keyword">try</span> {
        <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> api.getTransactions({
          <span class="hljs-attr">pageNumber</span>: <span class="hljs-number">1</span>,
          <span class="hljs-attr">pageSize</span>: <span class="hljs-number">10</span>,
        });
        setTransactions(response.data.results);
        setTotalCount(response.data.totalCount);
      } <span class="hljs-keyword">catch</span> (error) {
        <span class="hljs-built_in">console</span>.error(<span class="hljs-string">'Failed to fetch transactions:'</span>, error);
      }
    };

    fetchTransactions();
  }, []); <span class="hljs-comment">// Empty dependency array - runs once on mount</span>

  <span class="hljs-comment">// Clear search when component unmounts</span>
  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> {
      setSearchTerm(<span class="hljs-string">''</span>);
    };
  }, []);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {transactions.map(transaction =&gt; (
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{transaction.id}</span>&gt;</span>{transaction.amount}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      ))}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-3-usememo-optimizing-expensive-calculations"><strong>3. useMemo - Optimizing Expensive Calculations</strong></h2>
<p>The useMemo hook memoizes expensive calculations and only recalculates when dependencies change.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useState, useMemo } <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">ExpensiveComponent</span>(<span class="hljs-params">{ items, filter }</span>) </span>{
  <span class="hljs-keyword">const</span> [count, setCount] = useState(<span class="hljs-number">0</span>);

  <span class="hljs-comment">// Expensive calculation - only runs when items or filter changes</span>
  <span class="hljs-keyword">const</span> filteredItems = useMemo(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Filtering items...'</span>);
    <span class="hljs-keyword">return</span> items.filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> 
      item.name.toLowerCase().includes(filter.toLowerCase())
    );
  }, [items, filter]);

  <span class="hljs-comment">// Another expensive calculation</span>
  <span class="hljs-keyword">const</span> totalValue = useMemo(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">return</span> filteredItems.reduce(<span class="hljs-function">(<span class="hljs-params">sum, item</span>) =&gt;</span> sum + item.value, <span class="hljs-number">0</span>);
  }, [filteredItems]);

  <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;Increment<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Total Value: ${totalValue}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
        {filteredItems.map(item =&gt; (
          <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{item.id}</span>&gt;</span>{item.name}: ${item.value}<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 class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h3 id="heading-real-world-example-filter-status-calculation"><strong>Real-world Example: Filter Status Calculation</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TerminalManager</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [appliedFilters, setAppliedFilters] = useState([]);

  <span class="hljs-comment">// Memoized calculations for filter states</span>
  <span class="hljs-keyword">const</span> statusActive = useMemo(
    <span class="hljs-function">() =&gt;</span> !!appliedFilters?.find(<span class="hljs-function"><span class="hljs-params">el</span> =&gt;</span> el.key === <span class="hljs-string">"status"</span>)?.key,
    [appliedFilters]
  );

  <span class="hljs-keyword">const</span> dateActive = useMemo(
    <span class="hljs-function">() =&gt;</span> !!appliedFilters?.find(<span class="hljs-function"><span class="hljs-params">el</span> =&gt;</span> el.key === <span class="hljs-string">"date"</span>)?.key,
    [appliedFilters]
  );

  <span class="hljs-keyword">const</span> terminalAddressActive = useMemo(
    <span class="hljs-function">() =&gt;</span> !!appliedFilters?.find(<span class="hljs-function"><span class="hljs-params">el</span> =&gt;</span> el.key === <span class="hljs-string">"terminalAddress"</span>)?.key,
    [appliedFilters]
  );

  <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">FilterTab</span> <span class="hljs-attr">active</span>=<span class="hljs-string">{statusActive}</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Status"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">FilterTab</span> <span class="hljs-attr">active</span>=<span class="hljs-string">{dateActive}</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Date"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">FilterTab</span> <span class="hljs-attr">active</span>=<span class="hljs-string">{terminalAddressActive}</span> <span class="hljs-attr">title</span>=<span class="hljs-string">"Address"</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-4-usecallback-memoizing-functions"><strong>4. useCallback - Memoizing Functions</strong></h2>
<p>The useCallback hook memoizes functions to prevent unnecessary re-renders of child components.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useState, useCallback, memo } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-comment">// Child component that only re-renders when props change</span>
<span class="hljs-keyword">const</span> ChildComponent = memo(<span class="hljs-function">(<span class="hljs-params">{ onButtonClick, name }</span>) =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Child component rendered'</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>Hello, {name}!<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">{onButtonClick}</span>&gt;</span>Click me<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>
  );
});

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ParentComponent</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">const</span> [name, setName] = useState(<span class="hljs-string">'John'</span>);

  <span class="hljs-comment">// Without useCallback, this function is recreated on every render</span>
  <span class="hljs-keyword">const</span> handleButtonClick = useCallback(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Button clicked!'</span>);
    <span class="hljs-comment">// Some expensive operation</span>
  }, []); <span class="hljs-comment">// Empty dependency array - function never changes</span>

  <span class="hljs-comment">// With dependencies</span>
  <span class="hljs-keyword">const</span> handleNameChange = useCallback(<span class="hljs-function">(<span class="hljs-params">newName</span>) =&gt;</span> {
    setName(newName);
  }, []);

  <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;Increment<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">ChildComponent</span> 
        <span class="hljs-attr">onButtonClick</span>=<span class="hljs-string">{handleButtonClick}</span>
        <span class="hljs-attr">name</span>=<span class="hljs-string">{name}</span>
      /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h3 id="heading-real-world-example-event-handlers"><strong>Real-world Example: Event Handlers</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">SearchComponent</span>(<span class="hljs-params">{ onSearch, suggestions }</span>) </span>{
  <span class="hljs-keyword">const</span> [searchTerm, setSearchTerm] = useState(<span class="hljs-string">''</span>);

  <span class="hljs-comment">// Memoized search handler</span>
  <span class="hljs-keyword">const</span> handleSearch = useCallback(<span class="hljs-function">(<span class="hljs-params">term</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (term.trim()) {
      onSearch(term);
    }
  }, [onSearch]);

  <span class="hljs-comment">// Memoized input change handler</span>
  <span class="hljs-keyword">const</span> handleInputChange = useCallback(<span class="hljs-function">(<span class="hljs-params">e</span>) =&gt;</span> {
    <span class="hljs-keyword">const</span> value = e.target.value;
    setSearchTerm(value);

    <span class="hljs-comment">// Debounced search</span>
    <span class="hljs-keyword">const</span> timeoutId = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
      handleSearch(value);
    }, <span class="hljs-number">300</span>);

    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">clearTimeout</span>(timeoutId);
  }, [handleSearch]);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span>
      <span class="hljs-attr">value</span>=<span class="hljs-string">{searchTerm}</span>
      <span class="hljs-attr">onChange</span>=<span class="hljs-string">{handleInputChange}</span>
      <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Search..."</span>
    /&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-5-usecontext-sharing-state-across-components"><strong>5. useContext - Sharing State Across Components</strong></h2>
<p>The useContext hook allows you to consume context values without wrapping components in Consumer components.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { createContext, useContext, useState } <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;

<span class="hljs-comment">// Create context</span>
<span class="hljs-keyword">const</span> ThemeContext = createContext();
<span class="hljs-keyword">const</span> UserContext = createContext();

<span class="hljs-comment">// Context provider component</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">AppProvider</span>(<span class="hljs-params">{ children }</span>) </span>{
  <span class="hljs-keyword">const</span> [theme, setTheme] = useState(<span class="hljs-string">'light'</span>);
  <span class="hljs-keyword">const</span> [user, setUser] = useState(<span class="hljs-literal">null</span>);

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ThemeContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">theme</span>, <span class="hljs-attr">setTheme</span> }}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">UserContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{{</span> <span class="hljs-attr">user</span>, <span class="hljs-attr">setUser</span> }}&gt;</span>
        {children}
      <span class="hljs-tag">&lt;/<span class="hljs-name">UserContext.Provider</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">ThemeContext.Provider</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// Custom hooks for consuming context</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useTheme</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> context = useContext(ThemeContext);
  <span class="hljs-keyword">if</span> (!context) {
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'useTheme must be used within AppProvider'</span>);
  }
  <span class="hljs-keyword">return</span> context;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useUser</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> context = useContext(UserContext);
  <span class="hljs-keyword">if</span> (!context) {
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'useUser must be used within AppProvider'</span>);
  }
  <span class="hljs-keyword">return</span> context;
}

<span class="hljs-comment">// Components using context</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Header</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> { theme, setTheme } = useTheme();
  <span class="hljs-keyword">const</span> { user } = useUser();

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">header</span> <span class="hljs-attr">className</span>=<span class="hljs-string">{</span>`<span class="hljs-attr">header</span> ${<span class="hljs-attr">theme</span>}`}&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Welcome, {user?.name || 'Guest'}!<span class="hljs-tag">&lt;/<span class="hljs-name">h1</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> setTheme(theme === 'light' ? 'dark' : 'light')}&gt;
        Toggle Theme
      <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span></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">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">AppProvider</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Header</span> /&gt;</span>
      {/* Other components */}
    <span class="hljs-tag">&lt;/<span class="hljs-name">AppProvider</span>&gt;</span></span>
  );
}
</code></pre>
<h3 id="heading-real-world-example-global-state-management"><strong>Real-world Example: Global State Management</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// Store context for global state</span>
<span class="hljs-keyword">const</span> StoreContext = createContext();

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">StoreProvider</span>(<span class="hljs-params">{ children }</span>) </span>{
  <span class="hljs-keyword">const</span> [amount, setAmount] = useState(<span class="hljs-literal">undefined</span>);
  <span class="hljs-keyword">const</span> [period, setPeriod] = useState(<span class="hljs-literal">undefined</span>);
  <span class="hljs-keyword">const</span> [downloadHistory, setDownloadHistory] = useState([]);

  <span class="hljs-keyword">const</span> value = {
    amount,
    setAmount,
    period,
    setPeriod,
    downloadHistory,
    setDownloadHistory,
  };

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">StoreContext.Provider</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{value}</span>&gt;</span>
      {children}
    <span class="hljs-tag">&lt;/<span class="hljs-name">StoreContext.Provider</span>&gt;</span></span>
  );
}

<span class="hljs-comment">// Custom hook to use store</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useStore</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> context = useContext(StoreContext);
  <span class="hljs-keyword">if</span> (!context) {
    <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">'useStore must be used within StoreProvider'</span>);
  }
  <span class="hljs-keyword">return</span> context;
}

<span class="hljs-comment">// Component using global state</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">TransactionFilter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> { amount, setAmount, period, setPeriod } = useStore();

  <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">input</span>
        <span class="hljs-attr">type</span>=<span class="hljs-string">"number"</span>
        <span class="hljs-attr">value</span>=<span class="hljs-string">{amount</span> || ''}
        <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e)</span> =&gt;</span> setAmount(e.target.value)}
        placeholder="Enter amount"
      /&gt;
      <span class="hljs-tag">&lt;<span class="hljs-name">select</span> <span class="hljs-attr">value</span>=<span class="hljs-string">{period</span> || ''} <span class="hljs-attr">onChange</span>=<span class="hljs-string">{(e)</span> =&gt;</span> setPeriod(e.target.value)}&gt;
        <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">""</span>&gt;</span>Select period<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"today"</span>&gt;</span>Today<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"week"</span>&gt;</span>This Week<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">option</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"month"</span>&gt;</span>This Month<span class="hljs-tag">&lt;/<span class="hljs-name">option</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">select</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-6-useref-accessing-dom-elements-and-persisting-values"><strong>6. useRef - Accessing DOM Elements and Persisting Values</strong></h2>
<p>The useRef hook creates a mutable ref object that persists across re-renders.</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">import</span> React, { useRef, useEffect, 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">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> countRef = useRef(<span class="hljs-number">0</span>);
  <span class="hljs-keyword">const</span> [renderCount, setRenderCount] = useState(<span class="hljs-number">0</span>);

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

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-comment">// Update ref value without causing re-render</span>
    countRef.current = countRef.current + <span class="hljs-number">1</span>;
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Render count:'</span>, countRef.current);
  });

  <span class="hljs-keyword">const</span> handleFocus = <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">div</span>&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">placeholder</span>=<span class="hljs-string">"This will be focused"</span> /&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{handleFocus}</span>&gt;</span>Focus Input<span class="hljs-tag">&lt;/<span class="hljs-name">button</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> setRenderCount(renderCount + 1)}&gt;
        Re-render ({renderCount})
      <span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Component has rendered {countRef.current} times<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h3 id="heading-real-world-example-click-outside-detection"><strong>Real-world Example: Click Outside Detection</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useOnClickOutside</span>(<span class="hljs-params">ref, handler</span>) </span>{
  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> listener = <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> {
      <span class="hljs-keyword">if</span> (!ref.current || ref.current.contains(event.target)) {
        <span class="hljs-keyword">return</span>;
      }
      handler(event);
    };

    <span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'mousedown'</span>, listener);
    <span class="hljs-built_in">document</span>.addEventListener(<span class="hljs-string">'touchstart'</span>, listener);

    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> {
      <span class="hljs-built_in">document</span>.removeEventListener(<span class="hljs-string">'mousedown'</span>, listener);
      <span class="hljs-built_in">document</span>.removeEventListener(<span class="hljs-string">'touchstart'</span>, listener);
    };
  }, [ref, handler]);
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Modal</span>(<span class="hljs-params">{ isOpen, onClose, children }</span>) </span>{
  <span class="hljs-keyword">const</span> modalRef = useRef(<span class="hljs-literal">null</span>);

  useOnClickOutside(modalRef, onClose);

  <span class="hljs-keyword">if</span> (!isOpen) <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"modal-overlay"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{modalRef}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"modal-content"</span>&gt;</span>
        {children}
        <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{onClose}</span>&gt;</span>Close<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 class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-7-custom-hooks-reusable-logic"><strong>7. Custom Hooks - Reusable Logic</strong></h2>
<p>Custom hooks allow you to extract component logic into reusable functions.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Custom hook for API calls</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useApi</span>(<span class="hljs-params">url, options = {}</span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState(<span class="hljs-literal">null</span>);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">true</span>);
  <span class="hljs-keyword">const</span> [error, setError] = useState(<span class="hljs-literal">null</span>);

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">const</span> fetchData = <span class="hljs-keyword">async</span> () =&gt; {
      <span class="hljs-keyword">try</span> {
        setLoading(<span class="hljs-literal">true</span>);
        setError(<span class="hljs-literal">null</span>);

        <span class="hljs-keyword">const</span> response = <span class="hljs-keyword">await</span> fetch(url, options);
        <span class="hljs-keyword">if</span> (!response.ok) {
          <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Error</span>(<span class="hljs-string">`HTTP error! status: <span class="hljs-subst">${response.status}</span>`</span>);
        }

        <span class="hljs-keyword">const</span> result = <span class="hljs-keyword">await</span> response.json();
        setData(result);
      } <span class="hljs-keyword">catch</span> (err) {
        setError(err.message);
      } <span class="hljs-keyword">finally</span> {
        setLoading(<span class="hljs-literal">false</span>);
      }
    };

    fetchData();
  }, [url]);

  <span class="hljs-keyword">return</span> { data, loading, error };
}

<span class="hljs-comment">// Custom hook for local storage</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useLocalStorage</span>(<span class="hljs-params">key, initialValue</span>) </span>{
  <span class="hljs-keyword">const</span> [storedValue, setStoredValue] = useState(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-keyword">try</span> {
      <span class="hljs-keyword">const</span> item = <span class="hljs-built_in">window</span>.localStorage.getItem(key);
      <span class="hljs-keyword">return</span> item ? <span class="hljs-built_in">JSON</span>.parse(item) : initialValue;
    } <span class="hljs-keyword">catch</span> (error) {
      <span class="hljs-built_in">console</span>.error(<span class="hljs-string">`Error reading localStorage key "<span class="hljs-subst">${key}</span>":`</span>, error);
      <span class="hljs-keyword">return</span> initialValue;
    }
  });

  <span class="hljs-keyword">const</span> setValue = useCallback(<span class="hljs-function">(<span class="hljs-params">value</span>) =&gt;</span> {
    <span class="hljs-keyword">try</span> {
      setStoredValue(value);
      <span class="hljs-built_in">window</span>.localStorage.setItem(key, <span class="hljs-built_in">JSON</span>.stringify(value));
    } <span class="hljs-keyword">catch</span> (error) {
      <span class="hljs-built_in">console</span>.error(<span class="hljs-string">`Error setting localStorage key "<span class="hljs-subst">${key}</span>":`</span>, error);
    }
  }, [key]);

  <span class="hljs-keyword">return</span> [storedValue, setValue];
}

<span class="hljs-comment">// Using custom hooks</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">UserProfile</span>(<span class="hljs-params">{ userId }</span>) </span>{
  <span class="hljs-keyword">const</span> { <span class="hljs-attr">data</span>: user, loading, error } = useApi(<span class="hljs-string">`/api/users/<span class="hljs-subst">${userId}</span>`</span>);
  <span class="hljs-keyword">const</span> [preferences, setPreferences] = useLocalStorage(<span class="hljs-string">'userPreferences'</span>, {});

  <span class="hljs-keyword">if</span> (loading) <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Loading...<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>;
  <span class="hljs-keyword">if</span> (error) <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Error: {error}<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></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">h1</span>&gt;</span>{user.name}<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Email: {user.email}<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> setPreferences({ ...preferences, theme: 'dark' })}&gt;
        Set Dark Theme
      <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>
<h3 id="heading-real-world-example-window-dimensions-hook"><strong>Real-world Example: Window Dimensions Hook</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">useWindowDimensions</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [windowDimensions, setWindowDimensions] = useState({
    <span class="hljs-attr">width</span>: <span class="hljs-literal">undefined</span>,
    <span class="hljs-attr">height</span>: <span class="hljs-literal">undefined</span>,
  });

  useEffect(<span class="hljs-function">() =&gt;</span> {
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">handleResize</span>(<span class="hljs-params"></span>) </span>{
      setWindowDimensions({
        <span class="hljs-attr">width</span>: <span class="hljs-built_in">window</span>.innerWidth,
        <span class="hljs-attr">height</span>: <span class="hljs-built_in">window</span>.innerHeight,
      });
    }

    handleResize(); <span class="hljs-comment">// Set initial dimensions</span>
    <span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">'resize'</span>, handleResize);

    <span class="hljs-keyword">return</span> <span class="hljs-function">() =&gt;</span> <span class="hljs-built_in">window</span>.removeEventListener(<span class="hljs-string">'resize'</span>, handleResize);
  }, []);

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

<span class="hljs-comment">// Usage</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">ResponsiveComponent</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> { width } = useWindowDimensions();

  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
      {width <span class="hljs-tag">&lt; <span class="hljs-attr">768</span> ? (
        &lt;<span class="hljs-attr">MobileLayout</span> /&gt;</span>
      ) : (
        <span class="hljs-tag">&lt;<span class="hljs-name">DesktopLayout</span> /&gt;</span>
      )}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}
</code></pre>
<h2 id="heading-best-practices-and-common-pitfalls"><strong>Best Practices and Common Pitfalls</strong></h2>
<h3 id="heading-1-rules-of-hooks"><strong>1. Rules of Hooks</strong></h3>
<ul>
<li><p>Only call hooks at the top level of your React function</p>
</li>
<li><p>Don't call hooks inside loops, conditions, or nested functions</p>
</li>
<li><p>Only call hooks from React function components or custom hooks</p>
</li>
</ul>
<h3 id="heading-2-dependency-arrays"><strong>2. Dependency Arrays</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// ❌ Missing dependencies</span>
useEffect(<span class="hljs-function">() =&gt;</span> {
  fetchData(userId, filter);
}, []); <span class="hljs-comment">// Missing userId and filter</span>

<span class="hljs-comment">// ✅ Correct dependencies</span>
useEffect(<span class="hljs-function">() =&gt;</span> {
  fetchData(userId, filter);
}, [userId, filter]);

<span class="hljs-comment">// ✅ Using useCallback for stable references</span>
<span class="hljs-keyword">const</span> fetchData = useCallback(<span class="hljs-keyword">async</span> (id, filterValue) =&gt; {
  <span class="hljs-comment">// fetch logic</span>
}, []);

useEffect(<span class="hljs-function">() =&gt;</span> {
  fetchData(userId, filter);
}, [fetchData, userId, filter]);
</code></pre>
<h3 id="heading-3-avoiding-infinite-loops"><strong>3. Avoiding Infinite Loops</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// ❌ This will cause infinite re-renders</span>
useEffect(<span class="hljs-function">() =&gt;</span> {
  setData(processData(data));
}, [data]);

<span class="hljs-comment">// ✅ Use a different trigger or memoization</span>
useEffect(<span class="hljs-function">() =&gt;</span> {
  setData(processData(rawData));
}, [rawData]);

<span class="hljs-comment">// ✅ Or use useMemo for derived state</span>
<span class="hljs-keyword">const</span> processedData = useMemo(<span class="hljs-function">() =&gt;</span> {
  <span class="hljs-keyword">return</span> processData(rawData);
}, [rawData]);
</code></pre>
<h3 id="heading-4-performance-optimization"><strong>4. Performance Optimization</strong></h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// ❌ Creating new objects/arrays in render</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Component</span>(<span class="hljs-params">{ items }</span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ExpensiveChild</span> 
      <span class="hljs-attr">items</span>=<span class="hljs-string">{items.filter(item</span> =&gt;</span> item.active)} // New array every render
      config={{ theme: 'dark' }} // New object every render
    /&gt;</span>
  );
}

<span class="hljs-comment">// ✅ Use useMemo and useCallback</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Component</span>(<span class="hljs-params">{ items }</span>) </span>{
  <span class="hljs-keyword">const</span> activeItems = useMemo(
    <span class="hljs-function">() =&gt;</span> items.filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item.active),
    [items]
  );

  <span class="hljs-keyword">const</span> config = useMemo(<span class="hljs-function">() =&gt;</span> ({ <span class="hljs-attr">theme</span>: <span class="hljs-string">'dark'</span> }), []);

  <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">ExpensiveChild</span> <span class="hljs-attr">items</span>=<span class="hljs-string">{activeItems}</span> <span class="hljs-attr">config</span>=<span class="hljs-string">{config}</span> /&gt;</span></span>;
}
</code></pre>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>React Hooks have revolutionized how we write React applications by providing a more direct and functional approach to managing state and side effects. The hooks we've covered - useState, useEffect, useMemo, useCallback, useContext, useRef and custom hooks - form the foundation of modern React development.</p>
<p>Key takeaways:</p>
<ul>
<li><p><strong>useState</strong> for managing component state</p>
</li>
<li><p><strong>useEffect</strong> for side effects and lifecycle events</p>
</li>
<li><p><strong>useMemo</strong> for expensive calculations</p>
</li>
<li><p><strong>useCallback</strong> for memoizing functions</p>
</li>
<li><p><strong>useContext</strong> for sharing state across components</p>
</li>
<li><p><strong>useRef</strong> for DOM access and persistent values</p>
</li>
<li><p><strong>Custom hooks</strong> for reusable logic</p>
</li>
</ul>
<p>By mastering these hooks and following best practices, you'll be able to build more efficient, maintainable, and performant React applications. Remember to always consider the dependency arrays, avoid common pitfalls like infinite loops, and use performance optimization techniques when necessary.</p>
<p>Start incorporating these patterns into your React projects, and you'll quickly see how hooks can simplify your code while making it more powerful and reusable.</p>
<hr />
<p><em>Happy coding! 🚀</em></p>
]]></content:encoded></item></channel></rss>