-
Hello everyone. I'm upgrading an older react codebase from class components to functional with react-query and zustand and I've come across a situation where I'm not sure about the best approach. I have certain endpoints that are cached on the backend. To use the cache, the query can add '&fromCache=true' to the query string, for instance for fetching posts.
The issue arises after a mutation, where the query is supposed to be called with 'fromCache: false'. If I invalidate the query with One solution would be to include a disabled query in the component where the mutation occurs and call refetch, but is that the best solution?
Another solution could be putting the 'fromCache' value into a zustand store and reading that value in the fetch function which might also work? Thanks for any help :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
yeah, this is a surprisingly tricky one. The API seems a bit weird, but I guess it's out of the question to change it. My thinking is that usually, the client shouldn't care about such things. The mutation itself should probably fill the cache on the server - it's not the client's responsibilities to instruct the server when to use a server-side cache and when not. I think storing that value somewhere else (zustand sounds fine) and then reading it in the What you'd want is to simply call: |
Beta Was this translation helpful? Give feedback.
yeah, this is a surprisingly tricky one. The API seems a bit weird, but I guess it's out of the question to change it. My thinking is that usually, the client shouldn't care about such things. The mutation itself should probably fill the cache on the server - it's not the client's responsibilities to instruct the server when to use a server-side cache and when not.
I think storing that value somewhere else (zustand sounds fine) and then reading it in the
queryFn
in a non-reactive-way seems like the best approach. With non-reactive, I mean do not have aconst fromCache = useStore(store => store.fromCache)
outside the queryFn and then close over it, because you will run into stale closure p…