Course contentsShow
← React
Lesson 1480 of 2,39637. React 19 and BeyondFree lesson

Caching and Deduplication with use()

Understanding that use() itself doesn't cache; integrating with cache() or libraries for request deduplication.

Caching and Deduplication with use()

You'll learn that use() itself doesn't automatically cache or deduplicate requests—you need to combine it with caching strategies to avoid redundant work.

The Core Problem

When multiple components call use() with the same promise, React will read that promise multiple times. But here's the catch: use() doesn't cache the promise or its result. If you create a new promise each time, you'll trigger duplicate network requests or recompute expensive operations unnecessarily.

// ⚠️ Problem: Creates new promise on every render
function UserProfile() {
  const user = use(fetch('/api/user')); // New promise every time!
  return <div>{user.name}</div>;
}

The Solution: Cache Your Promises

You need to cache promises before passing them to use(). React 19 provides a cache() function for this (typically used with Server Components), or you can use external libraries like SWR or React Query for client-side caching:

// ✅ Cache the promise, not the use() call
const getUserPromise = cache(() => fetch('/api/user'));

function UserProfile() {
  const user = use(getUserPromise()); // Same promise instance
  return <div>{user.name}</div>;
}

The Analogy

Think of use() as a book reader—it reads whatever book you hand it. It doesn't remember books or check if it's already read one. If you keep handing it new copies of the same book, it'll read them all. You need a librarian (cache() or a caching library) to say "here's the same book instance everyone should read."

Key Takeaway: use() reads promises; it doesn't cache them. Always cache promises outside of use() to prevent duplicate requests and unnecessary work.