query icon indicating copy to clipboard operation
query copied to clipboard

despite setting the enabled option to false, useQuery still initiates a fetch

Open aastronaut opened this issue 6 years ago • 36 comments

Describe the bug After an update to 2.23.1 or 3.2.0-beta.3 I noticed that passing the option { enabled: false } to useQuery will still initiate a request when the component mounts. Where this flag was enough to prevent the hook from causing a request on mount in an earlier version (2.5.13 in my case), now I would need to create a separate component and mount it conditionally in order to regain the same wanted behavior, which would result in a lot of boilerplate.

To Reproduce (on request if unclear)

Expected behavior I would expect the option { enabled: false } to disable fetching completely, as that would prevent the creation of an conditionally mounted component. However, the expected behavior is unclear to me due to mismatching descriptions in docs and source code:

According to the docs automatically running is mentioned:

enabled: Boolean | unknown
    Set this to false to disable this query from automatically running.
    Actually it can be anything that will pass a boolean condition. See Dependent Queries for more information.

According to the source code automatic refetching is mentioned:

/**
 * Set this to `false` to disable automatic refetching when the query mounts or changes query keys.
 * To refetch the query, use the `refetch` method returned from the `useQuery` instance.
 * Defaults to `true`.
 */
enabled?: boolean | unknown

A clear and concise description of what you expected to happen. Please either adapt the docs to mention that only refetching is controlled with this option, or a fix is needed for the hook to control also the fetching behavior when a component mounts.

aastronaut avatar Oct 21 '20 16:10 aastronaut

Queries that are mounted with enabled: false should start in the idle state and should not fetch until it is enabled. This is used for orchestrating dependent queries, where you don’t have all the data to make a request initially.

So this seems like a bug to me. Can you make a codesandbox to reproduce maybe?

TkDodo avatar Oct 25 '20 06:10 TkDodo

So, I just tested this with the latest beta (3.2.0-beta.12) and it works fine, have a look here: https://codesandbox.io/s/happy-browser-k286h

We start the query in idle state because enabled is false, and there is no request. Click the button, see the request in the browser devtools.

Maybe you were setting enabled to a falsy value rather than really setting it to false ? This is a breaking change in v3 (stated here: https://react-query-beta.tanstack.com/guides/migrating-to-react-query-3#queryoptionsenabled). Falsy values will no longer work, it needs to really be the boolean value false.

This sentence:

Actually it can be anything that will pass a boolean condition. See Dependent Queries for more information.

has also been removed from the v3 docs. It now states:

Screenshot 2020-10-25 at 16 50 55

TkDodo avatar Oct 25 '20 15:10 TkDodo

same bug to me, enable: false is set, useQuery still initiates a fetch, "react-query": "^2.23.1",

KingAmo avatar Oct 26 '20 12:10 KingAmo

@KingAmo here is the same example from above with react-query 2.23.1: https://codesandbox.io/s/funny-bird-0ulmy

Seems to work like advertised.

If you are having problems, please create a codesandbox example of your own where the issue is reproducible.

TkDodo avatar Oct 26 '20 18:10 TkDodo

@KingAmo here is the same example from above with react-query 2.23.1: https://codesandbox.io/s/funny-bird-0ulmy

Seems to work like advertised.

If you are having problems, please create a codesandbox example of your own where the issue is reproducible.

sorry, my fault, i use refetch in some useEffect, so there is still a initial fetch;

KingAmo avatar Oct 27 '20 11:10 KingAmo

Also, as discussed on Discord, the object syntax has changed from nested (v2) to flat (v3), see: https://react-query-beta.tanstack.com/guides/migrating-to-react-query-3#query-object-syntax

I think we can close this issue.

TkDodo avatar Oct 29 '20 19:10 TkDodo

Also, as discussed on Discord, the object syntax has changed from nested (v2) to flat (v3), see: https://react-query-beta.tanstack.com/guides/migrating-to-react-query-3#query-object-syntax

I think we can close this issue.

But what if you are not using query object syntax? The main docs themselves don't use an object https://react-query-beta.tanstack.com/reference/useQuery

evliu avatar Oct 29 '20 23:10 evliu

Actually it looks like my version of this bug is when i'm using usePaginatedQuery, either with object syntax or not

evliu avatar Oct 30 '20 00:10 evliu

Okay, can you post an example with that please? FYI, there will be no usePaginatedQuery in v3, it’s just useQuery with keepPreviousData set to true: https://react-query-beta.tanstack.com/guides/migrating-to-react-query-3#usepaginatedquery

TkDodo avatar Oct 30 '20 06:10 TkDodo

I just used the example for Pagination in the docs and set enabled to false hardcode. I'll just upgrade to v3.

evliu avatar Oct 30 '20 16:10 evliu

I just used the example for Pagination in the docs and set enabled to false hardcode. I'll just upgrade to v3.

If that's all you did, why can't you fork the example and post the link here to the non-working version? That would make it a lot easier for us ...

Anyways, I did exactly that: https://codesandbox.io/s/sleepy-wave-z16lt

You'll end up in a runtime error if you only do that, because you are in idle state then, and the example doesn't check that. So I added that check too. Apart from that, I only have one query now in idle state and there is no network request ...

So unless someone can show me a runnable example where something is clearly not working, or I am completely misunderstanding something here, we should close this issue please.

TkDodo avatar Oct 31 '20 20:10 TkDodo

I agree with closing the issue

evliu avatar Oct 31 '20 20:10 evliu

Sorry to bring back this again. But I'm able to reproduce it and it's an issue in my app.

Refresh with open dev-tools/console https://codesandbox.io/s/dry-fast-mc2yy9?file=/src/index.js

image

What am I doing wrong?

aleksandrenko avatar Sep 27 '23 14:09 aleksandrenko

you're using a very old version (v2). v5 is around the corner. In v2, options weren't flat:

  return useQuery(
    ["test"],
    () => {
      console.log("BUT STILL RUNNING THE QUERY FN");

      return "hello";
    },
    { enabled: false }
  );

please upgrade; we're not really supporting v2 anymore

TkDodo avatar Sep 27 '23 15:09 TkDodo

having the same issue on v4 with useQueries: "@tanstack/react-query": "^4.32.6",

const queryResults = useQueries({
    queries: [
      {
        queryKey: ['settings'],
        queryFn: async () => {
            const res: any = await fetch('http://something')
            return res 
        },
        enabled: false,
        cacheTime: 0
      },
      {
        queryKey: ['data'],
        queryFn: async () => {
            const res: any = await fetch('http://something')

            return res
        },
        enabled: false,
        cacheTime: 0
      },
    ],
})

ghost avatar Oct 26 '23 03:10 ghost

@daniel-ricado then show a reproduction with v4 please.

TkDodo avatar Oct 26 '23 06:10 TkDodo

unfortunately this happend again and the bug returned

moussa32 avatar Jan 11 '24 00:01 moussa32

This is occurring for me also. Seems to happen if a query is used elsewhere at the same time in another component (maybe because it is cached?). I've updated to the V5 useQuery format also.

martyjg avatar Jan 12 '24 19:01 martyjg

same here

ewof avatar Jan 19 '24 22:01 ewof

This is occurring for me also. Seems to happen if a query is used elsewhere at the same time in another component (maybe because it is cached?). I've updated to the V5 useQuery format also.

I was having this same issue, but it ended up being how I was using initialData in my queries. I had an "observer" hook with useQueries that listened to a bunch of queries and enabled: false so that the observers never triggered a fetch. Then I had separate components where those queries were actually fetched from with an enabled useQuery. I was passing an initialData to the enabled queries with staleTime: Infinity.

My expected behavior was that the initial data for the enabled queries would be set and not go stale, then the observer queries would see the initial data and nothing else until a user initiated more fetching. What actually was happening was that all of the enabled queries were fetching when they rendered.

Passing the same initialData to both the observer queries and the enabled queries fixed this. I think the root of the issue was that the observer queries were rendering first, without initialData. Then when the enabled queries were rendered after, their initialData would be ignored because a query with that key was already initialized. From the enabled query's perspective, they had no data and needed to fetch.

Here's an example repro, hopefully it helps someone.

alxHenry avatar Jan 20 '24 01:01 alxHenry