Replies: 1 comment 3 replies
-
All my thoughts on that topic, including pros and cons, are in my blog: https://tkdodo.eu/blog/react-query-data-transformations |
Beta Was this translation helpful? Give feedback.
3 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.
-
I was wondering how to conceptually approach the following pattern with react-query:
Possible solutions I've come up with so far:
single hook that does data fetching and merging, returning the result:
Advantage; I get the benefit of todos and users both be cached, can do optimistic updates on the cache etc. Disadvantages; I have to do more work in the view components to see if the
mergedTodosWithUsers
is ready to be used and the individual loading indicators of thetodosQuery
andusersQuery
do not make much sense.Another solution would be to create an async function, fetch all required data + do the transformations/merging in that function, return the results via a promise and use that function inside a
useQuery
hook:This way I have the benefit of using result of
useQuery
for its state flags and can trust thatisSuccess
really means that the data is available in my components, I only miss the caching of individual users and todos.A combination of the two approaches would be even better, but I could not figure out how to use a
react-query
inside an async function and await its response. If that would work, I could useuseQuery
calls for individiual data fetching, pass the queries to another hook that would call an async function in its ownuseQuery
function.Something like (which does not work of course):
Any thoughts on the above would be very welcome!
Beta Was this translation helpful? Give feedback.
All reactions