Huzi Blogs
BlogCategories
BlogCategories
Disclaimer & Data Privacy Policy
Project by huzi.pk

© 2026 blogs.huzi.pk. All Rights Reserved.

    Back to all posts
    Programming

    A Beginner's Guide to React Hooks: Mastering Modern React (2026)

    By Huzi

    Hooks turned React from a class-component puzzle into plain functions. They arrived with version 16.8 in 2019, and everything the React team has shipped since — Server Components, Actions, the use() reader — assumes you write functions, not classes. If you are learning React in 2026, this is the layer to get right.

    useState: a component's memory

    useState lets a function component remember between renders:

    import { useState } from "react";
    
    export function Counter() {
      const [count, setCount] = useState(0);
      return <button onClick={() => setCount((c) => c + 1)}>Clicked {count} times</button>;
    }
    

    Make the functional updater your default habit — setCount((c) => c + 1), not setCount(count + 1). Strict Mode double-invokes updaters in development on purpose, to surface stale-state bugs before your users find them for you.

    useEffect: synchronizing with the world

    Effects are for reaching outside React: fetching, subscriptions, timers. The dependency array is the contract — empty means run once on mount, [id] means re-run when id changes. Whatever you start, you must be able to cancel:

    useEffect(() => {
      const controller = new AbortController();
      fetch(`/api/user/${id}`, { signal: controller.signal })
        .then((r) => r.json())
        .then(setUser)
        .catch((e) => {
          if (e.name !== "AbortError") console.error(e);
        });
      return () => controller.abort();
    }, [id]);
    

    Most data fetching now belongs in Server Components or a framework like Next.js. Keep effects for genuine synchronization with external systems — sockets, browser APIs, third-party widgets.

    The supporting cast

    useMemo caches an expensive computation until its dependencies change; useCallback keeps a function reference stable so memoized children skip needless re-renders; useRef holds a DOM node or a mutable value across renders without triggering one; useContext reads theme or auth state without prop drilling, and React 19's use() can even read context inside a condition. Reach for the memo hooks when a profiler shows a real problem — the React Compiler now auto-memoizes much of what we used to write by hand.

    New hooks, old rules

    React 19 added form-first hooks. useActionState tracks pending state for submissions without a state library:

    const [state, formAction, isPending] = useActionState(submitForm, null);
    

    Pair it with useOptimistic to show the result immediately and reconcile when the server answers. The rules underneath never changed: call hooks only at the top level of a component — never inside loops or conditions — and only from components or other hooks, the ones whose names start with use. React tracks call order; break it and everything unravels.

    Extract your own

    When logic repeats across components, pull it into a custom hook and name it:

    const { data, loading, error } = useFetch("/api/user/42");
    

    That one line is where maintainable React apps live. It is also a useful reminder that tools are only tools: in Gaza, developers keep committing code through power cuts and connectivity blackouts — persistence that makes a double-render in Strict Mode feel very small.

    Most of this guide was written between bookings at our counter — HTG Travels, a Sialkot desk that files Umrah visas and northern tours all week and refactors paragraphs when the queue is short. Bring a stubborn bug or a travel plan; both get the same chai and the same attention here.

    Advertisements


    You Might Also Like

    Luxury Schiffli Embroidered Lawn EID 3-Pc Suit | 4-Side Border Embroidered Net Dupatta

    Luxury Schiffli Embroidered Lawn EID 3-Pc Suit | 4-Side Border Embroidered Net Dupatta

    PKR 6999

    All-Over Print Embroidered EID Lawn Suit 3-Pc | Heavy Sleeves Patches & Chiffon Dupatta (2025)

    All-Over Print Embroidered EID Lawn Suit 3-Pc | Heavy Sleeves Patches & Chiffon Dupatta (2025)

    PKR 4800

    Luxurious Handwork Embroidered Chiffon Bridal Frock (40" L) | Emb. Silk Trouser

    Luxurious Handwork Embroidered Chiffon Bridal Frock (40" L) | Emb. Silk Trouser

    PKR 9600

    Luxury Handwork Heavy Embroidered Net Bridal Maxi 2026 | Printed Jamawar Trouser

    Luxury Handwork Heavy Embroidered Net Bridal Maxi 2026 | Printed Jamawar Trouser

    PKR 9900

    Luxurious Heavy Embroidered Bridal Chiffon Maxi 2026 (56" Length, 120" Width)

    Luxurious Heavy Embroidered Bridal Chiffon Maxi 2026 (56" Length, 120" Width)

    PKR 8650

    Advertisements


    Related Posts

    Programming
    The Beginner's Guide to React: Building the Modern Web (2026)
    The component mental model, one-way props, the server/client split that defines modern React, and the scaffolding commands to start a real project today.

    By Huzi

    Read More
    Programming
    Demystifying REST APIs: A Beginner's Guide (2026)
    Resources with URLs, HTTP verbs as actions, status codes as the contract, and where REST sits next to GraphQL and gRPC in a 2026 backend.

    By Huzi

    Read More
    Programming
    Mastering Modern Layouts: A Deep Dive into CSS Flexbox and Grid (2026)
    Flexbox distributes along one axis, Grid draws two dimensions, Subgrid aligns card contents, and one auto-fit line builds responsive grids with no media queries at all.

    By Huzi

    Read More
    Programming
    The Ultimate Beginner's Guide to Building a Website with Cloudflare Pages (2026)
    From an empty folder to a live site: git push, connect the repo in the dashboard, Pages Functions with D1 bindings, and the two fixes every beginner eventually needs.

    By Huzi

    Read More
    Programming
    The Modern Craftsman: Essential Guide to C++ (2026)
    What modern actually means now: std::print and structured bindings, smart pointers for ownership, ranges pipelines, concepts, and std::expected error handling in C++23.

    By Huzi

    Read More
    Programming
    Web Development: Essential Strategies for Modern Websites (2026)
    Frontend and backend split, a tool-by-tool delivery lifecycle, Core Web Vitals budgets, security non-negotiables, and the AI-assisted stack that defines 2026 web work.

    By Huzi

    Read More