Repository navigation
Replies: 2 comments
|
A wrapper hook is a good fit here. There isn't an endpoint-level equivalent of the hook's For example, with your generated API hook and typed Redux selector: import { skipToken } from '@reduxjs/toolkit/query';
export function usePermittedReportQuery(
id: string,
options: { skip?: boolean } = {},
) {
const canRead = useAppSelector(selectCanReadReports);
const result = api.useGetReportQuery(
canRead ? id : skipToken,
options,
);
return {
...result,
canRead,
data: canRead ? result.data : undefined,
currentData: canRead ? result.currentData : undefined,
};
}Call that hook unconditionally from screens. A caller's If you also want a centralized check for imperative
So I'd keep reactive skipping in the shared hook, and treat any request-layer guard as an additional check. Neither is an authorization boundary: enforce permissions on the server, and handle logout/account-switch cache cleanup separately. Skipping also does not revoke an already-started request. I tested the wrapper with RTK 2.13.0 and React 19.3.1 through denied → granted → revoked permissions, argument changes while denied, and an explicit caller skip. I also verified the three References: conditional fetching, |
|
RTK Query doesn't have a built-in endpoint-level A good approach is to centralize the permission check in a custom hook for each protected endpoint, using For example, with Redux Toolkit and TypeScript: import { skipToken } from '@reduxjs/toolkit/query';
import { useAppSelector } from './store';
import { api } from './api';
export function useProtectedDataQuery() {
const canViewData = useAppSelector(
(state) => state.auth.permissions.includes('data:read')
);
return api.useGetProtectedDataQuery(
canViewData ? undefined : skipToken
);
}This example assumes Your components can now call For multiple protected endpoints, you can create a small wrapper hook per endpoint, or centralize permission enforcement in a custom |
Uh oh!
There was an error while loading. Please reload this page.
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
skiporskipToken?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
skipthe 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?All reactions