Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop (browser crashes) in Next 13 #5532

Closed
victorbianconi opened this issue Jun 5, 2023 · 8 comments
Closed

Infinite loop (browser crashes) in Next 13 #5532

victorbianconi opened this issue Jun 5, 2023 · 8 comments
Labels
invalid This doesn't seem right

Comments

@victorbianconi
Copy link

victorbianconi commented Jun 5, 2023

Describe the bug

Can't get React Query to work in Next 13 (even on client components). The app crashes totally and if I log the query it's basically looped indefinitely.

Your minimal, reproducible example

Code

Steps to reproduce

  1. Just use React Query as normal in a client component.

Expected behavior

It should not loop nor crash. or

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Windows, Chrome

Tanstack Query adapter

react-query

TanStack Query version

v4.29.12

TypeScript version

No response

Additional context

No response

@TkDodo
Copy link
Collaborator

TkDodo commented Jun 5, 2023

please show a runnable reproduction of your issue, or we'll close this issue.

@victorbianconi
Copy link
Author

I don't have an example right now but this is the code. Not sure how to format properly here. If I use the same code in a normal react app, it works. So I assume it must be related to Next.

"use client";

import { useInfiniteQuery } from "@tanstack/react-query";

const fetchGens = async (pageParam) => {
  const response = await fetch("/api/generations/my?pageParam=" + pageParam);
  const data = await response.json(); // Extract the JSON data from the response
  console.log(data);
  return data;
};

export default async function Generations({ gens }) {
  const {
    fetchNextPage,
    fetchPreviousPage,
    hasNextPage,
    hasPreviousPage,
    isFetchingNextPage,
    isFetchingPreviousPage,
    data,
  } = useInfiniteQuery({
    queryKey: ["generations"],
    queryFn: ({ pageParam = 0 }) => fetchGens(pageParam),
    getNextPageParam: (lastPage, allPages) => lastPage.nextCursor,
    getPreviousPageParam: (firstPage, allPages) => firstPage.prevCursor,
    onSuccess: (data) => {
      console.log(data)
    },
  });

  return (
    <main className="flex flex-col min-h-[100vh] items-center py-[4rem]">
      {" "}
      <div className="w-[80vw] mobile:w-[90vw] flex flex-col">
        {" "}
        <h1 className="text-3xl text-white font-bold mb-[1.6rem]">
          Generations
        </h1>
        <div className="grid grid-cols-3"> </div>
      </div>
    </main>
  );
}

@TkDodo
Copy link
Collaborator

TkDodo commented Jun 5, 2023

I don't have an example right now

then please create one? Put that code in a codesandbox, and stub out the api request with a Promise.resolve(someJsonData)

It's your job to provide a proper reproduction if you want help from maintainers. There are a ton of things missing that could be done wrongly, like queryClient creation etc. From seeing just that code, I'd say things are fine and it works on my machine ...

@victorbianconi
Copy link
Author

victorbianconi commented Jun 5, 2023

Here you go: https://codesandbox.io/p/sandbox/kind-snow-04rdx0
I hope this suffices. The API obviously doesn't work but I noticed it still loops and crashes.
Let me know if this is enough info for you.
edit: nvm, can't get it to replicate. You can close this issue but what do you think can be the reason? My code isn't any different except for the real API call which successfully returns the data (only once). The loop happens on the front-end and no API calls are made after the first (rightly so).

@TkDodo
Copy link
Collaborator

TkDodo commented Jun 5, 2023

Idk but that's exactly the reason why I always demand a reproduction. It's 95% not the libs fault, but something in your surroundings.

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Jun 5, 2023
@TkDodo TkDodo added the invalid This doesn't seem right label Jun 5, 2023
@victorbianconi
Copy link
Author

Idk but that's exactly the reason why I always demand a reproduction. It's 95% not the libs fault, but something in your surroundings.

I migrated back to Next 12.3.2 and it works just fine. Must have been the latest version from Next.

@Justinfan827
Copy link

I don't think you can have client components using the 'async' keyword: facebook/react#26801

@TkDodo
Copy link
Collaborator

TkDodo commented Aug 2, 2023

good catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
invalid This doesn't seem right
Projects
None yet
Development

No branches or pull requests

3 participants