在react-native中我们可以通过setNativeProps直接改动组件并出发局部刷新,不用使用props或state.
对此,官方的说明以及使用场景:
![](https://img.haomeiwen.com/i3112038/5b3a00ab6812591e.png)
源码中对Image中的组件属性描述:
node_modules/react-native/Libraries/Image/Image.android.js
![](https://img.haomeiwen.com/i3112038/64f5cf13bf1678f1.png)
node_modules/react-native/Libraries/Image/Image.ios.js
![](https://img.haomeiwen.com/i3112038/fd08afac57dda14c.png)
通过以上内容,我们可以看到,在通过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
网友评论