reactjs / reactjs/react.dev

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

Open
#7,393 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug: unconfirmed
Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the cache reference page and reproduce the provided use(getData()) example to compare the documented behavior with React 19. Check whether the example or its explanation accurately describes promise identity for the same input; done means the reported infinite-fetch behavior is either resolved or the documentation clearly explains the supported usage.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.