question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Next 13 example from docs of Use() together with fetch inside client component fires indefinitely

See original GitHub issue

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)

image

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.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:2
  • Comments:8

github_iconTop GitHub Comments

4reactions
himyjancommented, Nov 7, 2022

@SimonProper

'use client';

import { use } from 'react';

async function getData() {
  const res = await fetch('/api/hello');
  // The return value is *not* serialized
  // You can return Date, Map, Set, etc.
  console.log('fetching');
  return res.json();
}

const dataPromise = getData();

export default function Page() {
  const name = use(dataPromise);

  return '...';
}

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

1reaction
SimonPropercommented, Nov 1, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Data Fetching - Next.js beta docs
We recommend reading the support for promises React RFC which introduces async / await in Server Components and a new use() hook for...
Read more >
Testing React components - Apollo GraphQL Docs
This article describes best practices for testing React components that use Apollo Client. The examples below use Jest and React Testing Library, ...
Read more >
Understanding the Event Loop, Callbacks, Promises, and ...
The event loop checks the queue for any pending messages and finds the anonymous function from setTimeout() , adds the function to the...
Read more >
Understanding Next.js routeChangeStart and router events
For example, the event routeChangeStart fires up when a route starts to change; in other words, when a user navigates to a new...
Read more >
Kafka 3.3 Documentation
Once received, the brokers will store the events in a durable and fault-tolerant manner for as long as you need—even forever. Run the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found