Replies: 2 comments 6 replies
-
Hi 👋 Have you seen the documentation on dependent queries? I like how they're expressed, personally. Are the current ergonomics a pain point for you? https://tanstack.com/query/v4/docs/react/guides/dependent-queries I wonder if your implementation would violate the rules of hooks somewhere internally, although I haven't looked at the internals in much detail so perhaps that's a moot point. |
Beta Was this translation helpful? Give feedback.
4 replies
-
See #4727 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I wonder if we can execute conditional / dependent queries with API like following.
Is it feasible?
Following CodeSandbox is a rough proof of concept.
https://codesandbox.io/s/nice-sun-diu0uc?file=/src/App.tsx
Though CodeSandbox looks "works" for me, it might be broken under some use cases.
I'm not familiar with react-query's internal lifecycle.
Idea to realize this API (added on 2023-01-27)
To realize it, I think following conditions should be satisfied.
fetcher.fetch
should return query cache immediately (doesn't return Promise).queryFn
should happen inuseEffect
hook.useFetcher
in the CodeSandbox works like following way.useFetcher
has a local variableobservers
, which storesQueryObserver
that is necessary for current render.useFetcher
invokesuseEffect
. The effect is "subscribe all theobservers
. and unsbscribe them at cleanup.".QueryObserver
will invokequeryFn
.fetcher.fetch
creates a newQueryObserver
on each call and push it toobservers
. And it returns a result from the current cache.This seems to satisfy the conditions above.
Beta Was this translation helpful? Give feedback.
All reactions