Como armazenar em cache a imagem no React Native

Nov 29 2022
Vou mostrar como salvar imagens no cache. Usaremos esta biblioteca para salvar a imagem: react-native-fs.

Vou mostrar como salvar imagens no cache. Usaremos esta biblioteca para salvar a imagem: react-native-fs .

Para começar a usar o fluxo de arquivos, primeiro importe react-native-fs :

import RNFS from 'react-native-fs';

let CacheDir = RNFS.CachesDirectoryPath + '/';

{"bytesWritten": 0, "jobId": 1, "statusCode": 200}

function saveImageInCache(imageUrl, imageName) {
    let filePath = CacheDir + imageName;

    RNFS.downloadFile({
        fromUrl: imageUrl,
        toFile: filePath,
    }).promise.then(response => {
        if(response.statusCode == 200)
            return filePath;
    })
}

async function isExistImage(imagePath) {
            await RNFS.exists(imagePath)
                .then((exists) => {
                    return exists;
                });
        }