react-native Image使用setNativeProps问题

在react-native中我们可以通过setNativeProps直接改动组件并出发局部刷新,不用使用props或state.
对此,官方的说明以及使用场景:
image.png

源码中对Image中的组件属性描述:

node_modules/react-native/Libraries/Image/Image.android.js
image-android
node_modules/react-native/Libraries/Image/Image.ios.js
image-ios

通过以上内容,我们可以看到,在通过image使用setNativeProps直接修改图片源的时候,在设置的时候还是不一样的,Android端是src, IOS端是source,需要注意这一点

使用方法如下:

1.导入source

import resolveAssetSource from 'resolveAssetSource'

2.图片组件

<Image
                  ref={'backImage'}
                  source={require('images/tabbar/back_white.png')}
              />

3.需要修改图片的地方

let sourceAttr = Platform.OS === 'ios' ? 'source' : 'src';
this.refs.backImage.setNativeProps({
        [sourceAttr]: [resolveAssetSource(require('images/tabbar/back_black.png'))]
      })

ok,大功告成。

ps: react-native开发之旅,痛并快乐着。?
最后,在此感谢:https://blog.csdn.net/lu1024188315/article/details/73733724

发布了84 篇原创文章 · 获赞 13 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/wayne214/article/details/100044298