cache – React
cache – React
v19
cache
Reference
cache(fn)
Usage
Troubleshooting
My memoized function still runs even though I’ve called it with the
same arguments
Reference
cache(fn)
https://react.dev/reference/react/cache 1/13
20/02/2025, 09:07 cache – React
function Chart({data}) {
const report = getMetrics(data);
// ...
}
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. fn can take any
arguments and return any value.
Returns
cache returns a cached version of fn with the same type signature. It does
Note
https://react.dev/reference/react/cache 2/13
20/02/2025, 09:07 cache – React
Caveats
React will invalidate the cache for all memoized functions for each server
request.
Each call to cache creates a new function. This means that calling cache
with the same function multiple times will return different memoized
functions that do not share the same cache.
cachedFn will also cache errors. If fn throws an error for certain
arguments, it will be cached, and the same error is re-thrown when
cachedFn is called with those same arguments.
Usage
function Profile({user}) {
const metrics = getUserMetrics(user) ;
// ...
}
function TeamReport({users}) {
for (let user in users) {
https://react.dev/reference/react/cache 3/13
20/02/2025, 09:07 cache – React
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 .
When TeamReport renders its list of users and reaches the same user
object, it will call getUserMetrics and read the result from cache.
Pitfall
caches.
// Temperature.js
import {cache} from 'react';
import {calculateWeekReport} from './report';
https://react.dev/reference/react/cache 4/13
20/02/2025, 09:07 cache – React
// Precipitation.js
import {cache} from 'react';
import {calculateWeekReport} from './report';
up. If both components render for the same cityData , they will do
duplicate work to call calculateWeekReport .
// getWeekReport.js
import {cache} from 'react';
import {calculateWeekReport} from './report';
// Temperature.js
https://react.dev/reference/react/cache 5/13
20/02/2025, 09:07 cache – React
// Precipitation.js
import getWeekReport from './getWeekReport';
Note
Preload data
By caching a long-running data fetch, you can kick off asynchronous work
prior to rendering the component.
https://react.dev/reference/react/cache 7/13
20/02/2025, 09:07 cache – React
function Page({id}) {
// ✅ Good: start fetching the user data
getUser(id) ;
// ... some computational work
return (
<>
<Profile id={id} />
</>
);
}
When rendering Page , the component calls getUser but note that it
doesn’t use the returned data. This early getUser call kicks off the
asynchronous database query that occurs while Page is doing other
computational work and rendering children.
When rendering Profile , we call getUser again. If the initial getUser call
has already returned and cached the user data, when Profile asks and
waits for this data , it can simply read from the cache without requiring
another remote procedure call. If the initial data request hasn’t been
completed, preloading data in this pattern reduces delay in data-fetching.
DEEP DIVE
Show Details
Pitfall
the cache.
https://react.dev/reference/react/cache 9/13
20/02/2025, 09:07 cache – React
DEEP DIVE
Show Details
Troubleshooting
If none of the above apply, it may be a problem with how React checks if
something exists in cache.
If your arguments are not primitives (ex. objects, functions, arrays), ensure
you’re passing the same object reference.
When calling a memoized function, React will look up the input arguments to
see if a result is already cached. React will use shallow equality of the
arguments to determine if there is a cache hit.
https://react.dev/reference/react/cache 10/13
20/02/2025, 09:07 cache – React
function MapMarker(props) {
// 🚩 Wrong: props is an object that changes every render.
const length = calculateNorm(props);
// ...
}
function App() {
return (
<>
<MapMarker x={10} y={10} z={10} />
<MapMarker x={10} y={10} z={10} />
</>
);
}
In this case the two MapMarker s look like they’re doing the same work and
calling calculateNorm with the same value of {x: 10, y: 10, z:10} . Even
though the objects contain the same values, they are not the same object
reference as each component creates its own props object.
React will call Object.is on the input to verify if there is a cache hit.
function MapMarker(props) {
// ✅ Good: Pass primitives to memoized function
const length = calculateNorm(props.x, props.y, props.z);
// ...
}
function App() {
return (
<>
<MapMarker x={10} y={10} z={10} />
<MapMarker x={10} y={10} z={10} />
</>
);
https://react.dev/reference/react/cache 11/13
20/02/2025, 09:07 cache – React
primitives.
Another solution may be to pass the vector object itself as a prop to the
component. We’ll need to pass the same object to both component
instances.
function MapMarker(props) {
// ✅ Good: Pass the same `vector` object
const length = calculateNorm(props.vector);
// ...
}
function App() {
const vector = [10, 10, 10];
return (
<>
<MapMarker vector={vector} />
<MapMarker vector={vector} />
</>
);
}
PREVIOUS
act
NEXT
https://react.dev/reference/react/cache 12/13
20/02/2025, 09:07 cache – React
createContext
uwu?
Describing the UI
Adding Interactivity
Managing State
Escape Hatches
Community More
Acknowledgements Terms
https://react.dev/reference/react/cache 13/13