React Hooks
Hooks are functions that let function components have state and side effects. The ones you must know: useState, useEffect, useRef, useContext. Then useMemo / useCallback when you actually measure extra renders. Custom hooks (useUser, useDebounce) share logic. Classes cannot use hooks — that is why old code still has this.state.
Rules, say them slowly: only call hooks at the top level of a React function (component or custom hook). Not inside if, loops, or nested functions. Same order every render. Names start with use so linters can catch mistakes. Break the order, React throws. This is not optional style.
Let’s share fetch on the board. function useUser(id) { const [user, setUser] = useState(null); useEffect(() => { …fetch… }, [id]); return user; } then Profile and Header both call useUser(id). Same hook rules inside useUser. That is how you stop copy-pasting fetch into five files.
useMemo caches a computed value. useCallback caches a function identity. Don’t wrap everything. Wrap when a child is memoised and a new inline function would break the skip, or when a calculation is actually heavy. Freshers memo-everything and still pass style={{}} — wasted ceremony.
Trap: useState inside if (user). Trap: calling hooks from a plain util.js that is not a hook. Trap: useEffect for something you can compute during render. Trap: ‘hooks replaced Redux’ as a slogan — they replaced classes for local state; big shared state is a separate choice.
React Hooks — click → n updates → effect sets tab title. Both hooks run in the same order every render.
Hook rules + one custom hook example.