首页 > 解决方案 > 有什么方法可以学习 uri 没有图像 react-native

问题描述

我正在开发一个电子商务应用程序,有些产品没有图片,因此有时 ıimage 组件看起来是空的。看起来很糟糕。(要获取图片,我正在使用 uri。)

我的问题是我怎么知道那个uri上没有图片?

<FastImage
          source={{
            uri: photoUri,
            priority: FastImage.priority.high,
          }}
          style={styles.logo}
        />

标签: react-native

解决方案


你可以试试 react-native-elements,它会在 uri 错误时显示 PlaceholderImage

import { Image as ImageElement } from 'react-native-elements'
import { Image } from 'react-native'

<ImageElement
   style={{ width: 100, height: 100 }}
   source={{ uri: reduceImageSize(item.image, width) }}
   PlaceholderContent={(
     <Image source={require('image-placeholder.png')} style={{ width: 100, height: 100 }} />
   )}
 />

推荐阅读