cache
Memoizes the result of a data fetch or computation across Server Components.
Syntax
const cached = cache(fn) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
fn | function | Yes | The function whose results should be cached by argument. |
Returns
function — A wrapped function that caches results per arguments within a render.
Examples
import { cache } from 'react';
const getUser = cache(async (id) => {
return db.user.findUnique({ where: { id } });
});
async function Profile({ id }) {
const user = await getUser(id);
return <h1>{user.name}</h1>;
}
Notes
Intended for React Server Components. The cache is scoped to a single server
request, so calling the wrapped function with the same arguments returns the
same result and avoids duplicate work across components. Not for use in
Client Components.