useQuery return always isLoading with true when enabled is false
See original GitHub issueDescribe the bug
When I created useQuery
with enabled: false
option, isLoading
variable is always true
.
Your minimal, reproducible example
https://codesandbox.io/s/react-query-enabled-7u58d5-7u58d5
Steps to reproduce
- Create simple query by use
useQuery
, - Add
enabled: false
into options, - Check state for
isLoading
.
Expected behavior
State isLoading
is false
.
How often does this bug happen?
Every time
Screenshots or Videos
Platform
- OS: Windows 11 22H2,
- Browser: Chrome
- Version: 103
react-query version
4.0.10
TypeScript version
4.7.4
Additional context
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:3
- Comments:30 (2 by maintainers)
Top Results From Across the Web
[Beta] Disabled query has isLoading: true property #3584
Describe the bug In react-query@beta version has strange behaviour. By default query that has enabled: false option has isLoading: true ...
Read more >What state represents the loading or data not fetched yet state ...
Because I am using the enabled dependency, the isLoading is false and isSuccess is true on the initial page load.
Read more >useQuery | TanStack Query Docs
enabled : boolean. Set this to false to disable this query from automatically running. Can be used for Dependent Queries. networkMode: 'online' |...
Read more >Queries - Apollo GraphQL Docs
When your component renders, useQuery returns an object from Apollo Client that contains loading , error , and data properties you can use...
Read more >Fetching data with React - GQty
This hook returns the core query object, and it will detect the usage of it, and suspend (if enabled) when the data is...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
So I think I have up my mind for what I want to do for the next major version. Believe me, I am terribly sorry that I didn’t do this with v4 already. No one regrets this more than me. The plan is to:
status: 'loading'
tostatus: 'pending'
isLoading
toisPending
(a simple derived state from the status, like isSuccess and isError)isLoading
as derived value from:isPending && isFetching
That would allow our main status field to remain intact and describe
data
(pending/success/error), while also having a goodisLoading
boolean that works like in v3. The breaking thing would be thatstatus === 'loading'
wouldn’t exist anymore, as you would have to change it tostatus === 'pending'
.The thing is, if we introduce something now that combines the two like
isInitialDataLoading
or whatever, it wouldn’t actually be needed, as it would beisLoading
as described above.With that in mind, yes, I think we can:
isLoading && isFetching
now.isLoading
Please drop more name suggestions, and I’ll also run this proposal by @tannerlinsley
I found this issue after running into the same issue and spending time debugging it. And tbh, I also had the same ‘are you serious?’ thought.
For a breaking change like this, I didn’t find the word ‘enabled’ used in the doc Migrating to React Query 4. It would be helpful to include explicit instructions (e.g. a before&after code example) about how to migrate code with
enabled: false
in the guide.