reduxjs / redux-toolkit

The official, opinionated, batteries-included toolset for efficient Redux development
https://redux-toolkit.js.org
MIT License
10.74k stars 1.18k forks source link

Supporting "hard invalidate" the "specific" cached data with "key" instead of "soft invalidation" #2814

Closed prabhasjoshi closed 1 year ago

prabhasjoshi commented 2 years ago

Similar questions asked on stackoverflow here

In RTK query - when I define my invalidation with invalidateTags - after I invalidate a particular key - it only refetches that data.

How can I delete cached data by invalidation or any other strategy.

The use case I have is this - I have a getter api which gives the details of an entity.

And another mutation API which is performing some action on this entity - which invalidates the getter key of the entity.

But when I am showing the detail of the entity "after the operation" - invalidation only causes isFetching to be true, isLoading is not true.

And showing stale data "while action is being performed" with old data will be an unsafe thing to do.

Hence I want to delete the cached data - "after the action" - so that the loader is shown.

Currently the "invalidation" of data is "soft invalidation" - is there a way to "hard invalidate" the "specific" cached data with "key"

Also, how do I clear the cache of some data using a custom hook ?

prabhasjoshi commented 2 years ago

@markerikson @phryneas - please help :)

phryneas commented 2 years ago

There's pretty much no way to explicitly remove all of that. currentData will make sure to give you data for the current cache entry, but if that is getting refetched it will still give you the old value in the meantime.

What exactly keeps you from reading isFetching and showing a spinner here?

prabhasjoshi commented 2 years ago

@phryneas There are 2 scenarios:

Suppose entity is Transaction - and there are many rows of transactions being show - when u click on a transaction - an inline detail view of the transaction is shown (in same page)

Scenario "1" - when the user is clicking between transaction rows in quick succession - we want to show "stale data" and a "progressive loader on top". Result - only "isFetching" will be true in this case (except when detail loaded for first time)

Scenario "2" - when an action is happening on 1 of the transaction and showing "stale data" will send a wrong message to users - so we want to show a plain loader and no data. Result - here also only "isFetching" will be true.

There is no way to distinguish between the 2 - because in both cases "user quickly loading details" or "doing an action" - same flag is true and there is no way to delete the cahed data associated with a specific key