0

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 ».

Mik378
  • 21,881
  • 15
  • 82
  • 180

1 Answers1

2

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).

phry
  • 35,762
  • 5
  • 67
  • 81
  • Thanks for your answer. Edge case? That’s a very very current scenario in my apps. It is very problematic according to me to not handle it. That’s a big reason why I can’t use RTK-Query. – Mik378 Feb 12 '23 at 09:10
  • It would require far more code with the proposed solution than when not using RTK-Query :s Why not provide a kind of selectFirst, that grabs the first matching state no matter the cache key is, very often the sole entry in that sort of scenario. – Mik378 Feb 12 '23 at 09:24
  • Total edge case. Almost all endpoints are either called without argument, or more than once with different arguments. Because it would *very* often just select the wrong thing and often lead to people shooting themselves in the foot. "First" implies an order, and as I said, there cannot be a guarantee of an order. As said, you can implement that in a few lines in userland, but we are not going to provide a tool that will lead to people shooting themselves in the foot so easily. – phry Feb 12 '23 at 09:48
  • For instance you can have a Quiz’s question on the screen and on the act of answering (with arguments) you put that answer validation in the state. I want to be able to retrieve that answer from a selector without argument and compute some derived state, because there will always be one answer persisted in the state at a time. With that way of doing you wouldn’t need to store the arguments in a global state to retrieve the answer validation. I want to do : answerValidationEndpoint.selecFirst() without arguments. Otherwise it would be a code smell for me. A warning in the doc would me enough. – Mik378 Feb 12 '23 at 09:51
  • Yes. And now think about how many real applications are quizzes. Put the number of people who would benefit from this against the number of people who accidentally have multiple cache entries and accidentally retrieve the wrong ones, either resulting in a bug, or in a multiple-hour debugging session. Could even happen in a quiz if a person were to take the quiz twice. Please implement this in an individual solution in your own application. Just take the first entry from `state.api.queries` that starts with your endpoint name. – phry Feb 12 '23 at 09:56
  • I understand your vision and I respect it. Mine is that such an highlight on caching is far more overkill in most apps than dealing with that way of dealing with derived state computing. I thought indeed about implementing that kind of selectors myself if I choose to use RTK-Query, a kind of regexp, thanks :) – Mik378 Feb 12 '23 at 09:58
  • 1
    Also, a quiz answer should not be a query, but a mutation, and there you could just use `fixedCacheKey` to set a known cache key that you can use in selectors anywhere. You might be able to do this for a query as well by providing [serializeQueryArgs](https://redux-toolkit.js.org/rtk-query/api/createApi#serializequeryargs) – phry Feb 12 '23 at 10:00
  • In that case it’s a mutation, in a lot of other scenarios it can be a query. – Mik378 Feb 12 '23 at 10:06