Effects, refs and custom Hooks

useEffect with dependencies and cleanup, refs for DOM access and silent values, and custom Hooks for reusable logic.

About 5 hours of study

Learning objectives

Lessons

  1. 1

    Effects: syncing with the outside world Intermediate

    useEffect for work that must happen after rendering: timers, the document title, subscriptions; dependency arrays, cleanup functions, and when you do not need an effect at all.

  2. 2

    Refs: remembering without rendering Intermediate

    useRef for values that must survive renders without causing them, refs to DOM elements for focus and measurement, storing timer ids, and when to use a ref instead of state.

  3. 3

    Custom Hooks: reusing stateful logic Advanced

    Extracting state and effects into functions whose names start with use, what they may return, how each call gets its own state, and the rules of Hooks.

Module practice

Module quiz

1. When does an effect with [] as dependencies run?
2. What happens when ref.current changes?
3. Two components call the same custom Hook. Do they share its state?
4. What is a cleanup function for?
5. Which value belongs in a ref rather than state?
6. Why must Hooks not be called inside an if?

‹ State and formsBuilding bigger apps ›