Skip to content
Source
Sign inSign up
TanStack/query

Infinite loop when used with Emotion and complex query object #8897

Send to my agent
ClosedOskarPersson(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

I've created a minimal reproducible example where I try to showcase that having these properties results in a infinite loop:

  • Having a custom hook that calls useQuery that has a queryFn that returns a complex object, in this case a Geometry object from @arcgis/core
  • Two nested components that both uses the hook
  • The most inner component wrapped in an element styled using Emotion

Removing any of these from the code seems to solve the issue.

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/9t48qy?file=%2Fsrc%2FApp.js%3A21%2C16

Steps to reproduce

Go to the linked sandbox and see the console logs

Expected behavior

No infinite loop

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 134.0.6998.166

Tanstack Query adapter

react-query

TanStack Query version

v5.71.1

TypeScript version

No response

Additional context

Not sure if this is a bug in TanStack Query, Emotion, ArcGIS or a mistake in my code, but when used together it results in an infinite loop of fetches and re-renders.

  1. in the sandbox, the query goes into an error state:

    {"name":"ReferenceError","message":"Geometry is not defined"}

    I tried importing it from "@arcgis/core" but it didn’t work for me:

    ModuleNotFoundError
    Could not find module in path: '@arcgis/core' relative to '/src/App.js'

    please fix the sandbox (and add the query-devtools)

  2. @TkDodo Now it should be fixed

  3. you’re creating the Wrapper inside your react component:

    const Wrapper = styled.div``;

    That’s a huge anti-pattern in react - components need to be stable. That makes it a new reference for react on each render, so it will re-mount children. Because staleTime is zero, you’ll see a refetchOnMount, which will re-render the parent (which also uses the same query), and then it starts again.

    So, not a bug - you need to make the wrapper stable.

  4. TkDodo closed this

  5. TkDodo added labelsinvalid

  6. TkDodo added labelsworks as designed

Sign in to join this conversation.