[Bug]: cache doesn't return the same refrence to a resolved promise
Ninguém assumiu esta issue ainda.
- Linguagem predominante
- JavaScript
- Estrelas
- 11.8k
- Forks
- 7.9k
- Merge médio
- 1d 11h
- PRs com merge (30d)
- 11
Descrição
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")
Guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Direção de pesquisa
Comece pela página de referência de cache e reproduza o exemplo fornecido de use(getData()) para comparar o comportamento documentado com o React 19. Verifique se o exemplo ou sua explicação descreve corretamente a identidade da promise para a mesma entrada; considera-se concluído quando o comportamento de infinite-fetch relatado for resolvido ou quando a documentação explicar claramente o uso compatível.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- javascript, react
- Domínio
- documentation
- Tipo de issue
- Bug
- Dificuldade
- 3/5
- Tempo estimado
- 1-2 dias
- Status de atividade
- Estagnada
- Clareza
- Razoavelmente clara
- Facilidade para iniciantes
- 38/100