-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useMutation cacheTime not work #5058
Comments
no, that's just a misunderstanding of how |
@TkDodo Im confuse about the purpose of cacheTime in mutation |
is that explanation not helpful? cacheTime is about garbage collection, which is why we've renamed it to |
@TkDodo so, there is no way that mutation can cache data |
it does cache, but each call to
creates two cache entries, one where the variables are I think I can better help if you're describing the problem you're trying to solve rather than asking a specific question about caching mutations |
Thank you for your kindness , I’d like to cache data like useQuery in useMutation. when fetch data again ,return previous cache data first , and then fetch new data |
okay but that's not how mutations work, sorry. Each invocation is a separate thing. Also, "I want to cache data in useMutation" is not your problem - it's a potential solution that you think will solve your problem... |
@TkDodo After looking at some articles, I found that I misunderstood, when I want to cache data, I should use useQuery, even if it is the |
@TkDodo I'm trying to get a cache-able callback function returning a promise. Is this supported by your library? What is actually being cached here if not the data returned from the promise? My use case is to call the mutateAsync with a value 'hello' and return the response of an api call, the next time I call it with 'hello' I would expect to get the cached response. Then when another value is passed 'goodbye' I would expect a different cache entry to be created and a new api call to be made with different response. I'll illustrate: const a = await mutateAsync('hello'); const b = await mutateAsync('goodbye'); const c = await mutateAsync('hello'); If this is not the intended behavior then I am not sure what the purpose of the cache would be, am I missing anything? |
mutations don't cache like that because mutations are imperative. You would sure get that behaviour if you use https://tanstack.com/query/v5/docs/reference/QueryClient#queryclientfetchquery |
Thanks @TkDodo this looks like what I was incorrectly trying to do with useMutation. I’ll go in that direction. Out of curiosity I am wondering what is getting cached with the mutation. Is it just a record of the calls? If there’s further reading I’d be grateful. |
the mutation is cached alongside its state (pending / success / error), it has callbacks that will still run even if the component was already unmounted when it finishes (needed for invalidation), you can read in other components with |
I have the case that when i call mutation with the same input i want mutation to fetch data from api instead of using data from cache. I don't know how can i solve this as when i call the mutate with the same input it return cached data even if the data has been changed from api, any help will be appreciated |
Describe the bug
In my understanding, after setting cacheTime, the data should return the value from the previous request, rather than undefined.
Your minimal, reproducible example
https://github.com/ShiChenCong/useMutation-cacheTime-demo
Steps to reproduce
undefined
[1,2,3]
undefined
(this step, In my understanding, should log[1,2,3]
directly)[1,2,3]
Expected behavior
return previous value rather undefined
How often does this bug happen?
None
Screenshots or Videos
No response
Platform
macOS
TanStack Query version
4.24.10
TypeScript version
No response
Additional context
reproduce repo
https://github.com/ShiChenCong/useMutation-cacheTime-demo
, andmutation
did not show in the devtool too.The text was updated successfully, but these errors were encountered: