0
0
NextJSframework~20 mins

Request memoization in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Request Memoization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this memoized fetch in a Next.js Server Component?

Consider this Next.js Server Component that fetches user data with memoization. What will be rendered if the component is called twice with the same userId?

NextJS
import { cache } from 'react';

const fetchUser = cache(async (userId) => {
  console.log('Fetching user', userId);
  const res = await fetch(`https://api.example.com/users/${userId}`);
  return res.json();
});

export default async function User({ userId }) {
  const user = await fetchUser(userId);
  return <div>{user.name}</div>;
}
AThe console logs 'Fetching user' once, but the user name is rendered only once.
BThe console logs 'Fetching user' once, and the user name is rendered twice.
CThe console logs nothing, and the user name is rendered twice.
DThe console logs 'Fetching user' twice, and the user name is rendered twice.
Attempts:
2 left
💡 Hint

Think about how cache memoizes the function calls with the same arguments.

📝 Syntax
intermediate
2:00remaining
Which option correctly memoizes a fetch request in Next.js using React's cache?

Choose the code snippet that correctly memoizes a fetch request using cache from React in Next.js.

A
import { cache } from 'react';
const getData = cache(async (id) =&gt; {
  const res = await fetch(`/api/data/${id}`);
  return res.json();
});
B
import { memo } from 'react';
const getData = memo(async (id) =&gt; {
  const res = await fetch(`/api/data/${id}`);
  return res.json();
});
C
import { cache } from 'next/navigation';
const getData = cache(async (id) =&gt; {
  const res = await fetch(`/api/data/${id}`);
  return res.json();
});
D
import { cache } from 'react';
const getData = async (id) =&gt; {
  const res = await fetch(`/api/data/${id}`);
  return res.json();
};
Attempts:
2 left
💡 Hint

Remember the correct import source for cache in React 18+ and Next.js 14+.

🔧 Debug
advanced
2:00remaining
Why does this memoized fetch not cache results as expected?

Given this code, why does the fetch happen on every call despite using cache?

NextJS
import { cache } from 'react';

export default async function Page() {
  const fetchData = cache(async (url) => {
    const res = await fetch(url);
    return res.json();
  });
  const data1 = await fetchData('https://api.example.com/data');
  const data2 = await fetchData('https://api.example.com/data');
  return <div>{data1.value} - {data2.value}</div>;
}
ABecause the URL string is not a stable reference, so cache treats each call as different.
BBecause fetch is not memoized internally, so cache does not apply.
CBecause the fetchData function is redefined on every render, losing memoization.
DBecause the cache function only memoizes synchronous functions, not async ones.
Attempts:
2 left
💡 Hint

Consider where the fetchData function is declared and how cache memoizes.

state_output
advanced
2:00remaining
What is the value of count after these memoized fetch calls?

Analyze this code snippet. What will be the final value of count after both calls?

NextJS
import { cache } from 'react';

let count = 0;
const fetchData = cache(async (id) => {
  count++;
  return { id };
});

async function run() {
  await fetchData(1);
  await fetchData(1);
  await fetchData(2);
}

run();
Acount is 2 because fetchData is called twice with unique arguments.
Bcount is 3 because fetchData is called three times.
Ccount is 1 because fetchData is memoized and called once.
Dcount is 0 because count++ is never executed.
Attempts:
2 left
💡 Hint

Think about how cache memoizes calls with different arguments.

🧠 Conceptual
expert
3:00remaining
Which statement best describes request memoization with React's cache in Next.js?

Choose the most accurate description of how cache memoizes requests in Next.js Server Components.

A<p><code>cache</code> memoizes functions globally but does not consider function arguments, so it caches only one result per function.</p>
B<p><code>cache</code> memoizes only client-side requests and resets on every server render.</p>
C<p><code>cache</code> memoizes requests by storing results in localStorage for offline use.</p>
D<p><code>cache</code> memoizes function calls by argument identity and persists results across server renders, avoiding repeated fetches for the same inputs.</p>
Attempts:
2 left
💡 Hint

Recall how cache works in server components and argument-based memoization.