-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Enabling a disabled query does not unsuspend #8499
Comments
I tried switching to a conditional
This works a bit better - the component renders with However, it still doesn’t trigger the fetch. The reason is that because a cache entry exists already, we can’t trigger the fetch during render without violating the rules of react: query/packages/react-query/src/useBaseQuery.ts Lines 146 to 150 in 6c2a055
The only thing that works right now is to pass the https://codesandbox.io/p/devbox/hopeful-flower-f6t8qp?workspaceId=ws_XiBCv7MgHSszKyXHXG5ray @KATT what do you think ? If we changed our code to call |
I don't have many opinions really, but I do have observations. Your explanation why this is happening is great and makes total sense, but if you only look at the public API, this still seems like a bug. I do think avoiding observable side effects in render is something we should strive for though, so where does that leave us? On the one hand, maybe this should lead us to question the current implementation, why are we triggering the fetch in the observer and not on the outside of it? I do think Suspense has been pushing the current RQ implementation for a while now. (It's interesting that On the other hand, I think we have something like three cases:
I don't have strong arguments why exactly, but I am starting to feel the current model/implementation chafing a bit. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Oh yeah, it's because the Agreed this is a bug |
FWIW I agree this specific bug is a bit niche and there's indeed a good userland workaround, hoist the |
I don't know about the internals of the library but I believe this is how it should work:
|
By the way I found another workaround for userland, I am sharing it here: In short, it consists on issuing a refetch if the query becomes enabled and hasn't been fetched before: if (enabled && isPending) refetch(); Probably has its issues, point them out if needed. |
Also, in case context helps, what I am trying to do is simply extending my hooks to be able to do this: const { data: user } = useQuery(...).suspense(); suspense function is basically a shortcut to: React.use(useQuery(...).promise) So then I have my custom hooks and can use them with or without suspense: const user = useUser().data; // user might be null
// or
const user = useUser().suspense().data; // user cannot be null Then I can easily use suspense but with parallel fetching: // Start both queries
const userQuery = useUser();
const articlesQuery = useArticles();
// Suspend until both queries have data
userQuery.suspend();
articlesQuery.suspend();
// Natively it would have been:
React.use(Promise.all([userQuery.promise, articlesQuery.promise])); The thing is that all this logic works inside hooks, I cannot create suspense boundaries to make it work. |
@hector what you describe is not how it works. In the second render, enabled is true, but fetching will happen in an effect. That effect never executes because
@Ephem I think I disagree here. The cache entry could definitely be observed by a different component, for example, the devools will show it. We could try to bend the rules here but I’m not really a fan. Generally, it seems that relying on I think all of this would be easier for us if I’m all in all more inclined to leave this pattern as-is, and document it, or even go the opposite direction and remove |
@TkDodo I see, thanks for the explanation.
What if you did not create the cache entry for a disabled query? Seems like it would work fine then. |
I need to think about this issue some more, but, I kinda think that we should have a I'm not convinced what the right thing to do is but I fixed it so it works according to current expectations in #8501. |
Describe the bug
I am using the new mechanism to suspend explained here: https://tanstack.com/query/latest/docs/framework/react/guides/suspense#using-usequerypromise-and-reactuse-experimental
Disabled queries return a pending promise (which is ok), but the promise does not resolve when the query is enabled afterwards.
This happens because the query is not refetched when enabled changes from false to true.
Your minimal, reproducible example
https://codesandbox.io/p/devbox/mystifying-mopsa-qwhjwx?workspaceId=ws_5WeinpZuX6zrSSrhdSgo5C
Steps to reproduce
Click the button to enable the query
Expected behavior
When the query is enabled it should fetch and the promise should finally resolve
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
Tanstack Query adapter
react-query
TanStack Query version
@tanstack/[email protected]
TypeScript version
[email protected]
Additional context
No response
The text was updated successfully, but these errors were encountered: