Challenge - 5 Problems
Loading State Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
Identify the loading state output in a Next.js component
Consider this Next.js component that fetches user data asynchronously. What will the component render while the data is loading?
NextJS
import { useState, useEffect } from 'react'; export default function UserProfile() { const [user, setUser] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { setTimeout(() => { setUser({ name: 'Alice' }); setLoading(false); }, 2000); }, []); if (loading) return <p>Loading user data...</p>; return <p>User: {user.name}</p>; }
Attempts:
2 left
💡 Hint
Think about what the component returns when the loading state is true.
✗ Incorrect
While the data is loading, the component returns the loading message. Only after loading is false does it show the user name.
❓ state_output
intermediate2:00remaining
What is the final rendered output after data fetch?
Given this Next.js component, what will be the rendered output after 3 seconds?
NextJS
import { useState, useEffect } from 'react'; export default function DataFetcher() { const [data, setData] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { setTimeout(() => { setData('Hello World'); setLoading(false); }, 3000); }, []); if (loading) return <p>Loading...</p>; return <p>Data: {data}</p>; }
Attempts:
2 left
💡 Hint
After the timeout, loading becomes false and data is set.
✗ Incorrect
After 3 seconds, loading is false and data is 'Hello World', so the component renders the data.
🔧 Debug
advanced2:30remaining
Why does the loading state never end in this Next.js component?
Examine this component. Why does it keep showing the loading message forever?
NextJS
import { useState, useEffect } from 'react'; export default function BrokenLoader() { const [loading, setLoading] = useState(true); useEffect(() => { fetch('/api/data') .then(res => res.json()) .then(data => { // Missing setLoading(false) here }); }, []); if (loading) return <p>Loading...</p>; return <p>Data loaded</p>; }
Attempts:
2 left
💡 Hint
Check what happens after data is fetched.
✗ Incorrect
The loading state never changes because setLoading(false) is never called after data fetch completes.
📝 Syntax
advanced2:00remaining
Which option causes a syntax error in a Next.js loading state component?
Identify the option that will cause a syntax error when used in a Next.js component managing loading state.
Attempts:
2 left
💡 Hint
Look for missing punctuation between statements.
✗ Incorrect
Option A is missing a semicolon or line break between useState and if statement, causing a syntax error.
🧠 Conceptual
expert3:00remaining
What is the best way to handle loading states for server-side data fetching in Next.js 14 App Router?
In Next.js 14 using the App Router, which approach correctly handles loading states for server-side data fetching in a client component?
Attempts:
2 left
💡 Hint
Next.js 14 encourages server components and React Suspense for loading states.
✗ Incorrect
React Suspense with server components allows declarative loading UI with fallback while data loads server-side.