Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Loading states for data
📖 Scenario: You are building a simple Next.js app that fetches user data from an API. To improve user experience, you want to show a loading message while the data is being fetched.
🎯 Goal: Create a Next.js functional component that fetches user data and shows a loading message until the data is ready.
📋 What You'll Learn
Create a React functional component named UserList.
Use useState to hold the user data and loading state.
Use useEffect to fetch data from https://jsonplaceholder.typicode.com/users when the component mounts.
Show a Loading... message while fetching data.
Display the list of user names after data is loaded.
💡 Why This Matters
🌍 Real World
Loading states are common in web apps to inform users that data is being fetched, improving user experience.
💼 Career
Understanding loading states and data fetching is essential for frontend developers working with React and Next.js.
Progress0 / 4 steps
1
Set up the component and state variables
Create a React functional component called UserList. Inside it, create two state variables using useState: users initialized to an empty array, and loading initialized to true.
NextJS
Hint
Use const [users, setUsers] = useState([]); and const [loading, setLoading] = useState(true); inside the component.
2
Add data fetching with useEffect
Inside the UserList component, add a useEffect hook that fetches data from https://jsonplaceholder.typicode.com/users. After fetching, update users with the data and set loading to false.
NextJS
Hint
Use useEffect with an empty dependency array to fetch data once on mount. Use fetch and update states inside then.
3
Render loading message conditionally
Inside the UserList component's return statement, add JSX that shows a <p>Loading...</p> message if loading is true. Otherwise, show the list of user names.
NextJS
Hint
Use a ternary operator inside JSX to show <p>Loading...</p> when loading is true, else map over users to show names.
4
Add accessibility and export the component
Ensure the ul element has an aria-label attribute with the value "User list" for accessibility. Confirm the component is exported as default.
NextJS
Hint
Add aria-label="User list" to the ul element for screen readers.
Practice
(1/5)
1. What is the main purpose of a loading state in a Next.js component?
easy
A. To speed up the data fetching process automatically
B. To show users that data is being fetched and the app is working
C. To permanently hide the data from users
D. To prevent users from clicking buttons
Solution
Step 1: Understand loading state purpose
Loading states inform users that data is being fetched and the app is busy.
Step 2: Compare options
Only To show users that data is being fetched and the app is working correctly describes this purpose; others are incorrect or unrelated.
Final Answer:
To show users that data is being fetched and the app is working -> Option B
Quick Check:
Loading state = user feedback [OK]
Hint: Loading states show progress to users [OK]
Common Mistakes:
Thinking loading states speed up data fetching
Confusing loading state with error state
Ignoring user feedback during data fetch
2. Which of the following is the correct way to declare a loading state using React hooks in a Next.js component?
easy
A. const loading = useState(false);
B. let loading = true;
C. var loading = useState(true);
D. const [loading, setLoading] = useState(false);
Solution
Step 1: Identify correct useState syntax
useState returns an array with state and setter, so destructuring is needed.
Hint: Start loading as true to show loading UI immediately [OK]
Common Mistakes:
Setting loading false initially hides loading UI
Ignoring initial state impact on render
Misplacing setLoading calls
5. You want to show a loading spinner while fetching data and then display the data or an error message if fetching fails. Which approach correctly handles loading, success, and error states in a Next.js component?
hard
A. Use three state variables: loading (boolean), data (object|null), error (string|null); update them accordingly during fetch lifecycle
B. Use only one state variable for data and show loading until data is not null
C. Use loading state only and ignore errors to simplify code
D. Fetch data outside component and pass as props to avoid loading states
Solution
Step 1: Identify states needed for full fetch lifecycle
Loading, data, and error states cover all cases: waiting, success, and failure.
Step 2: Evaluate options
Use three state variables: loading (boolean), data (object|null), error (string|null); update them accordingly during fetch lifecycle uses all three states properly; others miss error handling or loading feedback.
Final Answer:
Use three state variables: loading (boolean), data (object|null), error (string|null); update them accordingly during fetch lifecycle -> Option A
Quick Check:
Loading + data + error states = robust UI [OK]
Hint: Track loading, data, and error separately for clear UI states [OK]
Common Mistakes:
Ignoring error state leads to silent failures
Using only data state misses loading feedback
Fetching data outside component loses dynamic loading UI