我有一个flatList,它从API循环播放PDF文档。用户可以将文档下载到世博会文件系统。如果文件存在于世博会文件系统中,如何隐藏下载按钮?
编辑:
功能:
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}
/>
正如你在docs expo-file-system中看到的,FileSystem.getInfoAsync(fileUri, options(返回一个承诺,所以你可以像这样修改:
checkFileExists = async(doc) => {
let result = await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'app_docs/' + doc);
if(result.exists){
return true;
} else {
return false;
}
}
{this.checkFileExists(item.doc_file) &&
<TouchableOpacity onPress={() => this.DownloadDocument(item.doc_file)}>
<Text style={styles.orangeBtn}>Download</Text>
</TouchableOpacity>
}
希望对您有所帮助。 随意怀疑