Summary of "useEffect Explained Dependency Array, Cleanup & Async"

Main Ideas, Concepts, and Lessons


Methodology / Structure of useEffect (with Detailed Bullet Points)

1) Basic Syntax and Purpose

2) Dependency Array Behaviors (3 Main Versions)

3) Cleanup Functions (“Automatic Off Switch”)

4) Dependency Correctness (Avoid Stale Data)


The Five Mistakes to Avoid (Detailed)

  1. Missing dependencies → stale closure / outdated data

    • Problem pattern:
      • effect uses count but count is not in the dependency array
      • effect runs once (e.g., []), captures count = 0
      • later UI updates count to 5, but effect still “thinks” it’s 0
    • Fix:
      • include every value the effect reads in the dependency array
  2. Infinite loops from wrong dependency choices

    • Problem pattern A (self-triggering state update)
      • effect fetches users, then calls setUsers(...)
      • but users is also in the dependency array
      • setUsers triggers re-render → effect runs again → fetch/set repeats forever
    • Fix:
      • If you want fetch only once: use []
      • If you want reruns on some input change: depend on the input (e.g., query), not the state you set (e.g., users)
  3. Objects/functions in the dependency array break reference equality

    • Problem pattern:
      • you include options (an object) in dependencies
      • even if the object content looks identical, it’s a new reference each render
      • React thinks it changed → effect reruns → state updates → rerender → new reference → infinite loop
    • Also applies to functions defined inside the component.
    • Fix options:
      • Fix 1: move the object/function outside the component (module scope) so it stays stable
      • Fix 2: move creation inside the effect so it’s not listed as a dependency
    • Mental model:
      • anything created inside the component body is recreated on every render
  4. Marking the effect itself as async

    • Problem:
      • useEffect(async () => ...) makes the effect return a Promise
      • React expects the cleanup to be a function or nothing, so cleanup won’t work correctly
    • Fix:
      • keep the useEffect function non-async
      • define an inner async function and call it immediately
  5. Race conditions in async work (e.g., search)

    • Problem:
      • user types quickly; multiple fetches occur
      • responses can return out of order
      • older responses may overwrite newer results
    • Fix shown:
      • use a cancellation flag in cleanup
        • set isCanceled = false when the effect starts
        • when the cleanup runs, set isCanceled = true
        • only call setResults(...) if isCanceled is still false
      • effect dependency change triggers the previous cleanup, preventing stale updates

Overuse / Incorrect Use of useEffect (Derived Values Instead)

When useEffect isn’t necessary

Resetting forms when props change (key)


Consolidated “Bring It All Together” Takeaway


Speakers / Sources Featured

Category ?

Educational


Share this summary


Is the summary off?

If you think the summary is inaccurate, you can reprocess it with the latest model.

Video