📖 Scenario: You are building a simple Next.js app that shows a list of books. Instead of fetching real data from the internet, you will create mock data inside your app to simulate fetching.
🎯 Goal: Build a Next.js component that displays a list of book titles using mocked data fetching.
📋 What You'll Learn
Create a mock data array of books
Add a configuration variable for the number of books to show
Use a React component with useState and useEffect to simulate fetching the mock data
Render the list of book titles in the component
💡 Why This Matters
🌍 Real World
Mocking data fetching helps you build and test UI components without needing a real backend or internet connection.
💼 Career
Understanding how to mock data is important for frontend developers to create reliable and fast prototypes and tests.
Progress0 / 4 steps
1
Create mock data array
Create a constant called mockBooks that is an array of objects. Each object should have id and title properties. Use these exact entries: { id: 1, title: 'The Great Gatsby' }, { id: 2, title: '1984' }, { id: 3, title: 'To Kill a Mockingbird' }.
NextJS
Hint
Use const to declare mockBooks as an array of objects with id and title.
2
Add configuration for number of books to show
Create a constant called booksToShow and set it to 2. This will control how many books to display from the mock data.
NextJS
Hint
Use const booksToShow = 2; to set the number of books to display.
3
Create React component with mock fetching
Create a React functional component called BookList. Inside it, use useState to create a state variable books initialized to an empty array. Use useEffect to simulate fetching by setting books to the first booksToShow items from mockBooks when the component mounts.
NextJS
Hint
Use useState to hold books and useEffect with an empty dependency array to set books once on mount.
4
Render the list of book titles
Inside the BookList component's return, render an unordered list <ul>. Use books.map to create a list item <li> for each book showing its title. Add a key prop to each <li> using the book's id.
NextJS
Hint
Use books.map inside the return to create <li> elements with keys and titles.
Practice
(1/5)
1. What is the main purpose of mocking data fetching in a Next.js app?
easy
A. To encrypt data before sending it
B. To speed up the production server
C. To permanently store data on the client
D. To simulate data retrieval without needing a real server
Solution
Step 1: Understand mocking concept
Mocking means pretending to get data, so you don't rely on a real server.
Step 2: Identify purpose in Next.js
This helps build and test UI without waiting for real backend responses.
Final Answer:
To simulate data retrieval without needing a real server -> Option D
Quick Check:
Mocking = Simulate data fetching [OK]
Hint: Mocking means faking data to test without a real server [OK]
Common Mistakes:
Thinking mocking speeds up production
Confusing mocking with data storage
Assuming mocking encrypts data
2. Which of the following is the correct way to create a mock async data fetch function in Next.js?
easy
A. async function fetchData() { return new Promise(resolve => setTimeout(() => resolve({ data: [] }), 1000)); }
async function fetchData() { return new Promise(resolve => setTimeout(() => resolve({ data: [] }), 1000)); } correctly returns a Promise that resolves after 1 second with data.
Step 2: Identify incorrect patterns
const fetchData = () => { return { data: [] }; } is synchronous, C does not return a Promise, D returns timeout ID, not data.
Final Answer:
async function fetchData() { return new Promise(resolve => setTimeout(() => resolve({ data: [] }), 1000)); } -> Option A
D. async function fetchUsers() {
return new Promise(resolve => setTimeout(() => resolve([{ name: "", id: 1 }, { name: "Eve", id: 2 }]), 1000));
}
Solution
Step 1: Identify filtering and delay
async function fetchUsers() {
const users = [{ name: "", id: 1 }, { name: "Eve", id: 2 }];
return new Promise(resolve => setTimeout(() => resolve(users.filter(u => u.name)), 1000));
} filters out users with empty names and returns a Promise resolving after 1 second.
Step 2: Check other options
A returns unfiltered data with delay, B performs filtering but is synchronous and non-async without delay, C returns filtered data synchronously without delay.