T
TanStack•4y ago
other-emerald

Can't get query data

I've constructed a query key for a paginated list with filters and sorting like this:
const useProducts = (currentPage = 1, activeFilters, activeSorting) => {
return useQuery(['products', currentPage, activeFilters, activeSorting], {
keepPreviousData: true
})
}
const useProducts = (currentPage = 1, activeFilters, activeSorting) => {
return useQuery(['products', currentPage, activeFilters, activeSorting], {
keepPreviousData: true
})
}
I'm trying to get the query data like this:
const queryData = queryClient.getQueryData(['products']) // undefined

const queryData = queryClient.getQueryData(['products', currentPage, activeFilters, activeSorting]) // full data
const queryData = queryClient.getQueryData(['products']) // undefined

const queryData = queryClient.getQueryData(['products', currentPage, activeFilters, activeSorting]) // full data
What am I doing wrong? Why can't I get the whole data with just ['products'] ?
5 Replies
rare-sapphire
rare-sapphire•4y ago
Because there are multiple cache entries. Try getQueriesData, it matches fuzzily
other-emerald
other-emeraldOP•4y ago
Ty! Is this the right way to create the query keys? Or should I change something? Might try the query key factory
rare-sapphire
rare-sapphire•4y ago
it's fine
other-emerald
other-emeraldOP•4y ago
alright, ty! @TkDodo 🔮 if I want to do optimistic updates with this should I also use setQueriesData? Or does setQueryData works fine? 🤔
rare-sapphire
rare-sapphire•4y ago
one sets it for all keys, the other doesn't

Did you find this page helpful?