Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to show a fallback UI while loading data in a React component.
React
function DataLoader() {
const [loading, setLoading] = React.useState(true);
return (
<div>
{loading ? [1] : <p>Data loaded!</p>}
</div>
);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Showing 'Done' or 'Ready' before data is loaded.
Leaving the fallback UI blank.
✗ Incorrect
The fallback UI during loading should inform the user that data is loading, so '
Loading...
' is the correct choice.2fill in blank
mediumComplete the code to use React Suspense with a fallback UI.
React
const LazyComponent = React.lazy(() => import('./LazyComponent')); function App() { return ( <React.Suspense fallback=[1]> <LazyComponent /> </React.Suspense> ); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a message that indicates completion instead of loading.
Not providing a fallback UI.
✗ Incorrect
The fallback prop in Suspense shows UI while the lazy component loads, so a loading message is correct.
3fill in blank
hardFix the error in the fallback UI code by completing the blank.
React
function Profile({ userId }) {
const [user, setUser] = React.useState(null);
React.useEffect(() => {
fetch(`/api/user/${userId}`)
.then(res => res.json())
.then(data => setUser(data));
}, [userId]);
if (!user) {
return [1];
}
return <div>{user.name}</div>;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Showing 'User loaded' before data is ready.
Returning null or empty UI without feedback.
✗ Incorrect
When user data is not yet loaded, showing a loading message is the correct fallback UI.
4fill in blank
hardFill both blanks to create a fallback UI with error handling using React error boundaries.
React
class ErrorBoundary extends React.Component { constructor(props) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error) { return { hasError: [1] }; } render() { if (this.state.hasError) { return [2]; } return this.props.children; } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting hasError to false on error.
Showing a success message instead of error fallback.
✗ Incorrect
When an error occurs, hasError should be true and the fallback UI should show an error message.
5fill in blank
hardFill all three blanks to create a Suspense fallback UI with lazy loading and error boundary.
React
const LazyWidget = React.lazy(() => import('./Widget')); function WidgetLoader() { return ( <ErrorBoundary fallback=[1]> <React.Suspense fallback=[2]> <LazyWidget /> </React.Suspense> </ErrorBoundary> ); } function ErrorBoundary({ fallback, children }) { const [hasError, setHasError] = React.useState(false); React.useEffect(() => { // Simulate error handling }, []); if (hasError) { return [3]; } return children; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing loading and error messages in wrong places.
Not providing fallback UI for error boundary.
✗ Incorrect
The ErrorBoundary fallback and the error UI inside it should show an error message, while Suspense fallback shows loading.