reactjs - 如何在平面列表中的本地反应中对图像设置 onError
问题描述
我有一个页面,在其中列出人员及其信息。我使用 Flatlist 来渲染我得到的 n 个数据以及分页。
<FlatList
showsVerticalScrollIndicator={false}
data={this.state.records}
keyExtractor={(item) => String(item.ServiceRequestID)}
renderItem={({ item }) => (
<View>
<ImageBackground
source={{ uri: this.state.baseUserImagePath + item.ImagePath }}
style={styles.image}
imageStyle={{ borderRadius: h2p(48) }}
/>
//other parts
</View>
)}
onEndReached={()=>{//logic}}
/>
我希望为无法加载的图像加载默认用户图像(在我的情况下在本地服务器中删除)。所以,我想在图像背景中添加 onError 属性。对于单个图像,我可以使用
onError={() => {this.setState({ image: require('../../assets/user.png') });}}
并使用source={this.state.image}
如何为 Flatlist 中的图像执行此操作以处理我的情况。
解决方案
我们可以创建一个单独的组件并在 flatlist renderItem 函数中使用它。
像:
const FlatListComponent = props => {
const [isError, setError] = useState(false)
return (
<View>
<ImageBackground
source={isError ? require('show-defaultImage') : { uri: props.image }}
style={styles.image}
imageStyle={{ borderRadius: h2p(48) }}
onError{(e) => setError(true)}
/>
//other parts
</View>
)
}
平面列表代码
<FlatList
showsVerticalScrollIndicator={false}
data={this.state.records}
keyExtractor={(item) => String(item.ServiceRequestID)}
renderItem={({ item }) => (
<FlatListComponent
image={this.state.baseUserImagePath + item.ImagePath}
/>
)}
onEndReached={()=>{//logic}}
/>
推荐阅读
- docker - docker_service - 使用 ansible 配置 docker 容器
- javascript - 在对象数组中的数组中查找元素
- ios - react-native-maps:必须将 AirGoogleMaps 目录添加到您的 xCode 项目中以支持 iOS 上的 GoogleMaps
- unicode - 属性中的字符被转换为 unicode
- dictionary - Elm:如何合并两个字典?
- c++ - 与剔除混淆的视图或透视矩阵
- php - 如果表只有一列,如何签入sql?
- c++ - 未能在线程中创建纹理
- spring-boot - 自定义 Oauth2 授权服务器 - AccessDeniedException:访问被拒绝
- flutter - Flutter - 从图库图像加载路径