8
Cross-platform support for easy disk caching in React Native
source link: https://reactnativeexample.com/cross-platform-support-for-easy-disk-caching-in-react-native/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Caching
Cross-platform support for easy disk caching in React Native
Jul 30, 2021
1 min read
React Native Disk Cache
Cache an object to disk, and refresh the value after a certain amount of time.
Under the hood, the data is stored as simple serialized JSON.stringify()
output, at a file located at {DocumentDirectoryPath}/__caches__/{store}/{timestamp}
.
Example
import { fromDiskCache } from 'rn-disk-cache'
/**
* Will be loaded live and written to disk, or read from disk cache if there is
* a non-stale cache on-disk already.
*/
const value = await fromDiskCache(
/** Name of this store. */
'myLargeList',
/** How to get the updated value when stale. */
async () => {
const update = await getValueOverNetworkOrOtherAsyncProcess();
return update;
},
/** Cache duration. **/
60,
);
Previous Post
Dynamic Styles for React Native and Expo Web
Subscribe to React Native Example for Android and iOS
Get the latest posts delivered right to your inbox
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK