[Bug]: cache doesn't return the same refrence to a resolved promise
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- JavaScript
- Estrellas
- 11.8k
- Forks
- 7.9k
- Merge medio
- 1 d 11 h
- PR fusionados (30 d)
- 11
Descripción
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")
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con la página de referencia de cache y reproduce el ejemplo proporcionado de use(getData()) para comparar el comportamiento documentado con React 19. Comprueba si el ejemplo o su explicación describen correctamente la identidad de la promise para la misma entrada; se considera terminado cuando el comportamiento de infinite-fetch reportado se haya resuelto o la documentación explique claramente el uso compatible.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- javascript, react
- Área
- documentation
- Tipo de issue
- Error
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 38/100