Skip to content

Select option with getQueryData, getQueryState #3814

Answered by TkDodo
dongqui asked this question in General
Discussion options

You must be logged in to vote

no, that's not how it works. There is the cache that holds one entry per key. That is determined by what the queryFn returns, and this is what you'll get with getQueryData, and it's also what you'll see in the devtools.

select can be customized on top of that to create fine-grained subscriptions per observer. So you can do:

const useTodos = (select) => useQuery(['todos'], fetchTodos, { select })

and then use that like:

const useTodoCount = () => useTodos(data => data.length) // will return a number
const useFirstTodo = () => useTodos(data => data[0]) // will return the first todo or undefined

so the bottom line is: every usage of useQuery (= every observer) can have their own select, an…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@dongqui
Comment options

Answer selected by dongqui
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants