Loading States
Loading States cover explicit async status models, skeletons, empty states, initial loading vs background refresh, flicker, and accessibility.
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:
idle -> loading -> success-> error
For richer experiences, distinguish initial loading, refreshing, success with data, empty success, and failure.
Explicit status model
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
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
1Why distinguish loading from empty?
BeginnerModel answer
Loading means data is not ready yet.
Empty means the request succeeded with zero items—showing empty too early misleads users.
2How would you make loading feedback accessible?
BeginnerModel answer
Provide clear status text and live-region semantics where needed; do not communicate loading only through color or animation.
Intermediate
3What is the difference between initial loading and background refreshing?
IntermediateModel answer
Initial loading has no usable data yet.
Background refresh keeps existing data visible while fetching a newer response.
4How should a submit button behave while a request is pending?
IntermediateModel answer
Enter a pending state that disables duplicate submission and communicates progress until success or failure.
Advanced
5How do you prevent stale API responses?
AdvancedModel 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.