reactjs / reactjs/react.dev

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

オープン
#7,393 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

bug: unconfirmed
主要言語
JavaScript
スター
11.8k
フォーク
7.9k
平均マージ
1日 11時間
マージ済み PR(30日)
11

説明

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")

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

cache リファレンスページから始め、提供されている use(getData()) の例を再現して、ドキュメントに記載された動作を React 19 と比較します。同じ入力に対する promise identity について、例またはその説明が正確に記述しているか確認します。報告された infinite-fetch の動作が解消されるか、ドキュメントでサポートされる使用方法が明確に説明されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, react
領域
documentation
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。