cache
cache lets you cache the result of a data fetch or computation.
const cachedFn = cache(fn);Reference
cache(fn)
Call cache outside of any components to create a version of the function with caching.
import {cache} from 'react';
import calculateMetrics from 'lib/metrics';
const getMetrics = cache(calculateMetrics);
function Chart({data}) {
const report = getMetrics(data);
// ...
}When getMetrics is first called with data, getMetrics will call calculateMetrics(data) and store the result in cache. If getMetrics is called again with the same data, it will return the cached result instead of calling calculateMetrics(data) again.
Parameters
fn: The function you want to cache results for.fncan take any arguments and return any value.
Returns
cache returns a cached version of fn with the same type signature. It does not call fn in the process.
When calling cachedFn with given arguments, it first checks if a cached result exists in the cache. If a cached result exists, it returns the result. If not, it calls fn with the arguments, stores the result in the cache, and returns the result. The only time fn is called is when there is a cache miss.
Caveats
- React will invalidate the cache for all memoized functions for each server request.
- Each call to
cachecreates a new function. This means that callingcachewith the same function multiple times will return different memoized functions that do not share the same cache. cachedFnwill also cache errors. Iffnthrows an error for certain arguments, it will be cached, and the same error is re-thrown whencachedFnis called with those same arguments.cacheis for use in Server Components only.
Usage
Cache an expensive computation
Use cache to skip duplicate work.
import {cache} from 'react';
import calculateUserMetrics from 'lib/user';
const getUserMetrics = cache(calculateUserMetrics);
function Profile({user}) {
const metrics = getUserMetrics(user);
// ...
}
function TeamReport({users}) {
for (let user in users) {
const metrics = getUserMetrics(user);
// ...
}
// ...
}If the same user object is rendered in both Profile and TeamReport, the two components can share work and only call calculateUserMetrics once for that user.
Assume Profile is rendered first. It will call getUserMetrics, and check if there is a cached result. Since it is the first time getUserMetrics is called with that user, there will be a cache miss. getUserMetrics will then call calculateUserMetrics with that user and write the result to cache.
When TeamReport renders its list of users and reaches the same user object, it will call getUserMetrics and read the result from cache.
If calculateUserMetrics can be aborted by passing an AbortSignal, you can use cacheSignal() to cancel the expensive computation if React has finished rendering. calculateUserMetrics may already handle cancellation internally by using cacheSignal directly.