0
0
NextJSframework~10 mins

Mocking data fetching in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Mocking data fetching
Start Component Render
Call fetch function
Is mock enabled?
YesReturn mock data
Return real data
Use data in component
Render UI with data
End
The component starts rendering, calls a fetch function that checks if mock data should be used. If yes, it returns mock data immediately; otherwise, it fetches real data. Then the component uses this data to render the UI.
Execution Sample
NextJS
export async function fetchData(mock = false) {
  if (mock) {
    return { name: 'Mock User', age: 30 };
  }
  const res = await fetch('/api/user');
  return res.json();
}
This function fetches user data. If mock is true, it returns fake data immediately; otherwise, it fetches real data from an API.
Execution Table
StepActionmock parameterData ReturnedNotes
1Call fetchData(mock=false)falseCalls fetch('/api/user')No mock, real fetch starts
2Await fetch responsefalseResponse objectWaiting for real data
3Parse JSON from responsefalse{ name: 'Real User', age: 25 }Real data received
4Return data to componentfalse{ name: 'Real User', age: 25 }Component uses real data
5Call fetchData(mock=true)true{ name: 'Mock User', age: 30 }Mock enabled, returns mock data immediately
6Return data to componenttrue{ name: 'Mock User', age: 30 }Component uses mock data
7End--No further actions
💡 Execution stops after returning data to the component either from mock or real fetch.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
mockfalsefalsefalsetruetrue
dataundefinedfetch response pending{ name: 'Real User', age: 25 }{ name: 'Mock User', age: 30 }{ name: 'Mock User', age: 30 }
Key Moments - 2 Insights
Why does the function return mock data immediately when mock=true?
Because the if condition at step 5 detects mock=true and returns the mock object without waiting for any fetch, as shown in execution_table row 5.
What happens when mock=false in terms of data fetching?
The function performs a real fetch call to the API, waits for the response, parses JSON, and then returns real data, as detailed in execution_table rows 1 to 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what data is returned at step 3?
A{ name: 'Real User', age: 25 }
B{ name: 'Mock User', age: 30 }
Cfetch response object
Dundefined
💡 Hint
Check the 'Data Returned' column at step 3 in the execution_table.
At which step does the function return mock data?
AStep 2
BStep 5
CStep 4
DStep 6
💡 Hint
Look for the step where mock=true and mock data is returned in the execution_table.
If mock parameter is always false, what changes in the variable_tracker?
Adata becomes mock data at final
Bmock changes to true after step 3
Cmock stays false, data never becomes mock data
Dmock toggles between true and false
💡 Hint
Refer to the 'mock' variable values in variable_tracker when mock=false.
Concept Snapshot
Mocking data fetching in Next.js:
- Use a parameter or flag to decide mock vs real fetch
- If mock enabled, return fake data immediately
- Else, fetch real data asynchronously
- Component uses returned data to render UI
- Helps test UI without real backend calls
Full Transcript
This visual execution shows how a Next.js data fetching function can return either mock data or real data based on a parameter. When the mock flag is true, the function returns a predefined object immediately, skipping any network call. When false, it performs a real fetch to an API endpoint, waits for the response, parses JSON, and returns the real data. The component then uses this data to render the UI. This approach helps developers test UI components without relying on backend availability or network conditions.