Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    ⚛React

    Components and JSX

    • Why React?
    • JSX rules
    • Props
    • Rendering lists and keys
    • Conditional rendering

    State and events

    • Handling events
    • useState: a component's memory
    • Objects and arrays in state
    • Forms and controlled inputs
    • Lifting state up

    Effects and essential hooks

    • useEffect
    • Fetching data
    • useRef
    • Context: data for the whole tree
    • useReducer

    Professional React

    • Writing your own hooks
    • Performance: memo, useMemo and useCallback
    • Suspense and error boundaries
    • What's new in React 19
    • Thinking in React
    • Project: a complete to-do app
    React

    Professional React · Lesson 18 of 21

    Suspense and error boundaries

    Loading and errors at the tree level, and lazy-loading components.

    10 minutes2 games

    Instead of each component doing its own if (loading) and if (error), React offers a tree-level approach:

    • <Suspense fallback={…}> shows the fallback until everything inside finishes loading (a lazy component, or data from a framework like Next.js).
    • An error boundary catches errors thrown while rendering its subtree and shows a fallback instead of a blank page.
    React
    // A component whose code "downloads" for a second (code splitting)
    const Chart = React.lazy(() =>
      new Promise((resolve) => setTimeout(() => resolve({ default: () => <p>📊 Chart loaded</p> }), 1000))
    )
    
    class ErrorBoundary extends React.Component {
      state = { error: null }
      static getDerivedStateFromError(error) {
        return { error }
      }
      render() {
        if (this.state.error) return <p style={{ color: "crimson" }}>Something broke: {this.state.error.message}</p>
        return this.props.children
      }
    }
    
    function Buggy() {
      throw new Error("lesson data is missing")
    }
    
    function App() {
      const [show, setShow] = useState(false)
      return (
        <div>
          <Suspense fallback={<p>Loading chart…</p>}>
            <Chart />
          </Suspense>
          <button onClick={() => setShow(true)}>Render a buggy component</button>
          <ErrorBoundary>
            {show && <Buggy />}
          </ErrorBoundary>
          <p>The rest of the page keeps working.</p>
        </div>
      )
    }
    • <App>
      • <Suspense fallback="Loading…">
        • <Chart> (lazy)
      • <ErrorBoundary>
        • <Buggy> throws → boundary shows its fallback
      • <p> unaffected

    Important

    Error boundaries catch render and lifecycle errors only, not errors in event handlers or async code (use try/catch there). They still have to be class components, so most projects use the react-error-boundary library, or error.tsx files in Next.js.

    Quick question

    A button inside an error boundary throws in its onClick. What happens?

    Interview question

    What is code splitting, and how do React.lazy and Suspense help?

    Exercise

    Wrap Profile in Suspense with the fallback Loading profile…. Profile is lazy, loads after 150 ms and shows Laila · 320 xp.

    The page should show: Laila · 320 xp

    React
    const Profile = React.lazy(() =>
      new Promise((resolve) => setTimeout(() => resolve({ default: () => <p>Laila · 320 xp</p> }), 150))
    )
    
    function App() {
      return <div>Add Suspense here</div>
    }

    Learn more from

    • react.dev: <Suspense>react.dev
    • react.dev: lazyreact.dev
    • react.dev: Catching rendering errors with an error boundaryreact.dev
    • react-error-boundary on GitHubgithub.com

    Played 0 of 2 games

    Sign in to save your progress and earn XP.

    PreviousPerformance: memo, useMemo and useCallbackNextWhat's new in React 19
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.