React · Senior · Question 1 of 75
Why is React's cache() useful in Server Components?
- ✓Correct answer: It deduplicates calls with the same arguments within a single request
- 2It keeps a function's result across requests from different users forever
- 3It caches a component's JSX in the browser between page transitions
Explanation
const getUser = cache(async (id) => db.user.find(id)) - several components in one server render can call getUser(1), and the database query runs once. The cache lives only within the request, so different users' data doesn't mix. Cross-request caching in Next.js is done with 'use cache'.