📖 Scenario: You are building a Next.js app that fetches user data from an API. To avoid fetching the same data multiple times during a session, you want to remember (memoize) the results of previous requests.
🎯 Goal: Create a simple Next.js component that fetches user data once and reuses the cached data on subsequent renders without making new requests.
📋 What You'll Learn
Create a function to fetch user data from a mock API
Create a cache object to store fetched data
Use memoization to return cached data if available
Display the user data in a React component
💡 Why This Matters
🌍 Real World
Memoization helps avoid repeated API calls, improving app speed and reducing server load.
💼 Career
Understanding memoization and React hooks is essential for building efficient Next.js applications.
Progress0 / 4 steps
1
Set up the user data fetch function
Create an async function called fetchUserData that returns a hardcoded object with id: 1 and name: 'Alice'.
NextJS
Hint
Use an async function that returns a simple object with the exact keys and values.
2
Create a cache object for memoization
Create a constant object called cache initialized as an empty object {} to store cached user data.
NextJS
Hint
Use const cache = {} to create an empty object for caching.
3
Implement memoized fetch function
Create an async function called getUserData that checks if cache.user exists. If yes, return it. Otherwise, call fetchUserData(), store the result in cache.user, and return it.
NextJS
Hint
Use an if statement to check cache, await the fetch function if needed, then store and return the data.
4
Create a React component to display user data
Create a React functional component called UserProfile that uses useState and useEffect to call getUserData() once on mount and display the user's name inside a <div>.
NextJS
Hint
Use useState to hold user data and useEffect to fetch it once. Show loading text if data is not ready.
Practice
(1/5)
1. What is the main purpose of request memoization in Next.js?
easy
A. To increase the number of server requests
B. To save repeated fetches by remembering previous results
C. To disable caching for all requests
D. To make fetch requests slower
Solution
Step 1: Understand request memoization concept
Request memoization stores the results of previous fetches to avoid repeating the same network calls.
Step 2: Identify the effect on performance
By remembering previous results, it reduces server load and speeds up response times.
Final Answer:
To save repeated fetches by remembering previous results -> Option B
Quick Check:
Request memoization = Save repeated fetches [OK]
Hint: Memoization means remembering past results to avoid repeats [OK]
Common Mistakes:
Thinking memoization increases server requests
Confusing memoization with disabling cache
Assuming memoization slows down fetches
2. Which of the following is the correct way to memoize a fetch function in Next.js using the built-in cache?
easy
A. const cachedFetch = fetch.cache('/api/data')
B. const cachedFetch = async cache(() => fetch('/api/data'))
C. const cachedFetch = cache(async () => await fetch('/api/data'))
D. const cachedFetch = cache.fetch('/api/data')
Solution
Step 1: Recall Next.js cache usage
Next.js provides a cache function that wraps async functions to memoize their results.
Step 2: Check correct syntax
The correct syntax is cache(async () => await fetch(...)), which matches const cachedFetch = cache(async () => await fetch('/api/data')).