Replies: 1 comment 7 replies
-
From day 1, the recommendation has always been to encapsulate reusable query logic into custom hooks. export default function useProduct (productId, options) {
return useQuery(['products', productId], () => fetchProductById(productId), options)
} Does this not suffice? |
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
The react-query API is great as is, but as I'm adding more queries and hitting scenarios for invalidating caches and/or prefetching and refetching I'm wondering if folks have been playing around with making queries and query concerns more portable.
I may have to invalidate a cache or prime a cache in different parts of my codebase (usually on mount or as an app initially renders) and I may need to do this for the same query in multiple locations (mostly due to legacy setup and wanting to migrate things over time vs big refactor). The issue I'm seeing is needing to pass a key builder around when performing some of these action in addition to the method for interacting with the cache. I think the use case isn't so important since I think the general issue of needing the cache key (when wanting to clear an exact cache) could be common enough
That example is trivial and the alternative without any abstractions isn't much different or that complex, but I think encapsulating the prefix, type or brand (whatever you'd want to call that key prefix) has benefits.
Wondering if there's a point to be made to maybe include some of the react-query cache utilities on the hook or near the hook.
Could play around with more ergonomic and friendly exports, but in general all the utility functions would have the same signature as the hook (where building a key is concerned).
Anyway not driving this too seriously at the moment, but was curious if anyone else was starting to work on patterns/higher-level APIs on top of react-query.
Beta Was this translation helpful? Give feedback.
All reactions