Skip to content

[rtk-query] what is the purpose of unsubscribe when dispatching initiate manually #3012

Answered by markerikson
cjhdom asked this question in Q&A
Discussion options

You must be logged in to vote

@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…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@cjhdom
Comment options

Answer selected by cjhdom
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants