【问题标题】:ReactNative Image | setNativeprops unable to change source反应原生图像 | setNativeprops 无法更改来源
【发布时间】:2017-08-18 10:48:42
【问题描述】:

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

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}/>

【问题讨论】:

    标签: react-native


    【解决方案1】:

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

    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 })} />
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 2021-05-22
      • 2021-01-05
      • 2021-08-23
      • 2013-08-21
      • 1970-01-01
      相关资源
      最近更新 更多