🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)
🕵️ SicherheitslückenWhat continuous operational resilience looks like under DORA(09.09.2026 um 17:53 Uhr)
🔧 AI Nachrichten OpenAI seeks tougher AI rules. CIOs may feel the ripple effects(10.09.2026 um 12:11 Uhr)
🔧 AI Nachrichten Mistral valued at €21bn after €3bn Series D funding round(08.09.2026 um 10:19 Uhr)
🪟 Windows TippsWindows XP's Cursor Indicator Is Getting a Windows 11 Refresh(25.08.2026 um 13:00 Uhr)

🔧 Programmierung 🕛 vor 3 Monaten 3 Min Lesezeit
0

React.js ~use() hook in a Client Component~

↗ Quelle (dev.to)
🗣️ Stimme:

What about Client-Side fetching for use() hook?

All examples so far start the fetch in a Server Component and pass the promise down. But what if you are already deep inside a client component, say a button opens a popup that needs fresh data?



You can't render a Server Component inside a Client Component. But use() + Suspense still works, you just have to manage promise identity yourself.



Start the Fetch in the Event Handler

The most straightforward approach: create the promise in the click handler, store it in state, and let use() read it.




CODE
"use client";

function DetailPopup({ dataPromise }: { dataPromise: Promise<ItemDetail> }) {
const detail = use(dataPromise);
return <div>{detail.description}</div>;
}

function ItemCard({ itemId }: { itemId: string }) {
const [promise, setPromise] = useState<Promise<ItemDetail> | null>(null);

const handleOpen = () => {
setPromise(fetchItemDetail(itemId)); // fetch starts immediately
};

return (
<>
<button onClick={handleOpen}>Show Details</button>
{promise && (
<Suspense fallback={<Skeleton />}>
<DetailPopup dataPromise={promise} />
</Suspense>
)}
</>
);
}







This is responsive, the fetch fires the instant the user clicks, not after React schedules a render. Each click creates a new promise, so you always get fresh data.



Use a Module-level Cache for Repeated Access

If the same popup might be opened multiple times with the same ID, a cache avoids redundant requests:




CODE
const cache = new Map<string, Promise<ItemDetail>>();

export function getItemDetail(id: string) {
if (!cache.has(id)) {
cache.set(
id,
fetch(`/api/items/${id}`).then((r) => r.json()),
);
}
return cache.get(id)!;
}










CODE
const handleOpen = () => {
setPromise(getItemDetail(itemId));
};







This is exactly the same caching pattern from the section above, it works identically whether the promise is created in a Server Component or a click handler. What matters is that use() always receives the same promise object for the same request.



When is use() Enough, and When Do You Need More?

For the popup scenario above, user clicks, data loads, popup displays it. use() with a simple cache function is all you need. No extra dependency, no provider, no configuration. The 5-line Map cache from above handles deduplication just fine.



Consider TanStack Query or SWR when the data has a lifecycle beyond a single display:




  • The same data is shown in multiple places and a mutation in one place should update all of them.

  • Data goes stale and should silently refetch when the user returns to the tab.

  • You need paginated or infinite-scroll lists with cursor tracking.

  • You want optimistic UI that rolls back on server error.



If none of those apply, use() + a cache function is the simpler choice. You can always add a library later when the caching requirements grow, the mental model (promise in, data out, Suspense handles the wait) stays the same.

Vollständiger Original-Bericht
Ausführliche Details, Code-Beispiele & Hersteller-Stellungnahme auf dev.to.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
1 Quelle
Sam Altman calls GPT-6 Astra rollout ‘messy’ as enterprise users wait for access
1 Quelle
Swiss government explores replacing Microsoft 365 with open-source software
1 Quelle
What continuous operational resilience looks like under DORA
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten React.js ~use() hook in a Client Component~

Thematisch verwandte Begriffe: Reactjs, hook, Client, Component · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...