Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

RTKQ: useLazyQuery - calling of trigger function with different arguments makes unsubscribe #4280

Open
greskosa opened this issue Mar 20, 2024 · 3 comments

Comments

@greskosa
Copy link

Hello 馃憢

Documentation for useLazyQuery says about one of the feature:

'Subscribes' the component to keep cached data in the store, and 'unsubscribes' when the component unmounts

But there is no mention about unsubscribing when trigger function was called a few times with different arguments.

Example:

const preferCacheValue = true; // use the value from a cache if exists
const [trigger, { data }] = useLazyEnpointQuery();

trigger({ id: 1 }, preferCacheValue); 
// asynchronous fetching of data for id 1 and putting it to the cache


trigger({ id: 2 }, preferCacheValue); 
// first action [nameOfApi]/subscriptions/unsubscribeQueryResult is called
// that causes clearing of cache for previous id, and then
// asynchronous fetching of data for id 2 and putting it to the cache

It always makes unsubscribe and clearing of cache for previous id but I need to keep both results in the cache and clear them only on component unmount. Is there any way to control that?

Thanks for help!

@greskosa greskosa changed the title RTKQ: Calling of trigger function with different arguments of useLazyQuery makes unsubscribe RTKQ: useLazyQuery - calling of trigger function with different arguments makes unsubscribe Mar 20, 2024
@phryneas
Copy link
Member

No, that's not supported. The hooks are generally always only meant to monitor the "last result", so any results previous to that will be unsubscribed.

You probably need hand-written logic that dispatches endpoint.initiate() calls.

@greskosa
Copy link
Author

@phryneas thanks for the quick reply. Is there any chance to update the documentation to have a more precise description of how cache clearing works for this particular case?

@phryneas
Copy link
Member

A PR would be very welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants