papigee
papigee

Reputation: 7378

React Native Clearing cache of Image.prefetch

Looking at the documentation related to the caching logic (Image.prefetch) of the Image component (https://reactnative.dev/docs/image#querycache), the closest thing to caching mentioned there is querycache. I do not see anything about how to clear the cache so it does not amass too many images over time. My question is how does the caching here work? Does it expire and then automatically cleared or do we have to handle that housekeeping part? If we have to handle it, I will appreciate some suggestions on how to do that. Ideally, I would want a way to get the cache key and be able to delete it from the cache - similar to the API exposed by Async Storage (https://reactnative.dev/docs/asyncstorage).

Upvotes: 2

Views: 1069

Answers (1)

Anılhan Hasceviz
Anılhan Hasceviz

Reputation: 751

As far as Image component does not provide an API for clearing the cache. When the number of images in the cache reaches a certain limit, you can delete them.

The number of images in the cache can be checked periodically and certain images can be deleted if necessary.

const keys = Object.keys(Image.queryCache);
keys.forEach((key) => {
  Image.removeFromCache(key);
});

Upvotes: 0

Related Questions