Building a Fallback UI with React Suspense and Error Boundary
📖 Scenario: You are building a user profile page in a React app. Sometimes the profile data takes time to load or might fail to load. To keep the app friendly, you want to show a loading message while waiting and an error message if loading fails.
🎯 Goal: Create a React component that uses Suspense to show a loading fallback UI and an ErrorBoundary component to catch errors and show an error fallback UI.
📋 What You'll Learn
Create a simple React component called UserProfile that simulates loading user data.
Create a variable called isLoading to control loading state.
Use React.Suspense with a fallback UI showing <div>Loading user data...</div>.
Create an ErrorBoundary component that shows <div>Failed to load user data.</div> on error.
Wrap UserProfile inside ErrorBoundary and Suspense in the main app component.
💡 Why This Matters
🌍 Real World
Fallback UI patterns improve user experience by showing helpful messages during loading or errors, common in real web apps fetching data.
💼 Career
Understanding Suspense and ErrorBoundary is essential for React developers to build resilient and user-friendly interfaces.
Progress0 / 4 steps
1
Create the UserProfile component with loading simulation
Create a React functional component called UserProfile that returns <div>User data loaded.</div>. Also create a variable called isLoading and set it to true.
React
Hint
Start by declaring isLoading as true. Then create a function UserProfile that returns a div with the text 'User data loaded.'
2
Add Suspense with loading fallback UI
Create a React component called App that uses React.Suspense to wrap UserProfile. Set the fallback prop of Suspense to <div>Loading user data...</div>.
React
Hint
Wrap UserProfile inside React.Suspense and provide a fallback div with the loading message.
3
Create an ErrorBoundary component for error fallback UI
Create a class component called ErrorBoundary that extends React.Component. It should have state { hasError: false }. Implement static getDerivedStateFromError() to set hasError to true. In render(), if hasError is true, return <div>Failed to load user data.</div>, else return this.props.children.
React
Hint
Create a class component with error state. Use getDerivedStateFromError to update state on error. Show error message if error happened, else show children.
4
Wrap UserProfile with ErrorBoundary and Suspense in App
Update the App component to wrap UserProfile inside ErrorBoundary and then inside React.Suspense with the same fallback UI.
React
Hint
Wrap UserProfile inside ErrorBoundary, then wrap that inside React.Suspense with the loading fallback.
Practice
(1/5)
1. What is the main purpose of a fallback UI in React applications?
easy
A. To speed up the rendering of all components
B. To permanently replace the main UI with a different design
C. To disable user interactions during loading
D. To show a temporary interface while content is loading or an error occurs
Solution
Step 1: Understand fallback UI role
Fallback UI is used to improve user experience by showing temporary content during loading or errors.
Step 2: Differentiate from permanent UI changes
Fallback UI is temporary, not a permanent replacement or disabling interaction.
Final Answer:
To show a temporary interface while content is loading or an error occurs -> Option D
B. The component will crash because user is undefined
C. The fallback UI <LoadingSpinner /> is shown twice when user is null
D. The fallback UI is missing for loading state
Solution
Step 1: Analyze conditional rendering logic
The first condition shows LoadingSpinner if user is falsy.
Step 2: Check second condition
The second condition shows LoadingSpinner if user is falsy, causing it to appear twice when user is falsy.
Final Answer:
The fallback UI <LoadingSpinner /> is shown twice when user is null -> Option C
Quick Check:
Duplicate fallback UI rendering due to conflicting conditions [OK]
Hint: Check if fallback UI appears more than once [OK]
Common Mistakes:
Assuming fallback UI shows only once
Confusing truthy and falsy conditions
Ignoring duplicated components in render
5. You want to create a fallback UI that shows a spinner while data loads, an error message if loading fails, and the content once loaded. Which React pattern best handles this cleanly and accessibly?
hard
A. Use separate components for Loading, Error, and Content, and conditionally render them in order
B. Use nested ternary operators inside JSX to handle loading, error, and content states
C. Render all three components simultaneously and hide/show with CSS
D. Use a single state variable with string values and switch-case inside JSX
Solution
Step 1: Evaluate clarity and accessibility
Separate components for each state improve readability and accessibility.
Step 2: Compare with other options
Nested ternaries (B) reduce readability; rendering all simultaneously (C) wastes resources; switch-case in JSX (D) is less common and can be confusing.
Final Answer:
Use separate components for Loading, Error, and Content, and conditionally render them in order -> Option A
Quick Check:
Separate components for fallback states = Use separate components for Loading, Error, and Content, and conditionally render them in order [OK]
Hint: Separate components for each state improve clarity [OK]
Common Mistakes:
Using nested ternaries that are hard to read
Rendering all states at once and hiding with CSS
Using complex switch-case inside JSX
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.