Skip to content

Dependant queries #1413

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

Closed
stromkalle opened this issue Dec 14, 2020 · 8 comments · Fixed by #1449
Closed

Dependant queries #1413

stromkalle opened this issue Dec 14, 2020 · 8 comments · Fixed by #1449

Comments

@stromkalle
Copy link

Describe the bug

Dependant queries leave an in between state after the first query has succeeded where the first query has a successful status and the following query is still in idle.

To Reproduce

const queryA = useQuery("queryA", fetchSomething);
const queryB = useQuery("queryB", fetchSomethingElse, { enabled: queryA.isSuccess });

console.log(queryA.status, queryB.status);

// loading idle
// success idle
// success loading

Expected behavior
I expect there to be no inbetween state and for queryB to go into a loading state as soon as queryA has succeeded. Expected console log would be:

// loading idle
// success loading
// success success

Desktop (please complete the following information):

  • OS: macOS Catalina
  • Chrome
  • Version: 3.2.0
@tannerlinsley
Copy link
Collaborator

This is purely because we use safe side effects as opposed to mutating or altering state during a render. If we were to do the opposite, you’d get some really really strange behavior, especially if and when you start using suspense.

Curious why this is an issue for you?

@stromkalle
Copy link
Author

stromkalle commented Dec 14, 2020

This is purely because we use safe side effects as opposed to mutating or altering state during a render. If we were to do the opposite, you’d get some really really strange behavior, especially if and when you start using suspense.

Curious why this is an issue for you?

Well, with something like this you end up seeing the loader flash, but perhaps there is some easy workaround that im missing?

<Loader visible={queryA.isLoading || queryB.isLoading} />

Also, I believe it used to work as the expected behaviour I presented.

@tannerlinsley
Copy link
Collaborator

Hmm. I’ll take a deeper look.

@TkDodo
Copy link
Collaborator

TkDodo commented Dec 14, 2020

I checked against v2.26.4, it prints:

loading idle 
success loading 
success loading 
success success 

(success loading is printed twice). also, every window re-focus prints success success 3 times, so there seem to be 3 re-renders. repro: https://codesandbox.io/s/charming-frog-kz4cd

v3.2.0 prints, as OP says:

loading idle 
success idle 
success loading 

every window re-focus also prints success success 3 times (not sure if this is a separate issue?)
repro: https://codesandbox.io/s/focused-hertz-yi541

@boschni
Copy link
Collaborator

boschni commented Dec 15, 2020

The returned values from the hooks in v3 are read from the component state instead of directly from the observer. This will make sure no state update is skipped. So when using a hook, you can be sure you will be notified of all changes. I guess this could be important when dealing with side effects. The downside of this though, is that no state update is skipped ;) When rendering, the observer might already contain a newer state, which could be used instead of the component state, but any states in between will then never be rendered.

@TkDodo
Copy link
Collaborator

TkDodo commented Dec 15, 2020

so how would we go about queryA.isLoading || queryB.isLoading ? I think this is a very valid use-case when dealing with dependent queries

@boschni
Copy link
Collaborator

boschni commented Dec 15, 2020

I guess the only way to prevent this is to immediately returning the updated value instead of waiting for the state to update. Let me see what I can do here.

@stromkalle
Copy link
Author

Cheers guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants