-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(react-query): Add usePrefetchQueries
hook
#8734
base: main
Are you sure you want to change the base?
Conversation
View your CI Pipeline Execution ↗ for commit 5f81470.
☁️ Nx Cloud last updated this comment at |
b3564b1
to
90802bc
Compare
usePrefetchQueries
hookusePrefetchQueries
hook
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #8734 +/- ##
===========================================
+ Coverage 44.09% 84.92% +40.82%
===========================================
Files 201 27 -174
Lines 8025 378 -7647
Branches 1770 111 -1659
===========================================
- Hits 3539 321 -3218
+ Misses 4058 49 -4009
+ Partials 428 8 -420
🚀 New features to boost your workflow:
|
90802bc
to
cfc4a49
Compare
export function usePrefetchQueries( | ||
options: { | ||
queries: ReadonlyArray<FetchQueryOptions<any, any, any, any>> | ||
}, | ||
queryClient?: QueryClient, | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is FetchQueryOptions<any, any, any, any>
really good enough here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I extracted out the rather through recursive SuspenseQueriesOptions
recursive type reducer from useSuspenseQueries
and modified it for using FetchQueryOptions
in this case. Although were not gonna be using all that type information in this case at this point.
4b70794
to
f8860e5
Compare
f8860e5
to
75ba89f
Compare
Any update on getting this merged? This would be very useful |
not a big fan of adding a new API for this. The types also shouldn’t need to be that complex because we don’t need to infer return types (there is no return type). what stops from having |
Yeah I wasn't sure if we really needed that complex type inference machine myself, wasn't sure what the standard was.
not a lot I think apart from the fact the library seems to have a convention that methods that take in multiple queries are called |
You'd go with something like this I think: // usePrefetchQueries.tsx
export function usePrefetchQueries<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options:
| FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>
| {
queries: ReadonlyArray<
FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>
>
},
queryClient?: QueryClient,
) {
const client = useQueryClient(queryClient)
const queryArray = 'queries' in options ? options.queries : [options]
for (const query of queryArray) {
if (!client.getQueryState(query.queryKey)) {
client.prefetchQuery(query)
}
}
}
// usePrefetchQuery.ts
/**
* @deprecated Use `usePrefetchQueries` instead.
*/
export function usePrefetchQuery<
TQueryFnData = unknown,
TError = DefaultError,
TData = TQueryFnData,
TQueryKey extends QueryKey = QueryKey,
>(
options: FetchQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
queryClient?: QueryClient,
) {
return usePrefetchQueries(options, queryClient)
} with |
okay, I think it’s more complicated than that, my bad. It obviously needs the complex types because otherwise you can’t do this:
|
Not sure that's the case I think. Pushing new tests now, will go over this in a bit, but I don't think the default |
0179035
to
5f81470
Compare
No description provided.