React native 如果文件系统中存在文件,则隐藏FlatList中的下载按钮
我有一个flatList,它循环使用API中的PDF文档。用户可以将文档下载到Expo文件系统。如果文件存在于Expo文件系统中,如何隐藏下载按钮 编辑: 功能:React native 如果文件系统中存在文件,则隐藏FlatList中的下载按钮,react-native,expo,React Native,Expo,我有一个flatList,它循环使用API中的PDF文档。用户可以将文档下载到Expo文件系统。如果文件存在于Expo文件系统中,如何隐藏下载按钮 编辑: 功能: async checkFileExists(doc) { try{ let result = await FileSystem.getInfoAsync(FileSystem.documentDirectory+'app_docs/'+doc); console.log(result) if(result.exists
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}
/>
item.doc\u id}
数据={this.state.docs}
renderItem={({item})=>{
如果(this.state.rows>0){
返回(
{item.doc_title}
{item.doc_desc}
{item.dl_允许!=0&&
this.props.navigation.navigate('DocPdf',{PdfFile:'https://example.com/library/documents/'+item.doc_文件,标题:item.doc_title}>View
}
{this.checkFileExists(item.doc_文件)&&
this.DownloadDocument(item.doc_文件)}>
下载
}
{item.dl_允许===0&&
本文件可从网站上购买。
}
)
}
}}
initialNumToRender={15}
maxToRenderPerBatch={15}
WindowsSize={15}
ListHeaderComponent={this.renderHeader()}
onEndReached={()=>this.LoadedDocs()}
onEndReachedThreshold={0.8}
ListFooterComponent={this.LoadingDocs()}
反弹={false}
/>
正如您在文档中看到的,文件系统。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>
}
checkFileExists=async(doc)=>{
让结果=等待FileSystem.getInfoAsync(FileSystem.documentDirectory+'app_docs/'+doc);
if(result.exists){
返回true;
}否则{
返回false;
}
}
{this.checkFileExists(item.doc_文件)&&
this.DownloadDocument(item.doc_文件)}>
下载
}
希望能有帮助。不要怀疑我刚刚试过这个。当i
console.log(result)
所有结果都显示为存在:false,即使其中一些应该为true。它们肯定存在于app_docs文件夹中,因为我在另一个屏幕上循环浏览它们。