我正在构建一个简单的图像列表应用程序,具有一些额外的功能,其中之一是下载图像功能。
但是,我不断收到“找不到变量项”错误。我该如何解决这个问题?
item.image
// ...
downloadImage = () => {
const fileUri = `${FileSystem.documentDirectory}breathtaking.jpg`
FileSystem
.downloadAsync(item.image, fileUri)
.then(({ uri }) => {
console.log('Finished downloading to ', uri)
})
CameraRoll.saveToCameraRoll(fileUri, 'photo')
}
// ...
return (
// ...
<Button
icon='file-download'
mode='contained'
onPress={this.downloadImage}
style={{ borderRadius: 0, width: '33.4%' }}
/>
// ...
);
更新
componentDidMount() {
const url = "/* json */";
this.setState({ isLoading: true });
fetch(url)
.then(response => response.json())
.then(responseJson => {
this.setState({
dataSource: shuffle(responseJson.listings),
dataBackup: responseJson.listings,
isLoading: false
});
})
.catch(error => {
console.log(error);
});
}
renderItem = ({ item }) => {
return (
<View style={{ padding: 15, paddingBottom: 0 }}>
<Card elevation={1}>
<View
style={{
flex: 1,
flexDirection: "row",
flexWrap: "wrap",
alignItems: "flex-start"
}}
>
<View style={{ flex: 1 }}>
<TouchableOpacity
onPress={() => {
this.toggleModal();
this.setState({
webViewurl: item.image
});
}}
onLongPress={() => Linking.openURL(item.image)}
activeColor="blue"
>
<ImageBackground
source={{ uri: item.image }}
style={{ height: 216 }}
>
<IconButton
icon="favorite-border"
size={20}
color="#6200EE"
style={{ alignSelf: "flex-end" }}
onPress={this._savedAlert}
/>
</ImageBackground>
</TouchableOpacity>
</View>
</View>
</Card>
</View>
);
}
最佳答案
您尚未声明变量item
// ...
downloadImage = () => {
const item = {}
const fileUri = `${FileSystem.documentDirectory}breathtaking.jpg`
FileSystem
.downloadAsync(item.image, fileUri)
.then(({ uri }) => {
console.log('Finished downloading to ', uri);
})
CameraRoll.saveToCameraRoll(fileUri, 'photo');
}
// ...
你可以做这样的事情
关于javascript - 如何修复 "Can' t 查找变量 : item",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58751980/