React Tutorial 0/120 lessons ~6 min read Lesson 55

    Loading States

    Loading States cover explicit async status models, skeletons, empty states, initial loading vs background refresh, flicker, and accessibility.

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Course: React — Routing & API Integration Lesson: 10

    Learning objectives

    Model asynchronous UI states, distinguish initial loading from background refreshing, design skeletons and empty states, and prevent confusing user experiences.

    Why loading states matter

    A network request takes time and can fail. If the interface gives no feedback, users may assume the application is frozen or click an action repeatedly. A robust data view communicates what is happening.

    A simple state model:

    text
    idle -> loading -> success
    -> error

    For richer experiences, distinguish initial loading, refreshing, success with data, empty success, and failure.

    Explicit status model

    javascript
    const [state, setState] = useState({
    status: "idle",
    data: [],
    error: null
    });

    Using one status value reduces contradictory combinations such as isLoading === true and isSuccess === true when those states should be mutually exclusive.

    Render by state

    javascript
    function OrdersView({ state, onRetry }) {
    if (state.status === "loading") return <OrdersSkeleton />;
    if (state.status === "error") {
    return <ErrorPanel message="Orders could not be loaded." onRetry={onRetry} />;
    }
    if (state.status === "success" && state.data.length === 0) {
    return <EmptyState title="No orders yet" />;
    }
    return <OrderTable orders={state.data} />;
    }

    A loading skeleton should resemble the structure of the final content without pretending that placeholder data is real.

    Initial loading vs background refresh

    Initial loading means no usable data is available yet. Background refresh means existing data remains visible while a newer response is being fetched. Do not replace a useful table with a full-screen spinner every time a filter changes if a subtle refresh indicator would be clearer.

    Avoid flicker and stale results

    For fast requests, a spinner may flash briefly. A small minimum display delay or skeleton strategy may improve perceived stability, but avoid arbitrary delays that make the application slower. When filters or route parameters change quickly, cancel or ignore obsolete requests so older responses cannot overwrite newer results.

    Accessibility

    Use clear status text and appropriate live-region semantics where necessary. Do not communicate loading only through color or animation. Ensure skeletons do not create confusing screen-reader output.

    Common mistakes

    • Showing a blank page during every request.
    • Showing an empty state before the first request has completed.
    • Treating an API failure as an empty result.
    • Replacing existing data unnecessarily during refresh.
    • Allowing duplicate submissions while a mutation is pending.

    Best practices

    Design loading, empty, error, and success states together. Use button-level pending states for mutations, preserve useful data during refresh when appropriate, and make retry behavior explicit.

    Interview questions & answers

    Interview preparation

    5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.

    Beginner

    2
    1. 1Why distinguish loading from empty?
      Beginner

      Model answer

      Loading means data is not ready yet.

      Empty means the request succeeded with zero items—showing empty too early misleads users.

    2. 2How would you make loading feedback accessible?
      Beginner

      Model answer

      Provide clear status text and live-region semantics where needed; do not communicate loading only through color or animation.

    Intermediate

    2
    1. 3What is the difference between initial loading and background refreshing?
      Intermediate

      Model answer

      Initial loading has no usable data yet.

      Background refresh keeps existing data visible while fetching a newer response.

    2. 4How should a submit button behave while a request is pending?
      Intermediate

      Model answer

      Enter a pending state that disables duplicate submission and communicates progress until success or failure.

    Advanced

    1
    1. 5How do you prevent stale API responses?
      Advanced

      Model answer

      Cancel or ignore obsolete requests when filters or params change so an older response cannot overwrite newer results.

    Practice

    Build an orders page with skeleton loading, empty state, error with retry, populated table, and a refresh indicator that does not remove existing rows.

    Ready to mark this lesson complete?Track your journey across the entire course.