Server Components and Functions

cache

Wraps a function for memoized reuse in a React Server Component render context. Use the same wrapper across consumers; this is not a persistent cross-request cache or an authorization mechanism. Errors can also be cached.

Formal Syntax & Structure

react syntaxreact
cache(fn)

Parameters & Attributes

NameTypeDescription
fnfunctionFunction whose result is reused for matching arguments.

Return Value

function

Memoized wrapper with the original call signature.

Working Example

Example Snippetreact
import { cache } from 'react';const getLabel = cache(id => `Tool ${id}`);export default function Page() {  return <p>{getLabel(7)} / {getLabel(7)}</p>;}
React Reference