Course lesson
Access the RTK Query Cache in a Memoized Selector Using an Endpoint's select() Method
This lesson covers two important principles. The first is memoized selectors with createSelector and RTK Query's built-in endpoint selectors.
- Duration
- 2 min
- Access
- Free
- Transcript
- Retained from source evidence
This lesson covers two important principles. The first is memoized selectors with createSelector and RTK Query's built-in endpoint selectors.
RTK Query doesn't give you an easy way to access its cache manually but it does give you these enpoint selector helpers that allow you to reach into the cache when you need it.
Using these memoized endpoint selectors are recommended because they only calculate a new value any time the data arguments change while the previous selectors we had would be re-run on every re-render.
While not mentioned in the lesson you don't need to use endpoint selectors with createSelector. This would work perfectly fine:
export const getServicesForLuckyDog = (state) => {
const { data: dogs } = api.endpoints.getDogs.select()(state);
const { data: services } = api.endpoints.getServices.select()(state);
// if you don't have a lucky dog, show all of the services
const dog = dogs?.[state.dogs.luckyDog];
if (!dog) {
return services;
}
// ...
};