-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Beta] Disabled query has isLoading: true
property
#3584
Comments
@TkDodo in such case I think it makes sense to mention in the migration guide that |
The migration guide mentions that the idle state was replaced with isLoading: true and fetchStatus: idle. If your query was in idle state on v3, it will be in loading state on v4. My guess is that's what's happening to you, but not every disabled query will be in loading state, just like not every disabled query was in idle state. If you have data and then disable the query, it will still be in success state. |
This is messed up. If the enabled = false, isLoading should be in false as well |
This caught me off guard as well. This page gives a bit of info on the subject |
How to deal with disabled false if we have a loading page that work if isloading is true and the loader is showing |
This is really messed up. On big repo that relies heavily on isLoading state, it is a nightmare upgrading to v4. |
This totally caught me off guard too! I am leaving a link to this in my repo for my future self 😂 It feels counter-intuitive to leave loading to true when the query is disabled. |
So what to do in this case? If I have a query that is disabled, am I to just use isFetching? There is not really as use for isLoading now that it will always show a spinner on components that do not meet a url param criteria (for my use case). Is param in url? fetch, if it is not always be enabled = false. Due to this change the spinner is always going even if it does not need to be. |
What I ended up doing as quick and dirty workaround is the following:
|
I did something slightly different const useWrapper = ()=> {
const query = useQuery(['key'], fetchFunction)
return {
...query,
isLoading: query.isLoading && query.fetchStatus !== "idle"
}
} |
for me it works testing on this |
this makes sense. What's the reason not to go with this approach? @tannerlinsley @TkDodo |
It is true that It makes the migration from v3 to v4 a bit worse :/ |
because isLoading is strictly a derived boolean from status === 'loading', which also discriminates the type of |
But as it is now. It's a breaking change from v3 to v4. Have so loading spinners on components that shouldn't have them, because the fetch is only to go out when a condition is met. So until it's met, spinners everywhere. I'd consider that a breaking change |
Yes, it's a breaking change. That's why it was made in a major version (v4). It comes from the idle state being removed, and it's documented in the migration guide. |
So reverting a breaking change. Or mimicking the previous implementation as I did below, is in itself a breaking change? It's reverting to what was present. Now the code needs to be littered with additional contional checks to see if the state is idle or not.
|
It is a breaking change from the perspective of version 4. People that have started with version 4 or that have already changed their code when migrating from v3 to v4 would have to adapter their code. This can only happen in a major v5. I've already stated what the plans are. For reference: |
Ouch that's unfortunate. So for now wrappers and or additional checks for v4. |
Oh this is great! |
we discussed this and there are good reasons not to do this, sorry. I am working on a PR that adds a new derived flag |
@TkDodo Would you say this is a good pattern with v4? Maybe you were thinking we change the way we use "enabled" or "isLoading"? Maybe I'm still in a v3 mindset and theres a better way to approach it? |
@changyeamoon yes it's a fine pattern, but you will be able to use the provided flag in the next version: |
@TkDodo I might be missing something, new to react-query. But getting strange behavior from isInitialLoading. My codebase is littered with queries relying on isLoading and these all work. But there is one that needs to be disabled and it is causing issues for some of our users some of the time. I suspect it might be due to patchy internet connection. The code
Sometimes the above will throw. Not often, but it happens a few times per day. "other" in this case is:
|
yes, it gets to
|
Thank you Dominic, much appreciated! For myself and others who might misunderstand how to do this properly - what would be the safe way of doing what I am doing in the code example above? |
|
The solution I found to keep isLoading functional: const { isInitialLoading, isRefetching } = useQuery(['some-key'], someMethod, { enabled: false })
const isLoading = isInitialLoading || isRefetching; |
This behaviour seems to be strange. What is the reason of having |
This link is broken, I assume you mean this piece of doc here: Very confusing change though, need to plough through my codebase now to handle this. |
I also wasted much time on trying to figure out why isLoading was true when the query is disabled, does not make sense. I was trying to find the bug in my own code, until i found this post... |
Is this intended? Or is there any plan to be revised later? If it's an intuitive api, I think when enabled is false, is loading should also be false. |
@lgj9172 unfortunately looks like it's intended, 'cause it's very nonintuitive and strange behaviour. :( Best solution for this case would be to use |
have a look at v5
so you can keep using |
This is not a design choice, this is incorrect behavior. If a query is not loading, then In the meantime |
Agree with @JeremyBernier I'm not very sure how loading should be true when there's no query in progress. |
lol it makes noooo sense to have |
In my case how can I delay the API call, untill visible set to true
|
Not sure why this issue is closed. What are we supposed to do to check if it's just loading once it's enabled ? |
|
Describe the bug
In
react-query@beta
version has strange behaviour. By default query that hasenabled: false
option hasisLoading: true
property (see code sandbox for details). In stable releasereact-query@^3
is hasisLoading: false
. I didn't find any notes about this update in change logs so it seems like a bug.Your minimal, reproducible example
https://codesandbox.io/s/eager-bell-epuvfi?file=/src/App.js
Steps to reproduce
Define simple query with
enabled: false
optionExpected behavior
Query should have
isLoading: false
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Any
react-query version
4.0.0-beta.7
TypeScript version
Not relevant
Additional context
No response
The text was updated successfully, but these errors were encountered: