Hooks are functions that let a component "hook into" React features — state, lifecycle, context. You've already met useState. This chapter covers useEffect for side effects (like fetching data), the rules every hook obeys, and how to package your own logic into reusable custom hooks.
What are hooks?
Any function starting with use is a hook: useState, useEffect, useRef, useMemo, and so on. They give function components superpowers that used to require class components.
The rules of hooks
Two rules keep hooks working — the linter enforces them, but understand why:
- Only call hooks at the top level of a component — never inside loops, conditions or nested functions. React tracks hooks by call order, so it must be the same every render.
- Only call hooks from React functions — components or other custom hooks, not plain functions.
useEffect
A component's job is to render. Anything outside rendering — network requests, timers, subscriptions, manual DOM work — is a side effect, and belongs in useEffect. It runs after render, and its dependency array controls when it re-runs:
import { useEffect, useState } from "react";function Timer() {const [seconds, setSeconds] = useState(0);useEffect(() => {const id = setInterval(() => setSeconds(s => s + 1), 1000);return () => clearInterval(id); // cleanup — runs before re-run/unmount}, []); // [] → run once, when the component mountsreturn <p>{seconds}s</p>;}
| Dependency array | When the effect runs |
|---|---|
[] | Once, after the first render (mount) |
[value] | On mount + whenever value changes |
| (omitted) | After every render — rarely what you want |
Fetching data
The classic effect: load data when the component mounts, tracking loading and error state:
function Students() {const [data, setData] = useState([]);const [loading, setLoading] = useState(true);useEffect(() => {let active = true;fetch("/api/students").then(res => res.json()).then(json => { if (active) { setData(json); setLoading(false); } }).catch(() => active && setLoading(false));return () => { active = false; }; // ignore a late response after unmount}, []);if (loading) return <p>Loading…</p>;return <ul>{data.map(s => <li key={s.id}>{s.name}</li>)}</ul>;}
Custom hooks
When two components need the same stateful logic, extract it into a function whose name starts with use. It can call other hooks and return whatever the components need:
function useToggle(initial = false) {const [on, setOn] = useState(initial);const toggle = () => setOn(o => !o);return [on, toggle];}// Any component can now:const [open, toggleOpen] = useToggle();
Practice
Build a component that fetches a list from a public API on mount, shows a "Loading…" state, and renders the results — with proper cleanup. Then extract a useFetch(url) custom hook that returns { data, loading, error }.
Key takeaways
- 01Hooks (functions starting with use) add state and lifecycle to function components.
- 02Call hooks only at the top level of components/custom hooks — never in loops or conditions.
- 03useEffect runs side effects after render; the dependency array controls re-runs ([] = once).
- 04Return a cleanup function from an effect to cancel timers/subscriptions and avoid leaks.
- 05Extract shared stateful logic into a custom use… hook.
