如果url在加载时显示错误,如何再次加载图像url



我正在尝试使用图像组件从平面列表上的URL加载图像。在该组件中有一个属性(onError?:((=>void(在图像获取错误时调用此属性。当我在低网络中运行我的应用程序时,一些图像无法加载,那么我应该写什么代码(onError?:((=>void(,使得未能加载图像的URL应当在低网络中再次加载。

我正在iOS 的React Native中创建此应用程序

我已经做到了:

App.js

import React, { useState } from 'react';
import Product from './product';
import {
FlatList,
SafeAreaView
} from 'react-native';
const products = [
{productImage: "https://media.istockphoto.com/photos/poverty-concept-used-red-shoes-for-children-in-a-thrift-shop-between-picture-id1303292803?s=612x612"},
{productImage: 'https://media.istockphoto.com/photos/poverty-concept-used-red-shoes-for-children-in-a-thrift-shop-between-picture-id1303292803?s=612x612'},
{productImage: "https://media.istockphoto.com/photos/poverty-concept-used-red-shoes-for-children-in-a-thrift-shop-between-picture-id1303292803?s=612x612"},
{productImage: 'https://media.istockphoto.com/photos/poverty-concept-used-red-shoes-for-children-in-a-thrift-shop-between-picture-id1303292803?s=612x612'},
]
const App = () => {
return (
<SafeAreaView>
<FlatList 
numColumns={2}
data={products}
keyExtractor={(item, index) => index.toString()}
renderItem={({ item }) => (<Product product={item} />)}>
</FlatList>
</SafeAreaView>
);
};
export default App;

product.js

import React from 'react';
import { View, Image } from 'react-native';
class Product extends React.Component {
constructor(props){
super(props);
this.state = {
uri : this.props.product.productImage,
errorCount : 0
}
}
passInError(e) {
const { productImage } = this.props.product
if (this.state.errorCount < 3) {
this.setState({uri: productImage, errorCount: ++this.state.errorCount})
console.log(" Corrupt Image URL : " + productImage )
console.log(" Corrupt Image Error Reason : ", JSON.stringify(e) )
console.log (" Corrupt Image Reload Count : ", this.state.errorCount)
}
}
render() {
return (
<View>
<Image
style={{ width: 200, height: 200, borderWidth: 2, }}
source={{ uri:this.state.uri }}
onError = {e => this.passInError(e.nativeEvent) }
key = {this.state.errorCount}
/>
</View>
)
}
}
export default Product;

我应该在(onError?:((=>void(函数来重新加载失败的图像URL?

尝试将图像url设置为状态,并在加载图像时出错时进行更新。

product.js

import React from 'react';
import { View } from 'react-native';
import FastImage from 'react-native-fast-image';
class Product extends React.Component {
constructor(props){
super(props);
this.state = {
uri : this.props.product.productImage,
errorCount : 0
}
}
render() {
const { productImage } = this.props.product
return (
<View>
<FastImage
style={{ width: 200, height: 200, borderWidth: 2, }}
source={{ uri:this.state.uri }}
resizeMode={FastImage.resizeMode.contain}
onError={e =>
this.state.errorCount < 3 &&
this.setState(
{uri: '', errorCount: ++this.state.errorCount},
() => this.setState({uri: productImage}),
)
}
/>
</View>
)
}
}
export default Product;

如果我理解正确,当第一次尝试时出现错误时,您希望尝试第二次加载相同的图像。我会尝试在错误时重新渲染组件(更好的是,用包装器组件包装图像组件,这样整个产品组件就不会被重新渲染(:

const Product = () => {
const [fetchCounter, setFetchCounter] = useState(0);
return (
<img 
src={'imageUrl'}
onError={() => {
if (fetchCounter < 1) {
setFetchCounter(fetchCounter++);
}
}} 
/>
)
}

我不知道你的用例,但你可以加载一个后备映像。它可能看起来像这样:

const Product = () => {
const [imageUrl, setImageUrl] = useState('product-img-url');
return (
<img 
src={imageUrl}
onError={() => setImageUrl('fallback-img-url')} 
/>
)
}

最新更新