Skip to content
Source
Sign inSign up
TanStack/query

TanStack Query causes components it's used in to render twice #5035

Closedthianphat(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

Hi,

First off, thank you for this. Game changer. I love it.

I'm running into this issue where any usage of my custom ReactQuery hook will cause a component to render twice. The first render is with undefined data, then the second render with the correct data. Even with it cached.

Is anyone else running into this issue? Please see CodeSandbox

Your minimal, reproducible example

https://codesandbox.io/embed/upbeat-minsky-6xf1im?fontsize=14&hidenavigation=1&theme=dark

Steps to reproduce

  1. Add react query to a component.
  2. Watch it render once undefined.
  3. Then watch it render again with the data.

Expected behavior

I expect it to render once with data.

How often does this bug happen?

Every time

Screenshots or Videos

Screenshot 2023-02-24 at 6 25 03 PM Screenshot 2023-02-24 at 6 24 42 PM Screenshot 2023-02-24 at 6 32 24 PM Screenshot 2023-02-24 at 6 24 48 PM Screenshot 2023-02-24 at 6 25 11 PM Screenshot 2023-02-24 at 6 30 26 PM

Platform

macOS

TanStack Query version

4.24.10

TypeScript version

No response

Additional context

CodeSandbox is what I have on my machine. I just changed the API URL.

  1. So I'm guessing this about the persistence plugin and you'd expect only one render after you have data persisted and reload the page? You're not mentioning that in the steps to reproduce, so this is just a guess.

    The persistence plugin restores asynchronously, but we'll still render the component while the restore is happening. This is crucial for server-side-rendering to work. The query renders in loading state, but with fetchStatus: idle. If you want to stop your app from rendering, you can check with useIsRestoring if that is in progress and render null or something instead of your App:

    const isRestoring = useIsRestoring()
    
    return isRestoring ? null : <App />

    then, your App will only render once.

  2. TkDodo added labelspackage: react-query-persist-client

  3. Idk, that component has a bunch of other side effects that could cause renders. If it works in a sandbox, we're good.

  4. TkDodo closed this

Sign in to join this conversation.