Replies: 3 comments 6 replies
-
If I’m not mistaken, you want the select option:
see also: https://tkdodo.eu/blog/react-query-data-transformations |
Beta Was this translation helpful? Give feedback.
-
Pretty much, except that the select works across N queries. So I guess more
like query composition. I'm using this to build a more complex state graph
from a couple simple queries.
…On Thu, Apr 22, 2021, 10:29 PM Dominik Dorfmeister ***@***.***> wrote:
If I’m not mistaken, you want the select option:
const useTodosQuery = (select) => useQuery(['todos'], fetchTodos, { select })
const useTodosCount = () => useTodosQuery((data) => data.length)
const useTodo = (id) => useTodosQuery((data) => data.find((todo) => todo.id === id))
see also: https://tkdodo.eu/blog/react-query-data-transformations
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2178 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AA5PZTTZDZ5RYYLNBHKO5ZLTKEAUTANCNFSM43NMRTUQ>
.
|
Beta Was this translation helpful? Give feedback.
-
This is a great question, and I found this thread searching: Composition is a fundamental (and powerful) design principle for managing complexity in software, and I would also find a reference for query composition valuable. I, too, looked at I essentially have the same use case as @rstone770 , creating derived data from multiple query results (in different contexts), while still propagating state. Would modeling something after Or how about returning a promise from We would then be able to create an array of promises from multiple custom query hooks, and return that from a standard |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
It would be really nice if there was some kind of recipe, hook, ect for derived queries. This is super useful when you have some derived data (similar to redux selectors) that you want but but also want to flow dependent query state down in order to propagate errors, use established react-query helpers, or simply to keep your async data related apis consistent.
In the above example, we wait for
allLocations
to besuccess
then we pass down thedata
into the derivation function which just simply returns a Promise as normal queries do. If an findAllLocations becomes faulted, we obviously expect the derived state to also be faulted.Here is a multi query example.
I have something locally that just finds the characteristic query and copy all values from it except
remove
andrefetch
which are more or less replaced with noops/identity since i couldn't decide if i should propagate those calls down too. There are also issues when deciding how to populate/aggregate certain meta fields like failureCount.I think this is a common enough pattern that it should be offered by as first party solution.
Beta Was this translation helpful? Give feedback.
All reactions