Skip to content
Source
Sign inSign up
TanStack/query

Weird bug in useQuery's select #7514

Send to my agent
ClosedmiguelSoldado1(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

Not 100% sure if it's just something weird with React/JS or actually a bug so I'll apologize right now if it isn't actually a bug. Summed up what happens is that whenever i call the selectData function like so:

const { data } = useQuery({
    queryKey: ["mock"],
    queryFn: mockFetch,
    select: (data) => selectData(data),
  });

selectData function is called twice, (which can be seen in my reproduction by count.

If I change it up to this:

  const { data } = useQuery({
    queryKey: ["mock"],
    queryFn: mockFetch,
    select: selectData,
  });

selectData is only called once.

In this simple scenario it seems like a quick fix but I discovered this by using 2 different useQuery calls and the second one has some weird dependencies on the first one so i used the select function to pass this data (I made sure to only enable the second function whenever the first one is finished) so I believe there's no other way of achieving this (lmk if I'm wrong pls)

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/busy-haslett-wxl5m2

Steps to reproduce

  1. Go to: https://codesandbox.io/p/sandbox/busy-haslett-wxl5m2
  2. Check that the rendered count is 2 (on a fresh page reload) whenever I don't pass the props through a direct function reference.
  3. Uncomment and start using the direct function reference and notice that the render count is now only one.

Expected behavior

I expected the selectData function above to only be called once in both scenarios.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Windows 11
  • Browser: Chrome
  • @tanstack/react-query: 5.40.0

Tanstack Query adapter

react-query

TanStack Query version

v5.40.0

TypeScript version

v5.3.3

Additional context

No response

  1. TkDodo closed this

  2. TkDodo added labelsinvalid

  3. TkDodo added labelsworks as designed

Sign in to join this conversation.