Thuta Learning
React
AdvancedWeb Developmentintermediate

Suspense, Lazy Loading & Error Boundaries

ReactLesson 21

What you'll walk away with

  • Explain the render, state, and interaction behavior of Suspense, Lazy Loading & Error Boundaries
  • Test loading, error, accessibility, and lifecycle cases
  • Write maintainable, testable, and performant React

Build Suspense, Lazy Loading & Error Boundaries as a user flow with pending, success, empty, error, retry, cancellation, and navigation recovery states. Give every route or asynchronous boundary intentional fallback and recovery behavior.

Build a Complete Mental Model

Build Suspense, Lazy Loading & Error Boundaries as a user flow with pending, success, empty, error, retry, cancellation, and navigation recovery states. Give every route or asynchronous boundary intentional fallback and recovery behavior.

Apply It in Production React

Test an original Suspense, Lazy Loading & Error Boundaries example with empty/loading/error states, keyboard-only interaction, a slow network, rapid repeated events, and component unmount/remount cases. Verify behavior with the browser console, React DevTools, tests, and profiler where relevant.

After This Lesson

jsx
import { Component, lazy, Suspense } from 'react';

const Reports = lazy(() => import('./Reports.jsx'));

class ErrorBoundary extends Component {
  state = { failed: false };
  static getDerivedStateFromError() { return { failed: true }; }
  render() {
    return this.state.failed ? <p role="alert">Could not load reports.</p> : this.props.children;
  }
}

export default function App() {
  return <ErrorBoundary>
    <Suspense fallback={<p>Loading reports…</p>}>
      <Reports />
    </Suspense>
  </ErrorBoundary>;
}
You should see
Users see a loading fallback, then reports or a recoverable error message.

Try It Yourself

Test an original Suspense, Lazy Loading & Error Boundaries example with empty/loading/error states, keyboard-only interaction, a slow network, rapid repeated events, and component unmount/remount cases. Verify behavior with the browser console, React DevTools, tests, and profiler where relevant.

State and Effect Warning

Assuming one happy-path render proves every stale-state, repeated-effect, accessibility, loading, and error path.

SuspenseReact

Easy traps

  • Assuming one happy-path render proves every stale-state, repeated-effect, accessibility, loading, and error path.
  • Mutating props or state, storing redundant derived state, or using an Effect to complicate data flow when no external system exists.

Hands-on Exercise

Test an original Suspense, Lazy Loading & Error Boundaries example with empty/loading/error states, keyboard-only interaction, a slow network, rapid repeated events, and component unmount/remount cases. Verify behavior with the browser console, React DevTools, tests, and profiler where relevant.

You'll know it worked when: Users see a loading fallback, then reports or a recoverable error message.

Suspense, Lazy Loading & Error Boundaries | Thuta Learning