Read RTK-Query state in a selector without having to pass the cache key

846 Views Asked by At

Very simple:

Let’s say we use RTK-Query to retrieve the current weather.
For that, I pass to the endpoint the arg ‘Paris’ as city.
It will serve the current weather of my « game ».

Then later, in a Redux selector, I need to compute some derived state based on that current weather.

How to read the state without having to pass the cache key « Paris »?
Indeed, that knowledge of « Paris » was only necessary at the beginning of the app.

It seems that with RTK-Query we’re stuck since you have to pass the argument that was used (the cache key) to the endpoint#select method.

Am I right in saying that RTK-Query does not currently allow that kind of state reading:

« select that current (and single) store entry X whatever the argument that was needed at loading time is ».

1

There are 1 best solutions below

8
On BEST ANSWER

No, since that's an edge case.
Usually, there are multiple cache entries per endpoint, and there is also no concept of a "latest entry" or something, since multiple different components can render at the same time, displaying different entries for the same endpoint - the concept of a "latest" there would come down to pretty random React rendering order.

The most common solution would be to just safe "Paris" somewhere in global state to have it readily available, or to write your selector against RTKQ store internals by hand (although there might be changes to the state internals in the future).