ReactNative 图像 |setNativeprops 无法更改源 [英] ReactNative Image | setNativeprops unable to change source

查看:48
本文介绍了ReactNative 图像 |setNativeprops 无法更改源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为网络图像实现了图像 onError 方法,并且错误地尝试使用默认图像更新视图,但源属性无效

I have implemented the image onError method for network image and in error am trying to update the view with default image but the source property have no effect

onLoadError=(event)=>{
     alert('Img load error');
     this.imgPayIcn.setNativeProps({
       source :{uri:'https://facebook.github.io/react/img/logo_og.png'} //not able to update view with new image... no effect


     });

渲染方法

<Image ref={(ref) => this.imgPayIcn = ref} source={this.props.source} style={{height:50,width:50}} resizeMode='cover' onError={this.onLoadError}/>

推荐答案

将您的图像包装在处理错误的组件中:

Wrap your image in a component which handles the error:

class ImageWithFallback extends Component {
  state = {
    failed: false,
  }

  render() {
    if (this.state.failed) {
      return <Image source={require('imageFallback.png')} />
    }
    return <Image {...this.props} onError={() => this.setState({ failed: true })} />
  }
}

这篇关于ReactNative 图像 |setNativeprops 无法更改源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆