I’m fairly new to React Native/Expo but have been getting on ok. This has stumped me though!
I have a flatList which is looping round PDF documents from an API. The user can download documents to the Expo FileSystem. Any ideas on how can i hide the download button if the file exists in the Expo fileSystem?
Code i have so far below (checkFileExists is in a componentDidMount):
async checkFileExists(doc) {
try{
let result = await FileSystem.getInfoAsync(FileSystem.documentDirectory+'app_docs/'+doc);
console.log(result)
if(result.exists){
return true;
} else {
return false;
}
} catch(err){
console.log(err)
}
}
<FlatList
contentContainerStyle={styles.gridContainer}
keyExtractor={(item) => item.doc_id}
data={this.state.docs}
renderItem={({ item }) => {
if(this.state.rows > 0){
return(
<View style={styles.itemBox}>
<View style={styles.innerBox}>
<Text style={styles.itemTitle}>{item.doc_title}</Text>
<Text style={styles.itemDesc}>{item.doc_desc}</Text>
{item.dl_permitted !=0 &&
<TouchableOpacity title={item.doc_title} onPress={() => this.props.navigation.navigate('DocPdf', {PdfFile: 'https://example.com/library/documents/'+item.doc_file, headTitle: item.doc_title})}><Text style={styles.outlineBtnBox}>View</Text></TouchableOpacity>
}
{this.checkFileExists(item.doc_file) &&
<TouchableOpacity onPress={() => this.DownloadDocument(item.doc_file)}>
<Text style={styles.orangeBtn}>Download</Text>
</TouchableOpacity>
}
{item.dl_permitted===0 &&
<Text style={{marginTop:10,color:'#f36a48', fontWeight:'bold'}}>This document is available for purchase from the website.</Text>
}
</View>
</View>
)
}
}}
initialNumToRender={15}
maxToRenderPerBatch={15}
windowSize={15}
ListHeaderComponent={this.renderHeader()}
onEndReached={() =>this.LoadedDocs()}
onEndReachedThreshold={0.8}
ListFooterComponent={this.LoadingDocs()}
bounces={false}
/>