React cache

A React function that wraps another function so repeated calls with the same arguments during one server request reuse the first result.

In more detail

Wrap database helpers such as getUser in cache so several Server Components can call them without extra queries. The cache is cleared after each request, so it never serves one user's data to another.

Example

import { cache } from 'react';
export const getUser = cache(async (id: string) => db.user.findUnique({ where: { id } }));

Related terms

Taught in

References