If you're using React Query you certainly know how the useQuery hook works. Some example similar to the ones you find in the React Query documentat...
For further actions, you may consider blocking this person and/or reporting abuse
Best thing to do is to create specific functions for your queries:
and so on.
Hooks are composable so make the most of it.
This will not help you when trying to invalidate the query. Of course you could create a invalidate function/hook for each query, too. But then the pattern starts to look not much different from what I'm proposing.
Or you can even use a combination of both: Create custom hooks PLUS use the key/query objects.
Invalidation is a whole other problem for sure. I usually have an invalidate hook that invalidates all queries within a certain domain i.e.
I've been using react query (and vue query) since day 1 and it's important we get some good/consistent patterns in place
Invalidating is easy why not just,
Works for me with normal queries and custom hooks.
Yes sure. But how would make sure that keys stay consistent in your mid-size to large React application? What if the key used by the query at another place of the application (by another person)? How do you make sure the invalidation also gets updated?
This is what my pattern tries to solve.
I don't understand what you mean by "invalidation also gets updated" what you mean by updated? Or do you mean other devs on your team will accidentally use an already existing key for a different query?
I prefer to create a separate key map per API namespace. It's looks like this:
In complex apps, this have several advantages:
allkeyrecentlyWatchedkey, since all other recently watched keys are based on thisAlso, I don't use
useQueryacross the app. I create custom query hooks in single API packages divided to API namespace and use it in app.I really like this approach. Thanks for sharing.
I think this post explain the concept mentioned by @dikamilo tkdodo.eu/blog/effective-react-que...
this topic requires much deeper elaboration than this
Isn't that the truth for 99% of all topics in coding?