reactjsrtk-query

How to clear RTK Query Cache after querying data?


I want to remove my cache from RTK Query because i am having several search fields into my project and once i use an api search field (rtk query) to search something from an API it give me results but it saves in RTK query cache.

After that if i would use another search field (other then rtk query) to search something i get my results but then right after that if i would go on to that same search field which is having an API(rtk query underneath) and search for the same query which i did first time then i can't get results again because it is saved in Cache and RTK query won't run.


Solution

  • SOLUTION: This will immediately remove all existing cache entries, and all queries will be considered 'uninitialized'. So just put the below code into onClick or according to your scenario so when you hit an enter request will go and cache would also be clear. below here api is your name of an api which you would set in your rtk query in store.

       dispatch(api.util.resetApiState());
    

    For more info please have a look in documentation https://redux-toolkit.js.org/rtk-query/api/created-api/api-slice-utils