learn deep. ship safe.Practice testsFollow ByteMunSubscribe

Concept

React hooks

Functions such as useState and useEffect that add state and lifecycle to function components.

React hooks explained

React hooks are functions such as useState, useEffect and useRef that let function components keep state, run side effects and hold values between renders. React tracks hooks by the order they are called in a render, which is why they must always be called in the same order: at the top level of a component or of a custom hook, never inside conditions or loops.

The hooks you use most

  • useState: a value that, when set, schedules a re-render.
  • useEffect: syncs the component with something outside React (a subscription, a timer, the document title) after it renders, and cleans up before the next run or on unmount.
  • useRef: a mutable box that survives renders without causing one; also how you reach a DOM node.
  • useMemo / useCallback: keep a computed value or a function between renders until its inputs change.
  • useContext: read a value provided higher in the tree without passing props through every level.
function Clock() {
  const [now, setNow] = useState(() => new Date());
  useEffect(() => {
    const id = setInterval(() => setNow(new Date()), 1000);
    return () => clearInterval(id); // cleanup
  }, []);                            // run once after the first render
  return <time>{now.toLocaleTimeString()}</time>;
}

Custom hooks

A custom hook is just a function whose name starts with use and that calls other hooks, such as useOnlineStatus or useDebouncedValue. It shares stateful logic between components; each component that calls it gets its own state.

Common mistakes

  • Missing dependencies. An effect that reads a prop or state value but leaves it out of the dependency array keeps using a stale value (a stale closure).
  • Using effects for derived data. If a value can be computed from props and state during render, compute it; an effect plus extra state adds a render and can drift out of sync.
  • Setting state from the old value with setCount(count + 1) inside timers or batched updates. Use setCount(c => c + 1).
  • Surprise double effects in development. Strict Mode runs effects twice on mount in development to reveal missing cleanup; it does not happen in production.

What interviewers ask about React hooks

  • "What are the rules of hooks, and why?" Same order every render, so React can match each call to its stored state.
  • "useMemo versus useCallback?" One caches a value, the other caches a function; both only help when something depends on referential equality.
  • "useRef versus useState?" Changing a ref does not re-render; changing state does.
  • "Fix this effect that runs in an infinite loop." Usually an object or function dependency recreated every render.
Concept map
Learn firstClosuresState
Part of ReactReact hooks

What should you learn before React hooks?

  1. Components and compositionBuilding a UI from small reusable pieces and combining them.
  2. FunctionsReusable blocks of code; in JavaScript they are values that can be passed around and returned.
  3. PropsInputs passed from a parent component to a child.
  4. Variables and scopeWhere a variable can be read: global, function and block scope, and how let, const and var differ.
  5. ClosuresStale closures are the classic hooks bug.
  6. StateData that changes over time and makes the UI re-render, and where to keep it.

What to learn after React hooks

  • ContextPassing data deep into a component tree without threading props through every level.
  • Effects (useEffect)Syncing a component with things outside React, and cleaning up afterwards.
  • MemoizationCaching a computed result or component so it is not recomputed when inputs have not changed.
  • RefsHolding a value or DOM node across renders without causing a re-render.

Connections to other concepts

Part of
React

Practise React hooks

All topics