T
TanStack3y ago
frail-apricot

Best way to handle combining 2 data objects.

Hi, so my question has to do with combining 2 separate fetched data objects. The current solution I have right now is I create a local state and then set that local state a new custom object containing the 2 fetched data objects from react query. I was wondering instead of doing this hack, is there a way to directly handle combining 2 data objects into 1, straight from react query without having to rely on creating a local state.
5 Replies
quickest-silver
quickest-silver3y ago
Hi you could define 2 queryKey/queryFn couples for your objects and a third one which would combine the results of the other queryFns. This way you could use useQuery for object 1 or object 2 while still being able to have the combination of both. All of this would rely on cached data.
frail-apricot
frail-apricotOP3y ago
Ok ty I'll look into it!
quickest-silver
quickest-silver3y ago
You could have a look here for some inspiration : https://codesandbox.io/s/fi882o
GLabat
CodeSandbox
react-query and queries composition - CodeSandbox
Illustrate how queries can be composed into a new one to simplify usage.
frail-apricot
frail-apricotOP3y ago
ty!
quickest-silver
quickest-silver3y ago
Let me know if some explanations are needed

Did you find this page helpful?