cache

Memoizes the result of a data fetch or computation across Server Components.

Since React 19 Spec ↗

Syntax

const cached = cache(fn)

Parameters

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

See also