[rtk-query] what is the purpose of unsubscribe when dispatching initiate manually #3012
-
https://redux-toolkit.js.org/rtk-query/api/created-api/endpoints#initiate in the doc above states
I am struggling to understand what exactly it does and when it's and it's not needed. Looking at the source code, it seems like resetting cached data when unsubscribe (deprecated, to use reset) is called to summarize Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@cjhdom : RTK Query uses a form of reference counting to know how many "subscriptions" still care about a given cache entry. Normally, one component+query hook call === one subscription. So, if you had 10 components that all all When the count of active subscriptions goes to 0, RTKQ sets a timer, and if the subscription count is still 0 when the timer expires, RTKQ will remove that cache entry: This works because the query hooks automatically unsubscribe in If you're manually dispatching You can see an example of unsubscribing manually here: |
Beta Was this translation helpful? Give feedback.
@cjhdom :
RTK Query uses a form of reference counting to know how many "subscriptions" still care about a given cache entry. Normally, one component+query hook call === one subscription. So, if you had 10 components that all all
useGetSomeDataQuery("abc")
, that's 10 active subscriptions to that cache entry.When the count of active subscriptions goes to 0, RTKQ sets a timer, and if the subscription count is still 0 when the timer expires, RTKQ will remove that cache entry:
This works because the query hooks automatically unsubscribe in
useEffect
cleanup functions, so when a component unmounts it tells RTK…