next.js: Next 13 example from docs of Use() together with fetch inside client component fires indefinitely
Verify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System: Platform: darwin Arch: arm64 Version: Darwin Kernel Version 22.1.0: Sun Oct 9 20:15:09 PDT 2022; root:xnu-8792.41.9~2/RELEASE_ARM64_T6000 Binaries: Node: 19.0.0 npm: 8.19.2 Yarn: 1.22.19 pnpm: N/A Relevant packages: next: 13.0.1-canary.4 eslint-config-next: N/A react: 18.3.0-next-2ac77aab9-20221029 react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
next dev
Describe the Bug
When following the example provided in the docs for fetching data inside a client component using the new Use() hook. The fetch function falls into an infinity loop and fires until the client session(browser tab) is closed.
The endpoint of the fetch also gets hit infinity amount of times as well as can be seen in the stackblitz provided. A simple counter in the pages/api/hello.ts file that increments and logs on each hit. This was additionally tested with a simple node server with the same result. (so not unique to when fetching from a next api endpoint)

Expected Behavior
For the Use() hook to fire the fetch function once.
Link to reproduction
https://stackblitz.com/edit/vercel-next-js-snerz6?file=app/page.tsx
To Reproduce
Run next dev and go to the / page. See the api hits in the log.
ctrl+c in the terminal to stop.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 7
- Comments: 16 (1 by maintainers)
@SimonProper
cache fetch promise with variable can fix use() hook loop;
or try query client custom function in video
new youtube video code fix
NextJS 13 WARNING: Easy Mistake = Infinite Loops
https://youtu.be/zwQs4wXr9Bg?t=531
Sorry no, I’ve just avoided loading data this way on the client side (for now). I’m using parent server components to pass in the initial data (if required), via props and then using fetch without use to load/refresh data within the client component.
Will copy my answer from here just in case
@himyjan is there any way of structuring your workaround so that we can pass data in to getData() to fetch a specific record or query?
Something like the below returns us to the same infinite loop issue…
`‘use client’;
import { use } from ‘react’;
async function getData(id) { const res = await fetch(
/api/{id}); console.log(‘fetching’); return res.json(); }const dataPromise = (id)=getData(id);
export default function Page() { const [id, setId] = useState(1);
const name = use(dataPromise(id));
//returned component would include some way of updating the id in state and reloading the data return ‘…’; }`
Sadly it doesn’t affect this case, if you look in the app/page.tsx file, the fetch is already inside a separate async function outside the component.
This is also copied directly from the docs (with the exception for the added console.log() ) so it should behave correctly.