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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useMutation cacheTime not work #5058

Closed
ShiChenCong opened this issue Mar 1, 2023 · 13 comments
Closed

useMutation cacheTime not work #5058

ShiChenCong opened this issue Mar 1, 2023 · 13 comments

Comments

@ShiChenCong
Copy link

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

  1. reproduce repo
  2. pnpm install
  3. pnpm run dev
  4. click button
  5. log undefined
  6. log [1,2,3]
  7. click button again
  8. log undefined(this step, In my understanding, should log [1,2,3] directly)
  9. log [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, and mutation did not show in the devtool too.
image

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 1, 2023

no, that's just a misunderstanding of how useMutation works. Every time you call mutate by clicking the button, a new entry in the mutationCache is created. cacheTime just decides how long that entry will stay in the cache after you've unmounted the component

@TkDodo TkDodo closed this as not planned Won't fix, can't repro, duplicate, stale Mar 1, 2023
@ShiChenCong
Copy link
Author

@TkDodo Im confuse about the purpose of cacheTime in mutation

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 1, 2023

cacheTime just decides how long that entry will stay in the cache after you've unmounted the component

is that explanation not helpful? cacheTime is about garbage collection, which is why we've renamed it to gcTime in v5. usually, you don't need to care about it...

@ShiChenCong
Copy link
Author

@TkDodo so, there is no way that mutation can cache data

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 1, 2023

it does cache, but each call to mutate is a different cache entry:

const { mutate } = useMutation({
  mutationFn: updateName
})

mutate('hello')
mutate('world')

creates two cache entries, one where the variables are hello and one where they are world. it's two different things, like as if you're switching between two cache keys.

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

@ShiChenCong
Copy link
Author

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

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 1, 2023

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...

@ShiChenCong
Copy link
Author

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

@dcatoday-cloud9
Copy link

@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');
// makes a call and returns 'hi'

const b = await mutateAsync('goodbye');
// makes a call and returns 'bye'

const c = await mutateAsync('hello');
// finds entry in cache and returns 'hi'

If this is not the intended behavior then I am not sure what the purpose of the cache would be, am I missing anything?

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 27, 2024

mutations don't cache like that because mutations are imperative. You would sure get that behaviour if you use const data = await queryClient.fetchQuery(...), given that you pass a sufficient staleTime to it.

https://tanstack.com/query/v5/docs/reference/QueryClient#queryclientfetchquery

@dcatoday-cloud9
Copy link

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.

@TkDodo
Copy link
Collaborator

TkDodo commented Jan 28, 2024

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 useMutationState etc

@root123-bot
Copy link

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

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

No branches or pull requests

4 participants