1

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.

Jamal Ashraf
  • 569
  • 6
  • 9

2 Answers2

1

As mentioned in Jamal's answer above, you can use resetApiState() to clear the entire api cache. If you need to clear the cache for a specific endpoint you can use reset() function returned by the useMutation() hook https://redux-toolkit.js.org/rtk-query/usage/mutations#performing-mutations-with-react-hooks

[postData, {reset: resetPostDataCache}] = usePostMutation()
// this will reset cache for specific endpoint
resetPostDataCache()
Eldenhor
  • 11
  • 1
  • 2
0

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

Jamal Ashraf
  • 569
  • 6
  • 9