reactjs / reactjs/react.dev

[Bug]: cache doesn't return the same refrence to a resolved promise

Aperta
#7,393 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug: unconfirmed
Lingua principale
JavaScript
Stelle
11.8k
Fork
7.9k
Merge medio
1g 11h
PR unite (30g)
11

Descrizione

Summary

The cache function generates a new promise reference on every call, causing the use hook to treat it as a new dependency. This results in an infinite fetch loop as getData() is continuously invoked on each render.

Page

https://react.dev/reference/react/cache

Details

The following code demonstrates an issue with the cache function, which leads to an infinite fetch loop when used with use

'use client';
import { use, cache } from "react";

export const getData = cache(() => fetch("https://jsonplaceholder.typicode.com/posts").then(res => res.json()));

export function Home() {
    const data = use(getData());

    return (
        <pre>{JSON.stringify(data)}</pre>
    );
};
Problem

The cache function is expected to memoize the result of a function call. However, it appears to return a new promise reference every time getData() is called, which causes the use hook to treat it as a new input on every render. This results in an infinite fetch loop.

Expected Behavior

The cache function should return the same promise reference for the same input, ensuring that use(getData()) only triggers a single fetch request.

Actual Behavior

Maybe the cache function creates a new promise reference on each call, leading to an infinite loop of fetch requests.

Environment
React version: >=19 ("^19.0.0")

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla pagina di riferimento di cache e riproduci l’esempio fornito di use(getData()) per confrontare il comportamento documentato con React 19. Verifica se l’esempio o la relativa spiegazione descrivono correttamente l’identità della promise per lo stesso input; il lavoro è completato quando il comportamento di infinite-fetch segnalato è stato risolto oppure la documentazione spiega chiaramente l’utilizzo supportato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript, react
Ambito
documentation
Tipo di issue
Bug
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.