React · Senior · Question 1 of 75

Why is React's cache() useful in Server Components?

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'.

Further reading

Problem with a question

React · Senior · question 1

Test yourself

Take the React · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz