useEffect and API Calls

Harry · 22 Sep 2026 · 1 views
Log in to track your progress and mark lessons complete.

The useEffect Hook

Run side effects after render. An empty dependency array runs once; listed values re-run the effect when they change.

useEffect(() => { fetchUsers(); }, []);
useEffect(() => { log(count); }, [count]);

Cleanup and Abort

Return a cleanup function; abort in-flight fetches with AbortController to avoid setting state on unmounted components.

API Service Layer

Keep HTTP in services/api.js: base URL, auth headers, 401 refresh, typed responses. Components stay focused on UI.

Key Points

  • Show loading, error and empty states.
  • Prefer async/await with try/catch.
  • Centralize API logic for testability.
Share this post:

Comments (0)

Please login or register to comment.

Create a free account to keep reading

You've enjoyed a free tutorial! Register (it's free) to unlock every tutorial, track your progress and save code.

or sign in with your account

Already have an account? Log in