Home > front end >  How to clear & invalidate cache data using RTK Query?
How to clear & invalidate cache data using RTK Query?

Time:11-21

I was facing a problem for sometime, that was I'm unable to clear cache using RTK query. I tried in various ways but cache data is not clear.

I used invalidatesTag in my mutation query and it called the api instantly. But in this case I want to refetch multiple api again, but not from any rtk query or mutation. I want to make the api call after some user activity like click. How can I solve this problem?

I made a separate function where I return api.util.invalidateTags(tag) or api.util.resetApiState().
this is my code-snipet:-

` const api = createApi({.....})

export const resetRtkCache = (tag?: String[]) => {
const api = 
if (tag) {
    return api.util.invalidateTags(tag)
} else {
    return api.util.resetApiState()
}

}`


& I called it using dispatch method from other files

`const reloadData = () => {
    dispatch(resetRtkCache())
 }`

but here cache data is not removed.I think dispatch funtion is not working. I don't see the api call is being sent to server in the browser network.

CodePudding user response:

But in this case I want to refetch multiple api again, but not from any rtk query or mutation. I want to make the api call after some user activity like click. How can I solve this problem?

So if I understood correctly what you want to achieve is to fetch some api that you have in RTK only after some kind of user interaction?

Can't you just define something like this?

const { data } = useGetYourQuery({ skip: skipUntilUserInteraction })

Where skipUntilUserInteraction is a component state variable that you will set to true and update to false based on the user interaction you need? (e.g. a click of a button). So essentially on component render that specific endpoint will be skipped but will be fetched after the interaction that you want will happen?

CodePudding user response:

wow, you actually asking so many questions at once. but I think you should definitely read the documentation because it covers all the questions you have. so trying to answer your questions one by one.

I used invalidatesTag in my mutation query and it called the api instantly.

invalidating with Tags is one of the ways to clear the cache. you should first set the tagTypes for your API then use those tags in mutation queries and tell the RTK query which part of entities you want to clear.

I want to refetch multiple APIs again

you can customize the query inside of a mutation or query like this example and by calling one function query you can send multiple requests at once and if you want to fetch the API again after the cache removed you do not need to do anything because RTK query will do it for you.

I want to make the API call after some user activity like click every mutation gives u a function that you can pass to onClick like below:

import { use[Mymutation]Mutation }  from 'features/api';

const MyComponenet() {
    const [myMutationFunc, { isLoading, ...}] = use[Mymutation]Mutation();

    return <button type='button' onClick={myMutationFunc}>Click for call mutaion</button>

}

and remember if you set providesTags for your endpoint which you were defined in tagTypes by clicking on the button and firing up the myMutationFunc you will be clearing the cache with those tags.

and if you looking for an optimistic update for the cache you can find your answer in here.


async onQueryStarted({ id, ...patch }, { dispatch, queryFulfilled }) {
        const patchResult = dispatch(
          api.util.updateQueryData('getPost', id, (draft) => {
            Object.assign(draft, patch)
          })
        )
        try {
          await queryFulfilled
        } catch {
          patchResult.undo()
        }
      }
  • Related