-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
RTK Query: Conditionally skip the query from the endpoints definition for hooks #4810
Comments
No, there is currently no way to skip a query from within the endpoint definitions. It's a thought that's come up a couple times, but it's never been something we've seriously tried to figure out. Wrapping the hooks with a custom hook is probably the right answer here. |
Ahh bummer. I would be open to looking into it and creating a PR if that something you would consider and or would want. I spent some time looking into the hook builder, and though about being able to pass a selector in the endpoint definition that returns a boolean. If it's true, skip the call like how it checks for the skipToken. https://github.com/reduxjs/redux-toolkit/blob/master/packages/toolkit/src/query/react/buildHooks.ts#L1085 |
Conceptually, this would probably need to be integrated into the existing redux-toolkit/packages/toolkit/src/query/core/buildThunks.ts Lines 526 to 573 in aaeda15
Honestly, it might even only be a 3-4 line change. Don't destructure if (endpointDefinition.condition && endpointDefinition.condition(arg, options) === false) {
return false
} |
I can play around with that and see how it works. Would that still keep the hook in a |
It should, yeah, because no actions get dispatched at all. |
Keep in mind that this will mess up the TS types a lot, which is the main reason we haven't done this yet. |
I spent some time on this today, and maybe I wasn't setting this up correctly, but as soon as the hook renders it is in a loading state. I tested this in buildThunks by calling the new function, as well as just adding a check for the endpoint name and returning
If I add
|
Ah, yeah, we do intentionally have an override selector that says "if it's |
Would it be expected to keep it in a loading state for this use case? |
Well, this is where the increasing number of options and behavioral interactions causes increasing complexity :) The selector code has been able to make this optimization based on a valid assumption that "this thunk will be dispatched in an effect right after this, therefore we can safely assume that the cache entry will go to a status of If we now have a So, what's probably needed is to extend the logic in the pre-selector to be along the lines of In other words, it should keep the existing Also, I just glanced at the code. I was going to suggest moving it right after It's feasible, but it'll take some shuffling of the code. |
@markerikson maybe I still don't fully understand the nuances of it but I just added the check to the same condition for the skip and skipToken. It's still a draft MR, for the idea and still needs some work around documentation. |
Hey @markerikson, if you have a second, could you take a look at the MR and let me know what you think? |
@SYoder1 I won't have the mental space available to look at that until after I get the infinite query feature shipped. (Fortunately, that's getting fairly close!) |
Is there a way to set a condition or even a callback on a endpoint to conditionally skip it, instead of at the hook using skip or skipToken?
I have a few queries in our app that we should only make if the user has a specific permission. These queries are all around our app, and currently we are having to add logic to ever screen in order to set the hook to skip the query. This is some unnecessary boilerplate code for most queries, and we have thought about making a wrapper hook to extend the skip logic to include the permissions. Is there a better way?
The text was updated successfully, but these errors were encountered: