微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如果 Image url 在 React Native 中出现 404 错误,我该如何使用默认图片?

如何解决如果 Image url 在 React Native 中出现 404 错误,我该如何使用默认图片?

如果图像 URL 出现 404 错误,我想加载认图像。我在图像道具中尝试了 onError() 但它不起作用。

<Image
              onError={() => {
                setError(true);
              }}
              source={{
                uri: error
                  ? imageUrl
                  : "https://logodownload.org/wp-content/uploads/2019/07/udemy-logo-5.png",}}
              style={{
                height: 50,width: "100%",alignItems: "center",justifyContent: "center",}}
              resizeMode="cover"
            />


我该怎么做?

解决方法

您可以在图像中使用 defaultSource 和默认图像,这会导致在提供的 imageUrl 无法加载时显示默认图像

var defaultImage = yourDefaultImageURl 

在你的图片中

    <Image
    defaultSource={defaultImage}
    source={{uri: imageUrl}}
       style={{
            height: 50,width: "100%",alignItems: "center",justifyContent: "center",}}
   resizeMode="cover"
   />
,

在这种情况下,如何将默认网址设置为错误

 onError={() => {       
 setImageUrl('https://logodownload.org/wpcontent/uploads/2019/07/udemy-logo-5.png');
          }}

在源码中

       source={{uri:imageUrl}}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。